Control Flow β
Learn how to control the flow of your Python programs using conditional statements and decision-making logic
π if, elif, else Statements β
Control flow allows your program to make decisions and execute different code based on conditions.
Think of it like this: Just like you make decisions in daily life ("If it's raining, take an umbrella"), Python can make decisions based on conditions.
Basic if Statement β
python
age = 18
if age >= 18:
print("You are an adult")
print("You can vote!")
# Expected output:
# You are an adult
# You can vote!if-else Statement β
python
age = 16
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
print("Wait a few more years!")
# Expected output:
# You are a minor
# Wait a few more years!if-elif-else Statement β
python
score = 85
if score >= 90:
print("Grade: A")
print("Excellent work!")
elif score >= 80:
print("Grade: B")
print("Good job!")
elif score >= 70:
print("Grade: C")
print("Keep improving!")
else:
print("Grade: F")
print("Study harder next time!")
# Expected output:
# Grade: B
# Good job!elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C") elif score >= 60: print("Grade: D") else: print("Grade: F")
## π’ **Comparison Operators**
```python
a = 10
b = 20
print(a == b) # False (equal to)
print(a != b) # True (not equal to)
print(a < b) # True (less than)
print(a > b) # False (greater than)
print(a <= b) # True (less than or equal to)
print(a >= b) # False (greater than or equal to)π Logical Operators β
python
age = 25
has_license = True
# AND operator
if age >= 18 and has_license:
print("Can drive")
# OR operator
if age < 18 or not has_license:
print("Cannot drive")
# NOT operator
if not has_license:
print("Need to get a license")β Truthy and Falsy Values β
Falsy Values β
python
# These evaluate to False
print(bool(0)) # False
print(bool("")) # False (empty string)
print(bool([])) # False (empty list)
print(bool({})) # False (empty dict)
print(bool(None)) # FalseTruthy Values β
python
# These evaluate to True
print(bool(1)) # True
print(bool("hello")) # True (non-empty string)
print(bool([1, 2, 3])) # True (non-empty list)
print(bool(-1)) # True (non-zero number)π― Nested Conditions β
python
weather = "sunny"
temperature = 75
if weather == "sunny":
if temperature > 70:
print("Perfect day for a picnic!")
else:
print("Sunny but a bit cold")
else:
print("Not a sunny day")π Practice Examples β
Example 1: Age Checker β
python
age = int(input("Enter your age: "))
if age < 0:
print("Invalid age")
elif age < 13:
print("You are a child")
elif age < 20:
print("You are a teenager")
elif age < 60:
print("You are an adult")
else:
print("You are a senior")Example 2: Login System β
python
username = "admin"
password = "secret"
user_input = input("Enter username: ")
pass_input = input("Enter password: ")
if user_input == username and pass_input == password:
print("Login successful!")
else:
print("Invalid credentials")Example 3: Number Classifier β
python
number = int(input("Enter a number: "))
if number > 0:
print("Positive number")
elif number < 0:
print("Negative number")
else:
print("Zero")
# Check if even or odd
if number % 2 == 0:
print("Even number")
else:
print("Odd number")π― Key Takeaways β
- Use
iffor single conditions - Use
eliffor multiple conditions - Use
elsefor default case - Combine conditions with
and,or,not - Empty values are generally falsy
- Non-empty values are generally truthy
- Proper indentation is crucial
Continue to: Loops β