-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_operators_2.py
More file actions
365 lines (220 loc) · 8.85 KB
/
python_operators_2.py
File metadata and controls
365 lines (220 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
لە پایتۆندا ئۆپەراتۆرەکان هێمای تایبەتن کە بۆ ئەنجامدانی ئۆپەراسیۆنەکان لەسەر گۆڕاوەکان و بەهاکان بەکاردەهێنرێن.
پایتۆن جۆرەها ئۆپەراتۆر دابین دەکات، کە دەتوانرێت بەم شێوەیە پۆلێن بکرێن
Arithmetic Operators , Comparison Operators , Logical Operators , Bitwise Operators
Identity Operators , Assignment Operators , Membership Operators
"""
# Arithmetic Operators
#ئەم ئۆپەراتۆرانە بۆ ئەنجامدانی ئۆپەراسیۆنە بنەڕەتییەکانی ژمێریاری بەکاردەهێنرێن
"""
+ Addition
- Subtraction
* Multiplication
/ Division
// Floor division
% Modulus
** Exponentiation
"""
add=5+3
print (add) # 8
sub=5-3
print(sub) # 2
mult=5*3 #جاران
print(mult) # 15
div=5/2
print(div) # 2.5
floor=5//2 # , 5 دابەشکردن بەسەر 2دا 2.5 دەدات Floor Division دەگۆڕێت بۆ نزیکترین ژمارەی تەواو
print (floor) # 2
moduls= 5%2 # 2 ---> 5 2*2 =4 chandi mawa ta bgat ba 5 ? 1 dana
print(moduls) # 1
exp=5**3 # 5 * 5 * 5
print(exp) # 125
#Comparison Operators
#ئەم ئۆپەراتۆرانە بۆ بەراوردکردنی دوو بەها و گەڕانەوەی بولی (ڕاست یان هەڵە) بەکاردەهێنرێن.
"""
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
"""
Equal_1=5
Equal_2=4
print(Equal_1 == Equal_2) # ئایا ئەوانە یەکسانن؟ False
print(Equal_1 != Equal_2) # True ئایا ئەوانە یەکسان نین؟
print(Equal_1 > Equal_2) # True 5 > 4
print(Equal_1 < Equal_2) # False 5 < 4
print(Equal_1 >= Equal_2) # True 5 >= 4
print(Equal_1 <= Equal_2) # False 5 <= 4
print(6 >= 8) # False 6 <= 8
#Logical Operators
#ئەم ئۆپەراتۆرانە بۆ تێکەڵکردنی ڕستەی مەرجدار if و گەڕانەوەی بەهایەکی bool بەکاردەهێنرێن.
"""
and
True دەگەڕێنێتەوە ئەگەر هەردوو ئۆپەراندەکە ڕاست بن
True and True -> True
True and False -> False
False and True -> False
False and False -> False
or
True دەگەڕێنێتەوە ئەگەر لانیکەم یەک ئۆپەراند ڕاست بێت
True and True -> false
True and False -> true
False and True -> true
False and False -> true
not دۆخی لۆژیکی ئۆپەراندەکەی پێچەوانە دەکاتەوە
true -> false
false -> true
"""
logic_1 = 10
logic_2= 5
logic_3= 20
anjami_op_1= (logic_1 > logic_2) and (logic_3 > logic_1)
print(anjami_op_1) # True (10 > 5 and 20 > 10)
anjami_op_2= (logic_1 > logic_2) and (logic_3 < logic_1)
print(anjami_op_2) # False (10 > 5 true, 20 < 10 false)
logic_3= 10
logic_4= 5
logic_5= 20
anjami_op_3 = (logic_3 > logic_4) or (logic_5 < logic_3)
print(anjami_op_3) # True (10 > 5 true, 20 < 10 false)
anjami_op_4 = (logic_3 < logic_4) or (logic_5 < logic_3)
print(anjami_op_4) # False
logic_6= 10
logic_7= 5
anjami_op_5 = not (logic_6 > logic_7)
print(anjami_op_5) # False (10 > 5 True )
anjami_op_6 = not (logic_6 < logic_7)
print(anjami_op_6) # True ( 10 < 5 False )
logic_8= 10
logic_9 = 5
logic_10= 20
anjami_op_7 = (logic_8 > logic_9) and (logic_10 < logic_8 or logic_8 == 10) and not (logic_8 == logic_9) # ! (10 <5) or (10 > 5 and 20==20)
print(anjami_op_7) # True (هەموو مەرجەکان ڕاستن)
anjami_op_7 = not (logic_8 < logic_9) or (logic_8 > logic_9 and logic_10 == 20) # ! (10 <5) or (10 > 5 and 20==20)
print(anjami_op_7) # True
# Assignment Operators
# ئەم ئۆپەراتۆرانە بەکاردەهێنرێن بۆ تەرخانکردنی بەهاکان بۆ گۆڕاوەکان.
"""
= Simple assignment
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
//= Floor divide and assign
%= Modulus and assign
**= Exponentiate and assign
"""
num_1 = 5
num_1 += 3 # num_1 = num_1 + 3
print(num_1) # 8
num_1 = 5
num_1 -= 3 # num_1 = num_1 - 3
print(num_1) # 2
num_1 = 5
num_1 *= 3 # num_1 = num_1 * 3
print(num_1) # 15
num_1 = 5
num_1 /= 3 # num_1 = num_1 / 3
print(num_1) # 1.6666666666666667
num_1 = 5
num_1 //= 3 # num_1 = num_1 // 3
print(num_1) # 1
num_1 = 5
num_1 %= 3 # num_1 = num_1 % 3
print(num_1) # 2
num_1 = 5
num_1 **= 3 # num_1 = num_1 ** 3
print(num_1) # 125
# Identity Operators
"""
لە پایتۆندا ئۆپەراتۆری ناسنامە بەکاردەهێنرێت بۆ ئەوەی بزانرێت ئایا دوو گۆڕاو ئاماژە بە هەمان شت دەکەن لە بیرگەدا.
ئەم ئۆپەراتۆرانە بەکارناهێنرێن بۆ پشکنینی ئەوەی کە ئایا بەهاکانی گۆڕاوەکان یەکسانن یان نا،
بەڵکو زیاتر ئاماژە بە هەمان شوێن دەکەن لە بیرگەدا یان نا.
"""
my_x = [1, 2, 3]
my_y = my_x
my_z = [1, 2, 3]
print(my_x is my_y) # True
print(my_x is my_z) # False
print(my_x == my_z) # True
print(my_x is not my_y) # False
print(my_x is not my_z) # True
# Membership Operators
#ئەم ئۆپەراتۆرانە بەکاردەهێنرێن بۆ پشکنینی ئەوەی کە ئایا بەهایەک لە زنجیرەیەکدا دۆزراوەتەوە یان نا (لیستی، توپڵ، و هتد).
"""
بەکاردەهێنرێن بۆ تاقیکردنەوەی ئەوەی کە ئایا بەهایەک لە زنجیرەیەک یان کۆمەڵێکدا ئامادەیە یان نا.
ئەم ئۆپەراتۆرانە بە شێوەیەکی سەرەکی بۆ پشکنینی ئەوەی کە ئایا توخمێک لە ڕستە،
لیست، توپڵ، کۆمەڵە، یان فەرهەنگەکاندا هەیە (یان بوونی نییە) بەکاردەهێنرێن.
in
not in
in
ئۆپەراتۆری in پشکنین دەکات کە ئایا بەهایەک لە ناو زنجیرەیەک یان کۆمەڵەیەکدا بوونی هەیە یان نا.
ئەگەر بەهاکە دۆزرایەوە، ئەوا True دەگەڕێنێتەوە؛ ئەگەرنا، False دەگەڕێنێتەوە.
"""
my_in= "apple"
print("a" in my_in) # True
print("z" in my_in) # False
fruits_in = ["apple", "banana", "cherry"]
print("apple" in fruits_in) # True
print("orange" in fruits_in) # False
colors_notin = {"red", "blue", "green"}
print("yellow" not in colors_notin) # True
print("blue" not in colors_notin) # False
print("R" in "programming") # False
nested_list_in = [[1, 2], [3, 4], [5, 6]]
print([1, 2] in nested_list_in) # True
print(1 in nested_list_in) # False
#Bitwise Operators
#ئەم ئۆپەراتۆرانە بۆ ئەنجامدانی ئۆپەراسیۆنەکان لەسەر ژمارە دووانەییەکان لە ئاستی بیتەکاندا بەکاردەهێنرێن.
"""
ئۆپەراتۆرەکانی بیتوایەز لەسەر ژمارەکان لە ئاستی دووانەییدا کاردەکەن.
لەبری ئەوەی کار لەگەڵ نوێنەرایەتی دەهەمی (بنەمای ١٠) ژمارەکان بکەن،
دەستکاری بیتەکانی (ژمارە دووانەییەکان: ٠ یان ١) ژمارە تەواوەکان دەکەن.
"""
binary_1= 5 # Binary: 0101
binary_2= 3 # Binary: 0011
my_result = binary_1 & binary_2 # Binary: 0001
print(my_result) # 1
"""
Bit of 5 Bit of 3 Result
0 0 0
1 1 0
0 1 0
1 1 1 = result = 0001 = 1
"""
binary_3= 5 # Binary: 0101
binary_4= 3 # Binary: 0011
my_result_2 = binary_3 & binary_4 # Binary: 0001
print(my_result_2) # 7
"""
Bit of 5 Bit of 3 Result
0 0 1
1 1 1
0 1 1
1 1 0 = result = 0111 = 7
"""
binary_5= 5 # Binary: 0101
binary_6= 3 # Binary: 0011
my_result_3 = binary_5 & binary_6
print(my_result_3) # 6
"""
Bit of 5 Bit of 3 Result
0 0 0
1 1 1
0 1 1
1 1 0 = result = 0110 = 6
"""
binary_7= 5 # Binary: 0101
my_result_4 = ~ binary_7
print(my_result_4) # -(0101 + 1) = -0110 =-6
binary_8= 5 # Binary: 0101
my_result_5 = binary_8 << 1 # یەک 0 زیاد دەکات بۆ ڕاست
print(my_result_5) # 10 0101 → 1010
binary_9= 5 # Binary: 0101
my_result_6 = binary_9 >> 1 # لابردنی یەک 0 زیاد دەکات بۆ ڕاست
print(my_result_6) # 2 0101 → 0010
list_1=[12,3]
list_2=[3,5]
print(list_1+list_2) # [12, 3, 3, 5]