Sharing notes from my ongoing learning journey — what I build, break and understand along the way.
Python Basics Part 3: If, Elif, Else Conditional Statements Explained
Python Basics – Part 3: Conditional Statements (if, elif, else)
In this part of my 5-week Python training notes, we’ll look at conditional statements – how to make decisions in your code using if, elif, and else.
These are part of Python’s control structures, specifically the group of branching (decision) structures.
What Are Conditional Statements?
Conditional statements allow your program to:
- Check a condition (e.g.
x > 10) - Decide which block of code to execute depending on whether the condition is
TrueorFalse
The main keywords are:
ifelif(“else if”)else
Basic if Statement
Rule:
If the condition after if is True, the indented block of code is executed.
If it’s False, the block is skipped.
number = 23
if number == 23:
print("Great, number is", number)
Output:
Great, number is 23
You can also have an if that does nothing for now using pass:
number = 42
if number == 42:
pass # do nothing (placeholder for future code)
else:
print("Number is not 42")
Output:
Number is not 42
pass is useful when you plan to implement logic later, but need a syntactically valid block.
if – else
if and else allow you to choose between two paths:
- If the condition is
True→ run theifblock - Otherwise (
False) → run theelseblock
weather = "Cloudy"
# weather = "Rain"
if weather == "Rain":
print("It is raining")
else:
print("It is not raining")
Possible Outputs:
- If
weather = "Rain"→It is raining - Otherwise →
It is not raining
if – elif
elif means “else if” and lets you check another condition if the previous one was False.
weather2 = "Sun"
if weather2 == "Sun":
print("Yeah, the sun is shining!")
elif weather2 == "Rain":
print("It is raining!")
Output when weather2 = "Sun":
Yeah, the sun is shining!
We can make this example more complete by adding an else:
weather2 = "Cloudy"
if weather2 == "Sun":
print("Yeah, the sun is shining!")
elif weather2 == "Rain":
print("It is raining!")
else:
print("Maybe it’s cloudy or something else.")
Output:
Maybe it’s cloudy or something else.
Important:
In an if / elif / else chain, only the first True condition is executed.
The rest are skipped.
if – elif – else with Multiple Conditions
Here’s an example with several possible values:
A week day represented by a number.
weekday = 3 # 1 = Monday, 2 = Tuesday, 3 = Wednesday, ...
print(weekday)
if weekday == 1:
print("Monday")
elif weekday == 2:
print("Tuesday")
elif weekday == 3:
print("Wednesday")
elif weekday == 4:
print("Thursday")
elif weekday == 5:
print("Friday")
elif weekday == 6:
print("Saturday")
elif weekday == 7:
print("Sunday")
else:
print("Error, there are only weekdays 1 to 7!")
Output (with weekday = 3):
3
Wednesday
Again: Exactly one block is executed in this chain.
Bonus: Cleaner Alternative with a Dictionary
The same weekday logic can be written more compactly using a dictionary and the get() method:
weekday = 3 # try changing this value
days = {
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
7: "Sunday"
}
print(days.get(weekday, "Error: invalid day!"))
Output (with weekday = 3):
Wednesday
This version is easier to maintain when there are many possible values.
Short Form: Conditional Expression (Ternary Operator)
Python also has a one-line conditional expression:
temperature = 25
print("Warm" if temperature > 20 else "Cold")
Output:
Warm
General pattern:
value_if_true if condition else value_if_false
This is helpful for simple, compact decisions.
Summary
In this lesson, we covered:
iffor running code only when a condition isTrueif/elsefor choosing between two pathsif/elif/elsefor multiple conditions- That only one branch in an
if–elifchain is executed - Using
passas a placeholder in empty blocks - A dictionary-based alternative for many
elifcases - The conditional expression (
a if condition else b) for short, inline decisions
Next up, we’ll move on to loops (for, while) and control statements like break and continue.
