Welcome back! In Module 1, we learned how to store data and talk to the user. But right now, our programs are a bit… well, dumb. They just follow instructions from top to bottom, every single time.
In Module 2, we are going to teach the computer how to make decisions. This is called Control Flow, and it’s what makes software feel “smart.”
1. The Yes/No Language: Booleans
Before a computer can decide anything, it needs to know if something is True or False.
In Python, this data type is called a Boolean (named after mathematician George Boole).
Think of a light switch:
- It is either ON (True)
- Or it is OFF (False)
- There is no “maybe.”
Python
is_raining = True
is_sunny = False
2. Comparison Operators: The Logic Tools
How do we produce those True/False answers? We compare things!
Here are the symbols (Operators) you need to know.
| Symbol | Meaning | Example | Result |
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 10 > 2 | True |
< | Less than | 2 < 5 | True |
>= | Greater or Equal | 5 >= 5 | True |
⚠️ Common Mistake Alert:
=(Single equals) is for assigning a value (putting things in the box).==(Double equals) is for comparing values (asking “are these the same?”).
3. The if Statement
This is the core of decision-making. We use the keyword if to tell Python: “Only run this code IF the condition is True.”
Shutterstock
The Syntax (Watch Your Indentation!)
Python is unique because it uses indentation (spacing) to group code.
Python
age = 20
if age >= 18:
# This line is indented (4 spaces or 1 tab)
# It ONLY runs if age is 18 or older
print("You are an adult.")
print("This line runs no matter what.")
If you forget the indentation, Python will give you an error!
4. else and elif: Handling Alternatives
What if the condition is False?
else: “If the first condition failed, do this instead.”elif(Else If): “If the first one failed, check this specific condition next.”
Example: The Weather App
Python
weather = "rainy"
if weather == "sunny":
print("Wear sunglasses.")
elif weather == "rainy":
print("Take an umbrella.")
else:
print("Stay inside, I don't know what the weather is!")
🎓 Module 2 Final Project: “The Virtual Bouncer” 🕵️♂️
Let’s combine inputs (from Module 1) with logic (Module 2). You are programming a security gate for a club.
The Challenge:
- Ask the user for their age.
- Convert that input into a number (integer).
- If they are under 18: Print “Sorry, you are too young to enter.”
- Else if (
elif) they are exactly 18: Print “Welcome to your first night out!” - Else: Print “Welcome in. Have fun!”
The Solution:
Try to write it yourself first!
Python
# 1. Get input and convert to integer age = input("How old are you? ") age = int(age)
2. The Logic Tree
if age < 18:
print(“Sorry, you are too young to enter.”)
elif age == 18:
print(“Welcome to your first night out!”)
else:
print(“Welcome in. Have fun!”)
https://igcomsystems.com/module-3-loop-de-loop-making-the-computer-do-the-boring-stuff/
