syntax - What's the difference between "2*2" and "2**2" in Python? - Stack Overflow
2 ** 1 ** 3 = 2??
This is the standard order of operations for power towers in mathematics:
https://math.hmc.edu/funfacts/tower-of-powers/
I did not know that Python actually handles this correctly. That is awesome.
More on reddit.comPython Operator precedence - 2 ** 3 ** 2 ** 1 = 512 - Not sure how? - Stack Overflow
Is in your programming language `3/2=1` or `3/2=1.5`?
Videos
Try:
2**3*2
and
2*3*2
to see the difference.
** is the operator for "power of". In your particular operation, 2 to the power of 2 yields the same as 2 times 2.
Double stars (**) are exponentiation. So "2 times 2" and "2 to the power 2" are the same. Change the numbers and you'll see a difference.
Just a quick question for math in python: 2 ** 1 ** 3 evaluates to 2 but I'm not sure why? Both operators (**) are the same exponentiation operator so how come the operator on the right executes first?
In other words why does it execute like 2 ** (1 ** 3) instead of (2 ** 1) ** 3 like in normal left-to-right reading?
Thank you!
This is the standard order of operations for power towers in mathematics:
https://math.hmc.edu/funfacts/tower-of-powers/
I did not know that Python actually handles this correctly. That is awesome.
The documentation does note that exponentiation is done right to left, while other operators are left to right. See here for the following quote:
Operators in the same box group left to right (except for exponentiation, which groups from right to left).
That doesn't offend my intuition too greatly, as if I have "stacked" exponents I likely want the right exponent to be part of the exponent, that is, I'd want 213 not (21 )3.
When in doubt, perhaps it's best to add parenthesis to ensure your meaning is clear.
Python operators usually evaluate left to right, except for the exponentiation operator:
Operators in the same box group left to right (except for exponentiation, which groups from right to left).
Source
Thus,
2 ** 3 ** 2 ** 1
is the same as
2 ** (3 ** (2 ** 1))
Not a Python developer myself, so other people can correct me if I'm wrong, but I'd venture to guess this is how it's getting parsed:
2 ** (3 ** (2 ** 1))
= 2 ** (3 ** 2)
= 2 ** 9
= 512
Like I've written on my blog:
Notice that in AEC for WebAssembly,
3/2=1(as in C, C++, Java, C#, Rust and Python 2.x), while, in AEC for x86,3/2=1.5(as in JavaScript, PHP, LISP and Python 3.x). It's hard to tell which approach is better, both can produce hard-to-find bugs. The Pascal-like approach of using different operators for integer division and decimal division probably makes the most sense, but it will also undeniably feel alien to most programmers.
In Python 2, / performs integer division. What this means is that the result, if it is not an integer, is rounded down to the next integer value. When the value is negative, this naturally rounds to a greater-magnitude negative number.
Intuitively, the result of integer division is simply the mathematical floor of the result of float division. For this reason, integer division is also commonly referred to as floor division.
floor(1.5) # Returns 1.0
floor(-1.5) # Returns -2.0
It's possible to alter this behavior in Python 2 by putting from __future__ import division at the top of your module. This import will make the / operator indicate only true division (float division), and enable explicit floor division (integer division) with the // operator. These conventions are the standard in Python 3.
from __future__ import division
print(3/2) # 1.5
print(3//2) # 1
As @Dunes notes in the comments, it's worth noting that - has a higher precedence than /, and therefore -3/2 is equivalent to (-3)/2 rather than -(3/2). If the division were applied first, the result would indeed be -1.
-3/2 == -1.5 , floor(-1.5) = -2
likewise
3/2 == 1.5 , floor(1.5) = 1