How do i use arithmetic in order of precedence?
-
Python, like most other programming languages, follows a precedence when using arithmetic.
* and /
Have the same precedence and are calculated first
+ and -
Have the same precedence and are calculated after * or /
e.g: 2 + 4 * 3 / 2
Ans:
4 * 3 = 12
12 / 2 = 6
6 + 2 = 8.0( )
Using parenthesis around arithmetic changes the precedence, it is done first, however, * or / with parenthesis is still done first.
e.g: e.g: (2 + 4) * 3 / 2
Ans:
2 + 4 = 6
6 * 3 = 18
18 / 2 = 9.0
© Lightnetics 2024