
In the last post, we built our first Python script, explored variables, and got user input. Now itβs time to make your programs think using if/else statements, comparison operators, and logic.
This is how your code decides what to do based on conditions β like whether a number is too big, a password is correct, or someone is allowed to log in.
π Step 1: Understanding if Statements
An if statement lets your code make decisions.
age = 18
if age >= 18:
print("You are an adult.")
If the condition age >= 18 is True, the indented code runs. If not, Python skips it and does nothing.
Output:
You are an adult.
If the condition is False and there’s no else, nothing happens. Thatβs perfectly valid β but sometimes you’ll want to handle the “what if itβs not true?” case too.
π Step 2: Adding else
Use else to define what should happen if none of the previous conditions are True.
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are underage.")
Output:
You are underage.
π Note:
elseis optional β but if you use it, it must come last in your if-elif-else chain. If you try to put anelifafterelse, Python will give you a syntax error.
βοΈ Step 3: Using elif (short for “else if”)
elif lets you check multiple conditions in sequence:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C or lower")
Output:
Grade: B
π Note: Python checks conditions from top to bottom and only runs the first one that is
True. It skips the rest after finding a match.
You can have only if and elif β and leave out else entirely if you donβt care about unmatched cases:
score = 60
if score >= 90:
print("A")
elif score >= 80:
print("B")
Output:
(No output β and that’s OK)
π’ How Many elif Can You Use?
π§ Python doesnβt limit how many
elifblocks you can have β use as many as you need. Just remember:
- If you have a long chain of conditions, your code might become harder to read.
- Consider using a dictionary, a function, or match-case (introduced in Python 3.10) if the logic starts to look repetitive or bulky.
Example with several elif blocks:
day = "Wednesday"
if day == "Monday":
print("Start of the week.")
elif day == "Tuesday":
print("Keep going.")
elif day == "Wednesday":
print("Halfway there!")
elif day == "Thursday":
print("Almost done.")
elif day == "Friday":
print("TGIF!")
else:
print("Weekend mode.")
β Works perfectly β just stay organized!
β Step 4: Comparison Operators
These are the tools you use to compare values in your conditions:
== # equal to
!= # not equal to
> # greater than
< # less than
>= # greater than or equal to
<= # less than or equal to
Example:
x = 5
if x != 10:
print("x is not 10")
Output:
x is not 10
π Step 5: Combining Conditions with and / or
You can check multiple things at once using logic operators.
age = 20
has_id = True
if age >= 18 and has_id:
print("Access granted.")
else:
print("Access denied.")
Output:
Access granted.
π§
and= both conditions must be True
πor= at least one condition must be True
π― Step 6: Interactive Example with User Input
Letβs put it all together with input():
age = int(input("Enter your age: "))
has_id = input("Do you have an ID? (yes/no): ")
if age >= 18 and has_id.lower() == "yes":
print("β
You can enter the venue.")
else:
print("β Access denied.")
Sample run:
Enter your age: 22
Do you have an ID? (yes/no): yes
β
You can enter the venue.
π Note:
input()always returns a string, even if the user types a number.
Thatβs why we useint()to convert age to a number so we can compare it.
β Recap: What You Learned
if,elif, andelsecontrol the flow of logic in your programelseis optional, but if you use it, it must go last- Python runs only the first True condition
- You can stack multiple
elifblocks β no hard limit - Use
andandorto combine conditions - Input from users is always a string, so use
int()when needed - Python uses indentation instead of curly brackets to group code
Youβve now given your programs the power to think β making them responsive and dynamic!
βοΈ Coming Up Next: Loops and Repeating Tasks
In the next lesson, weβll explore loops β how to repeat actions using while and for, how to loop through lists, and how to avoid infinite loops. Itβs about to get fun π
Leave a comment