- Write
if,if-elseandif-elif-elsestatements. - Use nested conditions and logical operators in conditions.
- Write conditional (ternary) expressions.
- Understand truthy/falsy values and the
matchstatement.
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: statementstemperature = 38
if temperature > 37.5:
print("Fever detected")
print("Check complete")Fever detected Check complete
2. if-else statement
number = 7
if number % 2 == 0:
print(number, "is even")
else:
print(number, "is odd")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)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")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")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")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)Your cart is empty Welcome, Meera
== (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"))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 ypicks 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)
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.
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.
- Python Software Foundation. (n.d.). More Control Flow Tools. In The Python Tutorial. https://docs.python.org/3/tutorial/controlflow.html
- Python Software Foundation. (n.d.). Compound Statements. In The Python Language Reference. https://docs.python.org/3/reference/compound_stmts.html
- Bucher, B., & van Rossum, G. (2020). PEP 634 – Structural Pattern Matching: Specification. Python Enhancement Proposals. https://peps.python.org/pep-0634/
- Kohn, D. F. (2020). PEP 636 – Structural Pattern Matching: Tutorial. Python Enhancement Proposals. https://peps.python.org/pep-0636/
- Python Software Foundation. (2021). What’s New In Python 3.10 (structural pattern matching). https://docs.python.org/3/whatsnew/3.10.html
- Python Software Foundation. (n.d.). Built-in Types. The Python Standard Library. https://docs.python.org/3/library/stdtypes.html
- Lutz, M. (2013). Learning Python (5th ed.). O’Reilly Media.