Cascading Conditional Statements in Python

Theory

Cascading Conditional Statements in Python

Sometimes you need to check several different conditions in sequence and execute only one matching branch. Python allows this with the if ... elif ... else structure—often called cascading conditionals.

Example: determining the quadrant

We can rewrite the nested quadrant example using a cascade:

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

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

How it works

  • Conditions are checked from top to bottom.
  • The first true condition’s block runs, and the rest are skipped.
  • If none are true, the else block runs (if present).
  • You can have as many elif branches as needed, but only one if and at most one else.

When to use cascades instead of nesting

  • When all conditions are mutually exclusive—only one can be true.
  • To improve readability compared to deep nested ifs.
  • When checking multiple possibilities of the same variable(s).

Example: grading system

score = int(input("Enter score: "))

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

Only one of these branches will execute, even if multiple conditions could be true.

Tips for using if/elif/else

  • Order matters: put more specific conditions before more general ones.
  • If two conditions can be true at the same time but you want both to execute, use separate if statements instead of elif.
  • Keep conditions simple for readability; break complex checks into variables.

Quick practice

  1. Classify a temperature as cold (< 0), cool (0–15), warm (16–25), or hot (> 25).
  2. Given an integer 1–7, print the corresponding weekday name.
  3. Given a test score 0–100, assign a letter grade using an if/elif/else cascade.
Show sample solutions
# 1) Temperature classification
t = int(input())
if t < 0:
    print("cold")
elif t <= 15:
    print("cool")
elif t <= 25:
    print("warm")
else:
    print("hot")

# 2) Weekday
day = int(input())
if day == 1:
    print("Monday")
elif day == 2:
    print("Tuesday")
elif day == 3:
    print("Wednesday")
elif day == 4:
    print("Thursday")
elif day == 5:
    print("Friday")
elif day == 6:
    print("Saturday")
else:
    print("Sunday")

# 3) Grade
score = int(input())
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
elif score >= 60:
    print("D")
else:
    print("F")

Key takeaways: if/elif/else lets you test conditions in sequence and run exactly one matching block. It’s clearer than nested if statements when dealing with mutually exclusive cases.

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