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
Related
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:
Calculation error with pow operator
(4 answers)
Closed 1 year ago.
Can someone explain the difference between these two expressions in Python:
(-1)**2 == 1
-1**2 == -1
Why do the parentheses change the outcome?
The parentheses means the whole value inside is will be raised to the power 2.
(-1)**2 == 1
So -1*-1 is 1
No parentheses means the - will be taken out of the equation and added to the end of the answer.
1) -1**2
2) 1**2
3) 1
4) -1
Python handles this the same way the world does :)
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:
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 4 years ago.
I am unable to figure out the output of the following program
big_num_1 = 1000
big_num_2 = 1000
small_num_1 = 1
small_num_2 = 1
big_num_1 is big_num_2 # False
small_num_1 is small_num_2 # True
What is happening above?
Why is one False and other True.
Source: https://luminousmen.com/post/python-interview-questions-senior
Because is compares the identity of two objects (that is, if they're exactly the same object.) You want to test for equality, and for that you must use the == operator:
big_num_1 == big_num_2
=> True
small_num_1 == small_num_2
=> True
In case you're wondering why this example worked:
small_num_1 is small_num_2
=> True
It's because Python caches small (between -5 and 256) int objects internally, so the objects used in the comparison were taken from the cache and were the same. big_num_1 and big_num_2 are greater than 256, so they're represented by two different objects, and the identity test fails.
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