AiTechWorlds
AiTechWorlds
Picture this: it's a Friday evening and you walk up to an ATM. You slide in your card and type your PIN. In that split second, something remarkable happens — the machine decides. If your PIN is correct, it welcomes you and shows your balance. If it's wrong, it flashes an error and locks you out after three attempts.
The ATM didn't flip a coin. It didn't call a human to ask. It followed a rule you can write in plain English:
"IF the PIN matches, show the balance. OTHERWISE, show an error."
That rule — that branching decision — is exactly what if-else does in Python. Every useful program you will ever write makes decisions. A login form, a weather app, a video game, a tax calculator — all of them branch based on conditions. Without if-else, your code would only ever walk in a straight line, unable to react to the real world.
Conditional logic means: execute certain code only when a specific condition is true.
Think about decisions you make every morning:
Your brain runs this check automatically. In Python, you write it out explicitly, and the computer follows your instructions to the letter.
Before we look at any code, there's one thing Python does differently from almost every other language: it uses indentation (spaces) to define blocks of code.
Most languages use curly braces {} to group code. Python says: the indented lines belong to the statement above them. This is not optional decoration — it is the syntax.
Why? Python's creator, Guido van Rossum, wanted code to be readable by humans. Forced indentation means every Python program looks tidy and consistent. Messy indentation is literally a syntax error.
The standard is 4 spaces per level.
if condition:
# runs when condition is True
elif another_condition:
# runs when first is False but this is True
else:
# runs when ALL above conditions are False
Text flowchart:
Start
|
v
[Check condition 1] --True--> [Run Block 1] --> End
|
False
|
v
[Check condition 2] --True--> [Run Block 2] --> End
|
False
|
v
[Run else Block] --> End
These operators compare two values and return True or False:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 7 > 3 | True |
< | Less than | 2 < 8 | True |
>= | Greater than or equal | 5 >= 5 | True |
<= | Less than or equal | 4 <= 6 | True |
Common mistake: Writing
=(assignment) instead of==(comparison). The expressionif score = 90:is a syntax error. Always use==when comparing.
Sometimes one condition isn't enough. You need to combine them.
| Operator | Meaning | Example | Result |
|---|---|---|---|
and | Both conditions must be True | True and False | False |
or | At least one must be True | True or False | True |
not | Flips True to False, vice versa | not True | False |
Truth table for and:
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
Truth table for or:
| A | B | A or B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Real-life use:
age = 20
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
else:
print("Entry denied")
Both conditions must pass — just like a real bouncer.
You can place an if-else inside another if-else. This is called nesting, and it handles more complex decision trees.
is_member = True
balance = 150
if is_member:
if balance >= 100:
print("Purchase approved — Member discount applied")
else:
print("Insufficient balance")
else:
print("Please sign up for membership")
Use nesting carefully — too many levels deep becomes hard to read. Often, and / or can replace shallow nesting.
Imagine you are a teacher who needs to convert a numeric score into a letter grade. You've been doing this by hand for 30 students. Let's let Python handle it.
score = int(input("Enter your score (0-100): "))
if score >= 90:
grade = "A"
message = "Excellent!"
elif score >= 80:
grade = "B"
message = "Good job!"
elif score >= 70:
grade = "C"
message = "Satisfactory"
elif score >= 60:
grade = "D"
message = "You passed"
else:
grade = "F"
message = "Please try again"
print(f"Grade: {grade} — {message}")
What each line does:
score = int(input(...)) — asks the user to type a number; input() returns text, so int() converts it to a whole numberif / elif checks a range — Python checks them in order, top to bottom, and stops at the first true onegrade and message are assigned based on which branch runsprint uses an f-string to display both variables in one lineSample runs:
| Input | Output |
|---|---|
95 | Grade: A — Excellent! |
83 | Grade: B — Good job! |
74 | Grade: C — Satisfactory |
61 | Grade: D — You passed |
42 | Grade: F — Please try again |
1. Using = instead of ==
# WRONG — this is assignment, not comparison
if score = 90:
# CORRECT
if score == 90:
2. Forgetting the colon :
# WRONG
if score >= 90
print("A")
# CORRECT
if score >= 90:
print("A")
3. Inconsistent indentation
# WRONG — mixing tabs and spaces causes errors
if score >= 90:
print("A") # tab
print("A") # spaces — Python sees these as different
# CORRECT — use 4 spaces consistently
if score >= 90:
print("A")
4. Unreachable elif
# WRONG — the first condition catches everything >= 60
if score >= 60:
print("Passed")
elif score >= 90: # this will NEVER run
print("A")
Always put more specific conditions before more general ones.
== for comparison, never =Next lesson: Loops — what happens when you need to make the same decision (or action) hundreds of times?
Get this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises