Skip to content

Module 4 · Operators — topic 16 of 16

Associativity

When operators of equal precedence appear together, associativity decides the grouping. Learn left-to-right associativity, the right-to-left exception of **, and how chained comparisons work.

Beginner 10 min read·Lesson 59 of 306 in Python Programming

Learning objectives

  • Explain left-to-right associativity
  • Recognise ** as right-associative
  • Distinguish chained comparisons from left-to-right grouping

Definition

When operators of equal precedence appear together, associativity decides the grouping. Learn left-to-right associativity, the right-to-left exception of **, and how chained comparisons work.

Introduction

Precedence decides between different operators. But what about 100 / 10 / 2, where both operators are the same? Associativity decides the grouping: Python reads it as (100 / 10) / 2, which is 5.0, not 100 / (10 / 2), which would be 20.0.

Why it matters

Associativity rarely matters for + or *, but it changes the answer for -, /, // and **. It also explains why chained comparisons such as a < b < c behave differently from the way they would in some other languages.

Detailed explanation

Most binary operators are left-associative: they group from left to right. 10 - 4 - 3 is (10 - 4) - 3 = 3.

For + and * the grouping doesn’t change mathematical results, but for -, /, // and % it does.

** is right-associative: 2 ** 3 ** 2 is 2 ** (3 ** 2) = 2 ** 9 = 512. This matches mathematical convention for stacked exponents.

Comparisons are not grouped at all; they chain. a < b < c means a < b and b < c, with b evaluated only once. It does not mean (a < b) < c.

Assignment chaining, a = b = 0, assigns the same value to every target from left to right.

When in doubt, parentheses remove any dependence on associativity.

Syntax

Python
a - b - c # (a - b) - c
a ** b ** c # a ** (b ** c)
a < b < c # a < b and b < c
  • Left-associative operators group from the left.
  • ** groups from the right.
  • Comparisons chain instead of grouping.

Code examples

Left to right

Python
print(10 - 4 - 3)
print(100 / 10 / 2)
print(2 * 3 * 4)
Output
3
5.0
24

Right to left for **

Python
print(2 ** 3 ** 2)
print((2 ** 3) ** 2)
Output
512
64

More examples

Chaining versus grouping

Python
a, b, c = 3, 2, 1
print(a > b > c)
print((a > b) > c)
print(1 < 3 > 2)
values = [5, 9, 12]
print(all(0 <= v <= 10 for v in values))
print(64 // 4 // 2, 64 // (4 // 2))
Output
True
False
True
False
8 32

Step-by-step explanation

  • a > b > c chains to 3 > 2 and 2 > 1, which is True.
  • (a > b) > c first produces True, then compares True > 1, which is 1 > 1, False.
  • 1 < 3 > 2 chains to 1 < 3 and 3 > 2, True, even though it looks odd.
  • 64 // 4 // 2 groups left to right (16 // 2 = 8); parentheses change it to 64 // 2 = 32.

Expected output

Running “Chaining versus grouping” prints exactly:

TextOutput
True
False
True
False
8 32

Practical applications

  • Formula translations into code must respect associativity for subtraction and division.
  • Range checks use chained comparisons such as 18 <= age <= 60.
  • Mathematical libraries document the associativity of custom operators.

Best practices

  • Use parentheses for repeated -, / or ** to avoid ambiguity.
  • Use chained comparisons only for simple ranges.
  • Avoid confusing chains such as a < b > c.

Common mistakes

Assuming ** groups left to right

Avoid

2 ** 3 ** 2 # expecting 64

Prefer

(2 ** 3) ** 2 # 64

** is right-associative, so the unparenthesised form gives 512.

Treating chained comparisons as grouped

Avoid

(x > y) > z # compares a Boolean with z

Prefer

x > y > z

Chained comparisons compare each pair; grouping compares True/False with a number.

Key takeaways

  • Associativity decides grouping for operators of equal precedence.
  • Most operators are left-associative; ** is right-associative.
  • Comparisons chain: a < b < c means a < b and b < c.

Try it yourself

  1. 1
    Predict and check 80 / 4 / 2 and 80 / (4 / 2).
  2. 2
    Write a chained comparison that checks a temperature is above 20 and below 30.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic