+ 1

Operator Precedence

can someone please explain more to me? please explain in the real example

2nd Apr 2019, 11:38 AM
Amir Reza
Amir Reza  - avatar
4 Answers
+ 10
HonFu 👍 Thanks a lot for correction!
2nd Apr 2019, 2:16 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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.
2nd Apr 2019, 12:02 PM
HonFu
HonFu - avatar
+ 2
very thank you for answer
2nd Apr 2019, 1:01 PM
Amir Reza
Amir Reza  - avatar
+ 2
Danijel Ivanović, that list differs from the official documentation. Is it maybe from an older version or something?
2nd Apr 2019, 1:14 PM
HonFu
HonFu - avatar