Day 4 45 min beginner

Control Flow - If Statements

Learn how to make decisions in your code using conditional statements

Learning Objectives

  • Understand the Boolean logic behind decisions
  • Write if, elif, and else statements
  • Use comparison and logical operators in conditions
  • Understand indentation and block structure in Python

Making Decisions

In the previous lessons, our code ran from top to bottom, executing every line. Real-world programs need to make decisions: “If the user is logged in, show their dashboard; otherwise, show the login screen.”

In Python, we use the if statement to control the flow of execution.

The if Statement

The simplest form of a conditional is the if statement.

age_check.py
1
2
3
4
age = 18

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

How it works:

  1. The condition age >= 18 is evaluated.
  2. If it is True, the indented block of code below it is executed.
  3. If it is False, the indented block is skipped.
Indentation Matters!
Unlike many other languages that use curly braces {}, Python uses indentation (usually 4 spaces) to define blocks of code. All lines indented at the same level are part of the same block.

Adding else

What if you want to do something else when the condition is False? Use the else statement.

else_example.py
1
2
3
4
5
6
age = 15

if age >= 18:
    print("You can vote.")
else:
    print("You are too young to vote.")

Multiple Conditions with elif

When you have more than two possibilities, use elif (short for “else if”).

grades.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
score = 85

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

Python evaluates these from top to bottom. As soon as one condition is True, its block runs, and the rest are skipped.

Logical Operators

You can combine multiple conditions using logical operators:

  • and: True if both conditions are True.
  • or: True if at least one condition is True.
  • not: Inverts the result (True becomes False).
logic.py
1
2
3
4
5
has_ticket = True
is_vip = False

if has_ticket or is_vip:
    print("Welcome to the show!")

Nested If Statements

You can place an if statement inside another if statement.

nested.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
is_weekend = True
is_sunny = True

if is_weekend:
    if is_sunny:
        print("Go to the beach!")
    else:
        print("Watch a movie at home.")
else:
    print("Go to work.")

Interactive Practice

Try writing a program that checks if a number is positive, negative, or zero.

flowchart TD Start([Start]) --> Input[Get Number] Input --> IsPos{Number > 0?} IsPos -- Yes --> Pos[Print Positive] IsPos -- No --> IsNeg{Number < 0?} IsNeg -- Yes --> Neg[Print Negative] IsNeg -- No --> Zero[Print Zero] Pos --> End([End]) Neg --> End Zero --> End

Quiz

Complete this quiz with a minimum score of 80% to mark Day 4 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!