Nested Conditions

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

1) Mapping from signs to labels

Compute the sign pair once, then look up the result. Great for avoiding branching repetition.

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)

2) Conditional expression for tiny branches

Prefer only for very short alternatives—don’t cram complex logic into one line.

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.

Problems

FAQ

Find quick answers to common questions about our lessons, pricing, scheduling, and how Exact Science can help your child excel.
Where do you hold your classes?
We hold our classes online or on-site on Saturdays at our branch in Pimlico Academy, London.
You can find our timetable here.
What do you need to start learning online?
For lessons you only need a computer or phone with a microphone, camera and Internet access. Wherever you are - in London, Nottingham, New York or Bali - online lessons will be at hand.
When can I take the trial lesson?
You can get acquainted with the school at any time convenient for you. To do this, just leave a request and sign up for a lesson.
What should I expect from the trial lesson?
The trial lesson is a 30-minute online session designed to get a sense of how your child approaches mathematical thinking and problem solving. (In practice, it often runs a bit longer if the student is engaged!)

We typically explore a range of fun and challenging problems drawn from competitions. We adapt the difficulty based on how the student responds, aiming to make it both accessible and stimulating.

After the session, we’ll have a quick conversation with the parent to share observations and suggest a personalised path forward.
I can't attend class, what should I do?
It is OK, it happens! Students have the opportunity to cancel a lesson up to 8 hours before the scheduled time without loss of payment. So you can reschedule it for a convenient time, and the teacher will have the opportunity to
I don't have much free time, will I have time to study?
Learning can take place at your own pace. We will select a convenient schedule and at any time we will help you change the schedule, take a break or adjust the program.
How long is one lesson?
All classes last 1 hour.

Meet our team

Our teachers will tell you how to prepare for exams, help you cope with difficult tasks and win the Olympiad

They will tell you about the pitfalls of exams and the most common mistakes, and explain how to avoid them
George Ionitsa
Founder &
Maths and Coding Coach

What our students and parents say about learning with us

"Olympiad Maths Lessons helped me a lot to get the Gold medal in Junior Maths Challenge"
St. Paul's Student
"Thanks to the 'Data Science' and 'Coding in Python' lessons I got accepted to my dream university."
Michael
Data Science Student
Warwick University
"Great courses, which thoroughly explained topics beyond the capability of the GCSE answer sheet. Thanks so much."
Ivan
GCSE Student in Dubai
"Financial Mathematics! Best course to understand Python and Mathematics behind Finance!"
Gleb
VC Investor
"We got silver in PMC! Thanks George!"
Mum of St. Paul's Student
Prepare for the Primary Maths Challenge
"My daughter took a batch of 10 classes with George to understand Python with Turtle. I found George extremely knowledgeable and accessible."
Dad of Latymer School Student
Python with Turtle