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

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.

Related

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)

This is regarding Python Exponentiation [duplicate]

This question already has answers here:
Why is exponentiation applied right to left?
(4 answers)
Closed 2 years ago.
print (2**3**2)
Answer is 512.
Why 512 is answer not 64? Because ((2^3)^2) = 64
I want to know the inside math operation of print (2** 3**2)
The order of operations for exponentiation is right-to-left, not-left-to right. So:
2**3**2
is interpretted as:
2**(3**2) = 2**(9) = 512

What does the "#" operator do in Python? [duplicate]

This question already has answers here:
numpy matrix vector multiplication [duplicate]
(1 answer)
What does the "at" (#) symbol do in Python?
(14 answers)
Closed 3 years ago.
I am trying to understand some code in which I found this statement. I don't know what "#" sign is doing here.
This is a running code for landmark detection, you can check full code here: https://github.com/cleardusk/3DDFA/blob/master/utils/ddfa.py
vertex = p # (u + w_shp # alpha_shp + w_exp # alpha_exp).reshape(3, -1, order='F') + offset
I believe it's used for matrix multiplication. Refer this for more: What does the "at" (#) symbol do in Python?

Python 3 - convert mathematical action to integer [duplicate]

This question already has answers here:
Math operations from string [duplicate]
(8 answers)
Closed 6 years ago.
I have a string with a formula 5 - 3, and I need to get the result in integer. How could I do that?
use eval function:
eval("5 - 3") # 2
test = "5-3"
print(eval(test))
Gives 2

How to write if-then statement as a logical statement? [duplicate]

This question already has answers here:
Python ? (conditional/ternary) operator for assignments [duplicate]
(2 answers)
Closed 7 years ago.
I Java it's possible to represent IF-THEN statement in the following form:
a = (x==10) ? true : false;
that is equivalent to
if (x==10)
a=true;
else
a=false;
Is it possible to do the same thing in Python?
a = True if x == 10 else False
or simply
a = x == 10

Categories

Resources