Lesson 7: Conditional Statements
·Programming

Lesson 7: Conditional Statements

Give your programs the power to make decisions using if, elif, and else statements.

Lesson 7: Conditional Statements

Until now, our programs have been like a straight train track—they go from start to finish without any choice. Conditional Statements allow us to add "forks in the road." They let our code make decisions based on whether a condition is True or False.

Lesson Overview

In this lesson, we will cover:

  • The if Statement: The basic decision-maker.
  • The else Statement: The fallback plan.
  • The elif Statement: Handling multiple choices.
  • Indentation: Python’s most important rule.
  • Nested Conditionals: Decisions inside decisions.

1. The Basic if Statement

The if statement checks a condition. If it’s true, the code inside runs.

age = 19

if age >= 18:
    print("You are an adult.")

Crucial Note: Notice the colon : at the end of the if line and the Indent (empty space) before the print line. This indentation is how Python knows which code belongs to the if statement.


2. Adding a Fallback with else

What if the condition is false? We use else.

age = 16

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

3. Multiple Choices with elif

If you have more than two options, use elif (short for "else if").

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Grade: F")

How it works: Python checks from top to bottom. As soon as it finds a true condition, it runs that block and skips the rest.


4. Logical Operators in Conditionals

You can combine multiple criteria using and and or.

temperature = 25
is_sunny = True

if temperature > 20 and is_sunny:
    print("Perfect day for a walk!")

5. Decisions Inside Decisions (Nested if)

Sometimes, one decision depends on another.

choice = "pizza"
topping = "pineapple"

if choice == "pizza":
    print("Italian food selected.")
    if topping == "pineapple":
        print("Wait... are you sure about that?")
    else:
        print("Classic choice!")

Practice Exercise: The Adventure Game

Create a file named adventure.py. Write a mini "Choose Your Own Adventure" game:

  1. Print a starting scenario (e.g., "You are in a dark room. There are two doors, left and right.")
  2. Ask the user for their choice using input().
  3. If they choose "left", print a outcome.
  4. If they choose "right", print a different outcome.
  5. If they type anything else, print: "Invalid choice, you trip and fall!"

Quick Knowledge Check

  1. What symbol must follow an if statement?
  2. Why is indentation important in Python?
  3. When does the code inside an else block run?
  4. What does elif stand for?

Key Takeaways

  • if statements allow for branching logic.
  • else provides a default action when no conditions are met.
  • elif handles multiple specific scenarios.
  • Correct indentation is required for code blocks to work.

What’s Next?

We can now make programs that choose between paths! But what if we want to do something over and over again? In Lesson 8, we’ll learn about Loops—the key to automation!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn