Chapter 2: Control Flow / Lesson 11

If Statements

Making Decisions with If Statements

If statements allow your program to make decisions based on conditions. They enable your code to execute different blocks of code depending on whether certain conditions are true or false.

Python's if statements are straightforward and readable. The basic structure uses if, optionally followed by elif (else if) for additional conditions, and else for a default case.

Basic If Statement

The simplest form checks a single condition and executes code only if that condition is true:

if_statement.py
age = 18 if age >= 18: print("You can vote!") elif age >= 16: print("You can drive!") else: print("Too young")

If-Elif-Else Structure

You can chain multiple conditions using elif (short for "else if"). Python evaluates conditions from top to bottom and executes only the first block where the condition is true:

grades.py
score = 85 if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C" elif score >= 60: grade = "D" else: grade = "F" print(f"Grade: {grade}")

Nested If Statements

You can nest if statements inside other if statements to create more complex decision trees:

nested.py
age = 25 has_license = True if age >= 18: print("You are an adult") if has_license: print("You can drive!") else: print("Consider getting a license") else: print("You are a minor")

Comparison Operators

Comparison operators allow you to compare values and return boolean results (True or False):

  • == Equal to - checks if two values are the same
  • != Not equal to - checks if two values are different
  • > Greater than - checks if left value is larger
  • < Less than - checks if left value is smaller
  • >= Greater than or equal to
  • <= Less than or equal to
comparisons.py
print(5 == 5) # True print(5 != 3) # True print(10 > 5) # True print(3 < 7) # True print(5 >= 5) # True print(4 <= 6) # True

Truthiness in Python

Python has a concept of "truthiness" - values that evaluate to True or False in a boolean context. Understanding this helps you write cleaner conditionals:

💡 Truthy Values

Values that evaluate to True: non-zero numbers, non-empty strings/lists/dicts, True

💡 Falsy Values

Values that evaluate to False: 0, 0.0, "", [], {}, None, False

truthiness.py
name = "Alice" if name: # Checks if name is not empty print("Name is not empty") # Common pattern for checking if something exists items = [] if not items: print("List is empty")

Common Patterns

Here are some practical patterns you'll use frequently:

patterns.py
# Checking if a value is in a range temperature = 75 if 70 <= temperature <= 80: print("Perfect weather!") # Multiple conditions with 'and' age = 25 income = 50000 if age >= 18 and income >= 30000: print("Eligible for loan") # Multiple conditions with 'or' day = "Saturday" if day == "Saturday" or day == "Sunday": print("Weekend!")
🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...