Skip to content
Worked Examples · Example 5.11

Q.Example 5.11: How will Python evaluate the following expression?
(20 + 30) * 40

Odisha ChseTextbookSubjective· 2mImportance★★★★★est
43% · 21/49 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Concept understanding — Operator Precedence

Read the expression 2 + 3 * 4 aloud and two answers compete: work left to right and you get 20; do the multiplication first and you get 14. Humans resolve this with a school convention — multiplication before addition — so everyone agrees the value is 14. A programming language faces the same ambiguity in every expression it reads, and it resolves it the same way: with a fixed, published rule book called operator precedence.

Operator precedence is the ranking that decides, when an expression contains several operators, which operation is performed first. Python evaluates the operator of higher precedence before the operator of lower precedence, regardless of the order they appear in the line. Because the ranking is fixed, any given expression has exactly one meaning — the program cannot shrug and guess.

For the operators used most in Informatics Practices, the ranking from highest to lowest runs:

  • Parentheses ( ) — whatever is enclosed is evaluated first, overriding everything else.
  • Exponentiation **
  • Unary minus (a negative sign attached to a value)
  • Multiplication *, division /, floor division //, modulus % — all on one level
  • Addition + and subtraction - — on one level
  • Comparison operators such as <, >, <=, >=, ==, !=
  • not
  • and
  • or

Two companion rules complete the picture. First, operators sharing a level (such as * and /) are applied left to right as they appear — this tie-breaking order is called associativity. Second, exponentiation is the notable exception: a chain of ** groups from the right, so 2 ** 3 ** 2 means 2 ** (3 ** 2), which is 2 ** 9 = 512, not 8 ** 2 = 64.

Worked in slow motion:

result = 10 + 4 * 2 ** 2
# step 1: 2 ** 2 -> 4      (exponentiation first)
# step 2: 4 * 4  -> 16     (then multiplication)
# step 3: 10 + 16 -> 26    (addition last)
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.