So far, you’ve learned how to write Python scripts that take input and display output. Now it’s time to make your programs smart — capable of making decisions.
In this post, we’ll cover:
- Conditional logic using
if,elif, andelse - Comparison and logical operators
- Real examples and AI tools to help you practice and test logic
🔁 What Is Control Flow?
Control flow allows your program to make decisions and run different code depending on certain conditions. Python uses:
if— checks a conditionelif— checks another condition if the first is falseelse— runs if none of the above are true
🧪 Example 1: Simple Conditional
age = int(input("Enter your age: "))
if age >= 18:
print("You're an adult!")
else:
print("You're a minor.")
Try changing the age to test different outcomes!
🧠 AI Tip: Let ChatGPT Quiz You on Conditionals
Ask ChatGPT:
“Can you give me 5 beginner challenges using if/else logic in Python?”
It might respond with challenges like:
- Check if a number is even or odd
- Grade a test score
- Recommend weather attire based on temperature
🔍 Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
== | Equals | x == 5 |
!= | Not equal | x != 3 |
> | Greater than | x > 10 |
< | Less than | x < 7 |
>= | Greater or equal | x >= 18 |
<= | Less or equal | x <= 100 |
🔗 Logical Operators
Use these to combine multiple conditions:
and— all conditions must be trueor— at least one must be truenot— inverts the condition
temperature = 75
is_raining = False
if temperature > 70 and not is_raining:
print("It's a great day for a walk!")
🤖 Use AI to Analyze Logic
Try this in ChatGPT:
“Explain this code and how the condition works:
if score >= 90 and attendance >= 80:”
You’ll get a step-by-step breakdown of how the logic is evaluated.
🎯 Practice Challenge
Write a program that:
- Asks for a user’s exam score (0–100)
- Prints:
"A"if score ≥ 90"B"if score ≥ 80"C"if score ≥ 70"D"if score ≥ 60"F"otherwise
Bonus: Ask ChatGPT to help you add input validation or suggest a different grading scale!
🛠️ Tools to Reinforce Learning
- 🔗 Real Python: If Statements
- 🔗 W3Schools: Python Conditions
- 🔗 ChatGPT: Turn your logic into quizzes or request logic breakdowns
🏁 Coming Up in Part 4…
In the next post, we’ll dive into loops — the key to repeating actions and building simple games. You’ll learn about forand while loops, and we’ll use AI to generate custom loop challenges.
Bonus preview: We’ll create a simple “number guessing game”!