If, Elif and Else
Week 1 — Python Basics Revision
Introduction
Programs need to make decisions. Should the ATM dispense cash or show an error? Should the student pass or fail? Conditional statements let your code choose different paths based on conditions.
Comparison Operators
Before writing conditionals, you need to know how to compare values. These operators return True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal | 5 != 3 | True |
| > | Greater than | 10 > 5 | True |
| < | Less than | 3 < 2 | False |
| >= | Greater or equal | 5 >= 5 | True |
| <= | Less or equal | 4 <= 3 | False |
The if Statement
The if statement checks a condition. If it is True, the indented code runs. If it is False, it is skipped entirely.
age = 18
if age >= 18:
print("You are an adult.")
You are an adult.
if-else
The else block runs when the condition is False. This gives your program two possible paths.
temperature = 35
if temperature > 30:
print("It is hot outside.")
else:
print("The weather is pleasant.")
It is hot outside.
if-elif-else
When you have more than two possibilities, use elif (short for "else if"). Conditions are checked from top to bottom. The first one that is True wins, and only that block runs.
score = 75
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(f"Your grade: {grade}")
Your grade: C
Real-World Example: ATM Withdrawal
Here is a practical example that combines user input with conditional logic to simulate an ATM withdrawal.
balance = 5000
amount = float(input("Withdraw amount: $"))
if amount <= 0:
print("Enter a valid amount.")
elif amount > balance:
print("Insufficient funds.")
else:
balance -= amount
print(f"Success! New balance: ${balance:.2f}")
How This Connects to OOP
Inside OOP classes, methods use if-else constantly. A BankAccount.withdraw() method checks if the balance is sufficient before deducting. Conditionals are the logic inside every class method.
Best Practices
- Always handle the
elsecase — account for unexpected values - Keep conditions simple and readable
- Use
eliffor multiple conditions instead of nested ifs - Test edge cases such as zero, negative numbers, and boundary values
Common Mistakes
- Using
=instead of==for comparison — single equals is assignment, double equals is comparison - Forgetting the colon
:afterif,elif, orelse - Wrong indentation — Python uses indentation to define blocks, not braces
- Overlapping conditions that make some branches unreachable
Practice Exercises
- Ask the user for a number. Print whether it is positive, negative, or zero.
- Write a grading system: take a score (0-100) and print the letter grade (A, B, C, D, or F).
- Ask for an age and print the ticket price: child (under 12) = $5, adult (12-64) = $10, senior (65+) = $7.
Summary
You can now make your programs think and decide. Next, we will learn about logical operators to combine multiple conditions.