Book Free Trial

Curriculum

Nested Conditions

Subject: ProgrammingCourse: Coding In PythonAges: Primary, Junior

Theory

Inside an if/elif/else block you can use any Python statements—including another if. This creates a nested conditional: a decision within a decision. Nested blocks are indented further (commonly by 4 more spaces; e.g., 8 spaces total).

Motivating example: point quadrant

Given nonzero numbers x and y, determine the quadrant of the point (x, y) on the Cartesian plane.

x = int(input())
y = int(input())

if x > 0: if y > 0: # x > 0, y > 0 print("First quadrant") else: # x > 0, y < 0 print("Fourth quadrant") else: if y > 0: # x < 0, y > 0 print("Second quadrant") else: # x < 0, y < 0 print("Third quadrant")

Note: A comment starts with # and continues to the end of the line. Python ignores comments.

Why (and when) to nest

  • Use nesting when the second decision depends on the first being true (or false).
  • Keep nesting shallow—deep nesting is harder to read. Often you can flatten with elif or combine conditions.

Flattening nested conditionals

Same logic, but clearer with elif and combined conditions:

x = int(input())
y = int(input())

if x > 0 and y > 0: print("First quadrant") elif x < 0 and y > 0: print("Second quadrant") elif x < 0 and y < 0: print("Third quadrant") else: # x > 0 and y < 0 print("Fourth quadrant")

Guard clauses (early exits)

Handle exceptional or edge cases first, then proceed. This reduces nesting.

x = int(input())
y = int(input())

Guard: ensure neither coordinate is zero (outside quadrant definition)

if x == 0 or y == 0: print("Point lies on an axis, no quadrant") else: if x > 0 and y > 0: print("First quadrant") elif x < 0 and y > 0: print("Second quadrant") elif x < 0 and y < 0: print("Third quadrant") else: print("Fourth quadrant")

Indentation rules recap

  • Each nested block increases indentation (recommended: 4 spaces per level).
  • All lines in the same block must align exactly.
  • Do not mix tabs and spaces (configure your editor to insert spaces on Tab).

Alternative patterns

<h3 style="margin:12px 0 6px; font-size:1.05rem;">1) Mapping from signs to labels</h3>
<p>Compute the sign pair once, then look up the result. Great for avoiding branching repetition.</p>
<pre style="background:#f6f8fa; padding:12px; border-radius:8px; overflow:auto;"><code class="language-python">x = int(input())

y = int(input())

if x == 0 or y == 0: print("On axis") else: sign = (1 if x > 0 else -1, 1 if y > 0 else -1) quadrant = { ( 1, 1): "First quadrant", (-1, 1): "Second quadrant", (-1, -1): "Third quadrant", ( 1, -1): "Fourth quadrant", }[sign] print(quadrant)

<h3 style="margin:12px 0 6px; font-size:1.05rem;">2) Conditional expression for tiny branches</h3>
<p>Prefer only for very short alternatives—don’t cram complex logic into one line.</p>
<pre style="background:#f6f8fa; padding:12px; border-radius:8px; overflow:auto;"><code class="language-python">n = int(input())

parity = "even" if n % 2 == 0 else "odd" print(parity)

Common mistakes

  • Missing colons after if/elif/else.
  • Inconsistent indentation (e.g., 4 spaces on one line, 2 on the next).
  • Over-nesting when elif or combined conditions would be clearer.
  • Unreachable branches (e.g., an elif that can never be true given previous checks).

Quick practice

  1. Read an integer temperature t. Print cold if t < 0, warm if 0 ≤ t < 25, else hot. (Start with nested ifs, then flatten to elif.)
  2. Given three integers, print the median (the middle value). Try a nested approach first.
  3. Given x and y, print axis if the point lies on an axis, else the quadrant name.
Show sample solutions
# 1) Temperature with elif
t = int(input())
if t < 0:
    print("cold")
elif t < 25:
    print("warm")
else:
    print("hot")

2) Median of three (nested approach)



a = int(input()); b = int(input()); c = int(input())
if a <= b:
if b <= c:
print(b)       # a <= b <= c
elif a <= c:
print(c)       # a <= c < b
else:
print(a)       # c < a < b
else:
if a <= c:
print(a)       # b <= a <= c
elif b <= c:
print(c)       # b <= c < a
else:
print(b)       # c < b < a



3) Axis vs quadrant

x = int(input()); y = int(input()) if x == 0 or y == 0: print("axis") elif x > 0 and y > 0: print("First quadrant") elif x < 0 and y > 0: print("Second quadrant") elif x < 0 and y < 0: print("Third quadrant") else: print("Fourth quadrant")

Key takeaways: Nest only when the second decision truly depends on the first; prefer elif or guard clauses to keep code readable; and always maintain consistent 4‑space indentation per level.