Learning Objectives – after this chapter you will be able to:
  • Write if, if-else and if-elif-else statements.
  • Use nested conditions and logical operators in conditions.
  • Write conditional (ternary) expressions.
  • Understand truthy/falsy values and the match statement.
Decision Making

Decision-making statements let a program choose which block of code to run depending on whether a condition is True or False. The block under each condition is defined by indentation, and every header line ends with a colon :.

1. if statement
if condition: statements
temperature = 38
if temperature > 37.5:
    print("Fever detected")
print("Check complete")
Output
Fever detected
Check complete
2. if-else statement
number = 7
if number % 2 == 0:
    print(number, "is even")
else:
    print(number, "is odd")
Output
7 is odd
3. if-elif-else ladder

Conditions are checked from top to bottom; only the first true block runs. else is optional.

marks = 72
if marks >= 90:
    grade = "A+"
elif marks >= 75:
    grade = "A"
elif marks >= 60:
    grade = "B"
elif marks >= 40:
    grade = "C"
else:
    grade = "Fail"
print("Grade:", grade)
Output
Grade: B
4. Nested if
age, has_licence = 20, False
if age >= 18:
    if has_licence:
        print("You can drive")
    else:
        print("Apply for a licence first")
else:
    print("Too young to drive")
Output
Apply for a licence first
5. Combining conditions
year = 2024
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")
Output
2024 is a leap year
Conditional (Ternary) Expression

A one-line form that chooses between two values: value_if_true if condition else value_if_false.

a, b = 14, 9
larger = a if a > b else b
print("Larger:", larger)
print("Pass" if 35 >= 40 else "Fail")
Output
Larger: 14
Fail
Truthy and Falsy Values

A condition does not have to be a comparison. Any object can be tested: falsy values are False, None, 0, 0.0, "", [], (), {} and set(); everything else is truthy.

cart = []
if cart:
    print("Items in cart:", len(cart))
else:
    print("Your cart is empty")

name = "Meera"
if name:
    print("Welcome,", name)
Output
Your cart is empty
Welcome, Meera
Use == (comparison) in conditions, not = (assignment). if x = 5: is a SyntaxError in Python.
The match Statement (Python 3.10+)

match compares a value against several patterns – similar to switch-case in other languages. case _ is the default (wildcard), and | combines alternatives.

def day_type(day):
    match day:
        case "Saturday" | "Sunday":
            return "Weekend"
        case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
            return "Weekday"
        case _:
            return "Invalid day"

print(day_type("Sunday"))
print(day_type("Monday"))
print(day_type("Funday"))
Output
Weekend
Weekday
Invalid day
Key Takeaways
  • if / elif / else run the first block whose condition is true; headers end with a colon.
  • x if condition else y picks a value in one line.
  • Empty collections, zero and None are falsy; match (3.10+) handles multi-way choices.
Video : Decision Making
Question / Answer (15 questions)

Answer : They are statements that execute different blocks of code depending on whether a condition is True or False. In Python they are if, if-else, if-elif-else, nested if and the match statement.

Answer : if condition: (indented statements run when the condition is True) else: (indented statements run when it is False). Each header line ends with a colon and the blocks are defined by indentation.

Answer : Conditions are tested from top to bottom. The block of the first condition that is True is executed and the rest are skipped. If none is True, the else block (if present) runs.

Answer : A nested if is an if statement placed inside another if or else block. It is used when a second decision depends on the result of the first.

Answer : (year % 4 == 0 and year % 100 != 0) or year % 400 == 0. A year divisible by 4 is a leap year unless it is divisible by 100, except that years divisible by 400 are leap years.

Answer : It chooses between two values in one expression: value_if_true if condition else value_if_false. Example: larger = a if a > b else b.

Answer : Falsy values are treated as False in a condition: False, None, 0, 0.0, empty string and empty collections. All other values are truthy and are treated as True.

Answer : = is assignment, which is a statement and not allowed as a condition. Python raises a SyntaxError. Use == to compare: if x == 5:.

Answer : No. else is optional; an if (or if-elif chain) can exist without it, in which case nothing happens when all conditions are False.

Answer : match is the structural pattern-matching statement introduced in Python 3.10. It compares a subject value against case patterns and runs the first matching block, similar to switch-case in other languages.

Answer : _ is the wildcard pattern. It matches any value and is used as the default case when no other pattern matches.

Answer : Combine patterns with |, for example case "Saturday" | "Sunday": matches either string.

Answer : pass is a placeholder that does nothing. It is used when a block is syntactically required but no action is needed yet.

Answer : and, or and not combine or invert conditions so that a single if can test several requirements, for example if age >= 18 and has_id:.

Answer : if a >= b and a >= c: largest = a; elif b >= c: largest = b; else: largest = c. Alternatively use the built-in max(a, b, c).
MCQ Test : Check Your Understanding (5 questions)

Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.

Q1. Which keyword checks another condition when the previous if condition is False?

Show Answer

Correct Answer : B. elif
Python uses elif.

Q2. What is printed by: x = 0; print("Yes" if x else "No")?

Show Answer

Correct Answer : C. No
0 is falsy, so the else value is chosen.

Q3. Which of these values is TRUTHY?

Show Answer

Correct Answer : A. "False"
"False" is a non-empty string, so it is truthy.

Q4. In which Python version was the match statement introduced?

Show Answer

Correct Answer : D. 3.10
Structural pattern matching arrived in Python 3.10.

Q5. marks = 72. In an if-elif ladder testing >= 90, >= 75, >= 60, >= 40 in order, which block runs?

Show Answer

Correct Answer : C. >= 60
72 >= 60 is the first true condition; later conditions are skipped.

Bibliography/References

The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.

  1. Python Software Foundation. (n.d.). More Control Flow Tools. In The Python Tutorial. https://docs.python.org/3/tutorial/controlflow.html
  2. Python Software Foundation. (n.d.). Compound Statements. In The Python Language Reference. https://docs.python.org/3/reference/compound_stmts.html
  3. Bucher, B., & van Rossum, G. (2020). PEP 634 – Structural Pattern Matching: Specification. Python Enhancement Proposals. https://peps.python.org/pep-0634/
  4. Kohn, D. F. (2020). PEP 636 – Structural Pattern Matching: Tutorial. Python Enhancement Proposals. https://peps.python.org/pep-0636/
  5. Python Software Foundation. (2021). What’s New In Python 3.10 (structural pattern matching). https://docs.python.org/3/whatsnew/3.10.html
  6. Python Software Foundation. (n.d.). Built-in Types. The Python Standard Library. https://docs.python.org/3/library/stdtypes.html
  7. Lutz, M. (2013). Learning Python (5th ed.). O’Reilly Media.