This question already has answers here:
What does & mean in python [duplicate]
(5 answers)
Bitwise operation and usage
(17 answers)
Python if statement without == operator
(3 answers)
'if' not followed by conditional statement
(7 answers)
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 3 months ago.
I have the following code segment in python
if mask & selectors.EVENT_READ:
recv_data = sock.recv(1024)
if recv_data:
data.outb += recv_data
else:
print(f"Closing connection to {data.addr}")
Would I read this as: 'if mask and selectos.EVENT_READ are equivalent:'
And similarly: 'if recv_data is equivalent to true:'
Help is greatly appreciated!
For the second assumptions, yes.
if var_name: is shorthand of saying if var_name evaluates to a truthy value.
Your first assumption is wrong though, a logical AND operation in python is actually and, not & - many languages do use an ampersand as a logical and, but this is usually a double ampersand, as in &&. A single ampersand is usually a bitwise AND, not a logical AND.
So in your code above, the first if statement is doing a bitwise (binary) AND on the selectors.READ_EVENT with a bitmask of mask. Basically its a way of asking if the values match, in a binary way. So if READ_EVENT is 010 and the mask is also 010, then the logic evaluates to true. Otherwise
All values have an inherent Boolean value. For the bytes value returned by sock.recv, the empty value b'' is false and all other values are true. This is a short, idiomatic way of writing
if recv_data != b'':
...
The & is a bitwise AND operator. The result of mask & selectors.EVENT_READ produces a value where a bit is 1 if both mask and selectors.EVENT_READ have a 1, and 0 otherwise. The result of that is an integer which may or may not be 0 (and for int values, 0 is false and all others are true).
Basically, mask & selectors.EVENT_READ is true if and only if any of the bits set in selectors.EVENT_READ are also set in mask.
Related
This question already has answers here:
Real world use cases of bitwise operators [closed]
(41 answers)
Bitwise operation and usage
(17 answers)
Closed 12 months ago.
I understand that the bitwise and operator (&) is equivalent to a product of two bit values. When would I use it?
Please also help me understand what num&1 does in the code below:
def func(num):
n = 1 + func((3*num+1) if num&1 else (num>>1))
return n
As the comments mentioned, num&1 is a bitwise AND between num and 1.
Since 1 in binary is ...000000001, the AND will result True iff the least significant bit of num is 1, in other words, if it is odd (here some explanation of binary)
This question already has answers here:
The tilde operator in Python
(9 answers)
Closed 1 year ago.
I'd like to check if a dataframe is empty or not. use ~df.empty return -2 while using Not df.empty return False.
why I cannot use ~?
df.empty
True
~df.empty
-2
not df.empty
False
In Python ~ is the bitwise not operator, it takes the bits and swaps 1s and 0s. For boolean values, true is 0b00000001 and false is 0b00000000 so ~true is 0b11111110 which, since bool a special case one byte int, evaluates as the signed integer value -2.
not on the other hand is the logical not operator, if a value is true/truthy it returns false, if a value is false/falsy it returns true, it doesn't care about the specific bits, only that (generally) at least one of the bits is 1.
Operation
True0b00000001
False0b00000000
~
-20b11111110
-10b11111111
not
False0b00000000
True0b00000001
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 1 year ago.
I wondering why the following tests return False:
Suppose I have 2 simple strings:
str0 = "trade"
str1 = "`trade"
I don't understand why the following tests in python return False:
str1.replace("`", "") is str0
And,
"".join(list(str1)[1:]) is str0 => False
Thanks for your education!
In Python, is compares two objects in memory, == compares their values. Since your two variables are stored at two different places in memory, your comparison using is evaluates to false.
This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 2 years ago.
Why "~False" is -1 in Python?
I was using a boolean variable in Python. When I try to do not of that, it returns -1. I want to understand why so? why a variable a changing its data-type duo to this operation.
Trying to add more details
b0 = False
print(type(b0))
b0 = ~b0
print(type(b0))
>>bool
>>int
The tilde ~ is the bitwise 'not' operator, not the boolean 'not' operator. To do a not you probably want 'not False'.
The reason for it changing its data type is that it treats False as binary 0 and then flips it to -1.
This question already has answers here:
Does Python evaluate if's conditions lazily? [duplicate]
(7 answers)
Closed 4 years ago.
print(True or 5 / 0 > 3)
This is my code but it returns True
Is there a reason why it doesn't return a Zero Division Error?
Its because True is always true. The Python interpreter does not evaluate the right side of the or operator in this case, because the outcome of an or expression is always true if one of its operands is true. If you enter5 / 0 > 3 or True than you get your devision by zero error