+ 1
Operator Precedence
can someone please explain more to me? please explain in the real example
4 ответов
+ 10
HonFu 👍
Thanks a lot for correction!
+ 8
You know from math class, that some operations are executed before others.
When you write 4*5 + 1 or 1 + 4*5, it leads to the same result: The multiplication is executed first, then the addition.
Python does it just like that, but it has such an order of operations for all the remainder of the language as well.
It is actually not that simple, you need to think about it and experiment until you understand. It comes with experience.
The complete list is here under operator precedence:
https://docs.python.org/3/reference/expressions.html
In that list for example you see that Python treats arithmetics just like you'd expect; you also see that all sorts of comparisons are executed later.
So when you have for example a condition like this:
4+5>=8
You know that first 4+5 is calculated and only then the comparison is done.
Also you see that and is 'quicker' than or.
That means in this condition:
a>5 and b<9 or c==a+b
first a+b would take place, then the two parts around and, and or last.
+ 2
very thank you for answer
+ 2
Danijel Ivanović, that list differs from the official documentation.
Is it maybe from an older version or something?