This question already has answers here:
Is Python's order of evaluation of function arguments and operands deterministic (+ where is it documented)?
(2 answers)
Closed 4 months ago.
Is the python argument evaluation order defined behaviour? And is the order of evaluation the same as the appear in the source-code?
Consider this example:
print(f"{(a:=1)}", f"{a=}", f"{(a:=2)}", f"{a=}") # print 1
print(f"{a=}") # print 2
Is the first statement guaranteed to print "1 a=1 2 a=2"?
And is the second guaranteed to print "a=2"?
Yes, python expressions are evaluated left to right. This is documented here.
Related
This question already has answers here:
In what situation should the built-in 'operator' module be used in python?
(6 answers)
Closed 1 year ago.
from operator import add
n1 = add(2,3)
n2 = 2 + 3
Both return exactly the same number and I believe they also work in the same way. What are the advantages of using add() to calculate the sum of two numbers?
are there any cases where only one method is accepted?
Sometimes, you may want to pass the operator as a function somewhere, for example:
functools.reduce(operator.mul, [2,3,4,5]) # 120
You could, of course do something like:
functools.reduce(lambda x, y: x * y, [2,3,4,5])
but the operator reads better and is faster.
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:
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.
This question already has answers here:
Complex numbers in python
(3 answers)
Closed 4 years ago.
I want to find the absolute value of something like 'i' but when I type 'abs(1)' it says 'i' is not defined. What do I do?
In python to find the absolute value of a complex function you use j instead of i.
abs(a+bj) # General Format
abs(0+1j)
>> 1
Or you could define i as the square root of -1 and use it instead
i = (-1) ** 0.5
abs(i)
>> 1
This question already has answers here:
Priority of the logical operators (order of operations) for NOT, AND, OR in Python
(8 answers)
Closed 6 years ago.
When I evaluate the following expression:
1 or (1/0) and 1
What are the rules (precedence, short-circuit evaluation etc) are followed to get the answer
b or anything_else is defined to return b if b is true-ish, without evaluating anything_else. Since your first 1 is true-ish, your 1/0 never gets evaluated, hence no error. By "true-ish" I mean any value that Python considers true, not only the True boolean value. Try your expression with True or [2] in place of the first 1 to see what I mean.
Python short-circuit evaluates. In your example, the expression
1 or (1/0) and 1
finishes evaluation at the first 1 and returns True.
A more minimal example serves to illustrate Python's short-circuit evaluation. Consider the expression:
(1/0) or 1
This raises a ZeroDivisionError exception upon evaluation of (1/0). But the expression:
1 or (1/0)
short-circuit evaluates to True. No exception is raised since the sub-expression (1/0) never gets evaluated.