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 True or False

The main keywords are:

  • if
  • elif (“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.

ifelse

if and else allow you to choose between two paths:

  • If the condition is True → run the if block
  • Otherwise (False) → run the else block
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

ifelif

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.

ifelifelse 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:

  • if for running code only when a condition is True
  • if / else for choosing between two paths
  • if / elif / else for multiple conditions
  • That only one branch in an ifelif chain is executed
  • Using pass as a placeholder in empty blocks
  • A dictionary-based alternative for many elif cases
  • 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.

Leave a Reply

Your email address will not be published. Required fields are marked *