Unit 3: Control Flow – Decision Making in Python

Overview

In real life, we make decisions constantly:

       •      If it’s raining, we bring an umbrella.

       •      If we’re hungry, we eat.

Python lets you do the same thing in your code using control flow tools like if, elif, and else. These help your program make decisions and choose different paths depending on the data it sees.

1. Conditional Statements: if, elif, else

What is a condition?

A condition is something that evaluates to True or False. Python uses these conditions to decide which block of code to run.

The if Statement

Think of if as a traffic signal:

       •      If the light is green, go.

       •      If not, do nothing.

Syntax:

if condition:

    # do something

Example:

temperature = 35

if temperature > 30:

    print(“It’s a hot day!”)

Explanation:

       •      Python checks if temperature > 30. Since it’s True, it prints the message.

       •      If it were 25, the condition would be False, and nothing would print.

The else Statement

else is like the “default” choice.

“If the condition is not true, do this instead.”

Example:

temperature = 20

if temperature > 30:

    print(“It’s a hot day!”)

else:

    print(“It’s not that hot.”)

Output:

It’s not that hot.

The elif Statement

What if you have multiple conditions to check?

That’s where elif (short for “else if”) comes in.

Analogy:

Think of getting dressed:

       •      If it’s cold → wear a coat.

       •      Else if it’s raining → bring an umbrella.

       •      Else → wear regular clothes.

Example:

temperature = 15

if temperature > 30:

    print(“It’s a hot day!”)

elif temperature > 20:

    print(“It’s a nice day!”)

else:

    print(“It’s cold!”)

Output:

It’s cold!

How it works:

       •      Python checks the first condition: temperature > 30 → False.

       •      Then checks temperature > 20 → False.

       •      Goes to else and prints “It’s cold!”

2. Boolean Expressions: Comparing Values

What is a Boolean?

A Boolean is a value that can be True or False.

Comparison Operators

Operator       Meaning Example

==  Equal to 5 == 5 → True

!=    Not equal to 3 != 4 → True

>     Greater than 7 > 4 → True

<     Less than       2 < 9 → True

>=  Greater than or equal to    5 >= 5 → True

<=  Less than or equal to  6 <= 8 → True

Example:

score = 85

if score >= 90:

    print(“Grade: A”)

elif score >= 80:

    print(“Grade: B”)

else:

    print(“Grade: C or lower”)

Output:

Grade: B

3. Combining Conditions: and, or, not

You can combine multiple conditions using logical operators:

Logical Operators

Operator       Description   Example

and Both conditions must be True   x > 5 and x < 10

or    At least one condition must be True       x < 5 or x > 10

not  Reverses a condition   not True → False

Example with and:

age = 25

if age > 18 and age < 65:

    print(“You are an adult.”)

Output:

You are an adult.

Explanation:

Both conditions (age > 18 and age < 65) are True, so the message is printed.

Example with or:

day = “Saturday”

if day == “Saturday” or day == “Sunday”:

    print(“It’s the weekend!”)

Output:

It’s the weekend!

Example with not:

is_tired = False

if not is_tired:

    print(“Let’s get to work!”)

Output:

Let’s get to work!

4. Real-Life Example: Activity Planner

Let’s put everything together into a practical program.

temperature = 22

is_raining = False

if temperature > 25 and not is_raining:

    print(“Go to the beach!”)

elif temperature > 15 and not is_raining:

    print(“Go for a walk.”)

elif is_raining:

    print(“Stay indoors and read a book.”)

else:

    print(“Wear a jacket and go outside.”)

Output:

Go for a walk.

Conclusion

In this unit, you learned how to control the flow of your Python programs with decision-making tools:

       •      if, elif, else to create branching paths in your code

       •      Boolean logic (True, False) to test conditions

       •      Logical operators (and, or, not) to combine conditions

These tools are the foundation of all smart programs—from calculators to games to AI decision engines. Next up, we’ll explore loops, which help you repeat tasks automatically.

Ready for Unit 4: Loops – Repeating Tasks? Let’s keep going!

Leave a Comment