The following code are two larger expressions to illustrate operator grouping.
It also shows the difference in the division operator in Python 3.X and 2.X:
a = 3 b = 4 # ww w .jav a 2 s . com print( b / 2 + a ) # Same as ((4 / 2) + 3) [use 2.0 in 2.X] print( b / (2.0 + a) ) # Same as (4 / (2.0 + 3)) [use print before 2.7]
In the first expression, there are no parentheses, so Python automatically groups the components according to its precedence rules.
All the numbers are integers in the first expression.
Because of that, Python 2.X's / performs integer division and addition and will give a result of 5, whereas Python 3.X's / performs true division, which always retains fractional remainders and gives the result 5.0 shown.
If you want 2.X's integer division in 3.X, code this as b // 2 + a;
if you want 3.X's true division in 2.X, code this as b / 2.0 + a.
In the second expression, parentheses are added around the + part to force Python to evaluate it first.