-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoolean&Comparatifs.py
More file actions
340 lines (254 loc) · 6.32 KB
/
Boolean&Comparatifs.py
File metadata and controls
340 lines (254 loc) · 6.32 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
334
335
336
337
338
339
340
########## BOOLEANs & COMPARATIFs ##########
### EQUALITES
'''
== EQUAL TO
!= NOT EQUAL TO
> UPPER THAN
>= UPPER THAN or EQUAL TO
< LESSER THAN
<= LESSER THAN OR EQUAL TO
Output is True OU False
True = 1
False = 0
Applies on strings and numbers
'''
### BOOLEANs
'''
==> AND
True and True = True
True and False = False
False and True = False
False and False = False
==> OR
True or True = True
True or False = True
False or True = True
False or False = False
==> NOT
not = INVERSE
not True = False
not not True = True
not not not not True = True
==> PRIORITE
1- NOT
2- AND = CONJUNCTION
3- OR = DISJONCTION
z = 2
y = 1
x = y < z or z > y and y > z or z < y ==> x = y < z or (z > y and y > z) or z < y
print(x)
# True
'''
### bool()
# ==> bool(x) = True SAUF si x = None or empty
print(bool(23))
True
print(bool(True))
True
print(bool(None))
False
print(bool([]))
False
print(bool(()))
False
print(bool(''))
False
### Usages
# INT
print ((4<5) and (4<6))
True
print ((8<5) and (4<6))
False
print ((8<5) or (4<6))
True
# Bool
print(True > False)
True
print(True < False)
False
# STR
animal = "tigre"
print (animal == "tig")
False
print (animal != "tig")
True
print (animal == 'tigre')
True
# !!! We can compare strings, alphabetic order applies
print ("a" < "b")
True
print ("ali" < "alo")
True
print ("abb" < "ada")
True
print ('alpha' < 'alphabet') # longer = bigger
True
print ('beta' > 'Beta') # b = 98 vs B = 66
True
print('10' > '010') # 1 = 49 vs 0 = 48
True
print('10' > '8') # 1 = 49 vs 8 = 56
# False
print('20' < '8') # 2 = 50 vs 8 = 56
# True
# But a string IS NOT equal to the first matching codepoint
print('10' == 49)
# False
print('10' == 10)
# False
print('10' != 10)
# True
print('10' == 1)
# False
print('10' != 1)
# True
# We cannot compare STR with INT/FLOAT
print('10' > 10)
# TypeError: '>' not supported between instances of 'str' and 'int'
# We can compare Bool with INT
print(True == 1)
True
print(False == 0)
False
print(int(True == 1))
1
print(float(False == 0))
1.0
### REMINDR - Operators priority
# Priority Operator
# 1 +, - unary
# 2 **
# 3 *, /, //, %
# 4 +, - binary
# 5 <, <=, >, >=
# 6 ==, !=
#########################
### None
# It is true that the None value CAN NOT BE USED AS AN ARGUMENT in ARITHMETIC OPERATIONS
# But the None value can be assigned and compared to variables and that can happen outside of a function
b = None
print (b) # prints nothing (None)
#
print(None + True)
# TypeError: unsupported operand type(s) for +: 'NoneType' and 'bool'
print(None + 2)
# TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
print(None + "none")
# TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
### in, not in
alphabet = "abcdefghijklmnopqrstuvwxyz"
print("f" in alphabet)
True
print("F" not in alphabet)
True
# spécificité Python ==> empty string in string = TRUE !!
print("" in "alphabet")
True
### BITWISE operations
## x << y
# Returns x with the bits shifted to the left by y places
# (and new bits on the right-hand-side are zeros).
# This is the same as multiplying x by 2**y.
print (2<<2) # 0010 ==> 1000 = 8
# 8
## x >> y
# Returns x with the bits shifted to the right by y places.
# This is the same as //'ing x by 2**y.
print (2>>2) # 10 ==> 00, we go out but no error
# 0
a = 5 # 0101
print(a >> 1) # 0010 = 2
# 2
print(a >> 2) # 0001 = 1
# 1
## x & y - AND
# Does a "bitwise and".
# Each bit of the output is 1 if x AND y is 1, otherwise it's 0.
a = 5 # (0101 in binary)
b = 3 # (0011 in binary)
print(a & b) # (0001 in binary) = 1
# 1
## x | y - OR
# Does a "bitwise or".
# Each bit of the output is 0 if x AND y is 0, otherwise it's 1.
### | (Bitwise OR)
# If at least one bit is 1, the result is 1.
a = 5 # (0101 in binary)
b = 3 # (0011 in binary)
print(a | b) # (0111 in binary) = 7
# 7
## ~ x
# Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1
# ==> Calculer -(x + 1)
x=2
print (~x)
# -3
x=-1
print (~x)
# 0
## x ^ y
# Does a "bitwise exclusive or".
# if bits are equals ==> 0
# if bits are differents ==> 1
x = 5 # 101
y = 3 # 011
print(x ^ y) # 110 = 6
# 6
## Logic and bit operations
# & (ampersand) - bitwise conjunction;
# | (bar) - bitwise disjunction;
# ~ (tilde) - bitwise negation;
# ^ (caret) - bitwise exclusive or (xor).
# Bitwise operations (&, |, and ^)
# Arg A Arg B Arg A & Arg B Arg A | Arg B Arg A ^ Arg B
# 0 0 0 0 0
# 0 1 0 1 1
# 1 0 0 1 1
# 1 1 1 1 0
# Bitwise operations (~)
# Arg ~Arg
# 0 1
# 1 0
# abbreviated forms:
# x = x & y ==> x &= y
# x = x | y ==> x |= y
# x = x ^ y ==> x ^= y
### Deal with SINGLE bits
'''
To understand:
Variable example on which we want to consider 1*bit:
flag_register = 0x1234
binary = 0000 0000 0000 0000 0001 0010 0011 0100
bits order = 31, 30, etc, 0, from left to right
we are given the bit 3 = 4th bit from the first, from right to left
==> flag_register = 0000000000000000000000000000x000
## Check bit state
we can use conjunction property:
x & 1 = x
x & 0 = 0
we apply a bit mask to the bit we want to check
00000000000000000000000000001000
0000000000000000000000000000x000
if the result is True, equal to 1, the bit is activated
else, the bit is False, equal to 0, deactivated
ex:
the_mask = 8 # = 0000 0000 0000 0000 0000 0000 0000 1000
if flag_register & the_mask:
print ("My bit is set")
else:
print ("My bit is NOT set")
## Reset the bit state
flag_register = flag_register & ~the_mask
~the_mask = 1111 1111 1111 1111 1111 1111 1111 0111
flag_register &= ~the_mask
## Set the bit to 1
x | 1 = 1
x | 0 = x
flag_register = flag_register | the_mask
flag_register |= the_mask
## Inverse the bit
x ^ 1 = ~x
x ^ 0 = x
flag_register = flag_register ^ the_mask
flag_register ^= the_mask
'''