Python Lesson: if, elif, else

1. What Are Conditional Statements?

Conditional statements allow your program to make decisions based on conditions that are either True or False.

2. The if Statement

age = 18

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

3. The else Statement

else runs when the if condition is False.

age = 16

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

4. The elif Statement

elif means "else if" and allows multiple conditions.

score = 85

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

5. Using Comparison Operators

x = 10

if x == 10:
    print("x is ten")
    

6. Using Logical Operators

age = 20
has_id = True

if age >= 18 and has_id:
    print("Entry allowed")
else:
    print("Entry denied")
    

7. Indentation Matters!

Python uses indentation to define blocks of code. Incorrect indentation will cause errors.

8. Common Mistakes

9. Ready for the Quiz?

Take the if / elif / else Quiz