+91 81900 98289

Operator Precedence

This Operator Precedence lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

Python chooses an order when several operators share a line. ** runs before multiplication, and multiplication runs before addition.

Parentheses force the order you mean. Add them when a reader would have to stop and think.

Example 1: Start here

2 + 3 * 4 is 14, because 3 * 4 happens first.

print(2 + 3 * 4) # the math runs, then print shows the result print((2 + 3) * 4) # the math runs, then print shows the result

Example 2: Multiply before add

Without parentheses the multiply happens first, so the first line is 14.

print(2 + 3 * 4) # the math runs, then print shows the result print((2 + 3) * 4) # the math runs, then print shows the result

Example 3: Power before multiply

3 ** 2 is 9, then 2 * 9 is 18.

print(2 * 3 ** 2) # the math runs, then print shows the result

Example 4: Comparisons chain

Python lets you write a range test in one line.

score = 82 # a whole number print(80 <= score < 100) # a comparison is True or False
2 + 3 * 4 is 14, because 3 * 4 happens first.
Call Now 81900 98289