Lesson 2.1

Introduction to if Statements

What are if statements?

  • Let your program make decisions
  • Run some code only if a condition is True
  • Example in everyday life:
    • If it is raining → take an umbrella
    • If it is not raining → don’t

Syntax of an if statement

if condition:
    # code block runs if condition is True

Example

x = 10
if x > 5:
    print("x is greater than 5")
  • Output: x is greater than 5

Importance of Indentation

  • In Python, indentation = structure
  • Indentation shows what code belongs to the if
  • Standard indentation = 4 spaces
  • No curly braces {} like other languages

Example with indentation

if True:
    print("This will run")
    print("So will this")
print("This is outside the if")

Common mistake

if True:
print("Oops, no indent!")   # ❌ error

Demo time

Try it yourself:

age = int(input("Enter your age: "))
if age >= 13:
    print("You are a teenager or older!")

Quick Challenge

Write code that:

  • Asks for a number
  • Prints "Even!" if divisible by 2
  • Otherwise does nothing (we’ll add else later)