Why "~False" is -1 in Python? [duplicate] - python

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.

Related

What does if x: mean in Python [duplicate]

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.

When to use the bitwise and operator (&)? [duplicate]

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)

Curious about the function of ~in the following code [duplicate]

This question already has answers here:
The tilde operator in Python
(9 answers)
Closed 1 year ago.
pd_selftest = pd_selftest[pd_selftest['SICCD'] != 0]
pd_selftest = pd_selftest[~pd_selftest['SICCD'].isnull()]
I'd like to know what the function of the ~ is in the above code.
That's the bit-wise invert or not operator. So, it returns only those lines where the SICCID column is not null. I would probably use the word not in this case.

Zero Division Error in boolean statement Python [duplicate]

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

the use of boolean logic "and" and bitwise operator "&" [duplicate]

This question already has answers here:
'and' (boolean) vs '&' (bitwise) - Why difference in behavior with lists vs numpy arrays?
(8 answers)
Closed 6 years ago.
sorry about this basic question, I just a beginner in Python programming.
From my undertsanding, & and "and" are the same things, and "&" is just a shorhand for "and" so if I use Python's dataframe
df[ (df.StateAb == "NSW") & (df.PartyAb == "LP") ]
this compliles OK,but if I type
df[ (df.StateAb == "NSW") and (df.PartyAb == "LP") ]
then it cannot be compiled correctly.
so what's the difference between "and" and "&",
I found this one useful:
1 and 2
>> 2
1 & 2
>> 0
The first result is due to short circuiting. Python tests 1 and finds it true and returns the 2. But, the second part does 01 (Binary 1) & 10 (Binary 2) hence evaluating to 00 (1 & 0, 0 &1) , which is 0.

Categories

Resources