Decision-Making in Python: Mastering if, elif, and else
In programming, it’s not enough to just perform calculations or store data — we often need our programs to respond to different situations. In other words:
“If something happens, do this. Otherwise, do something else.”
Python provides a simple but powerful way to handle this using decision-making logic with the keywords if, elif, and else.
1. What is an if statement?
The if statement allows your program to run a block of code only when a specific condition is true.
number = 10
if number > 0:
print("The number is positive.")
Output:
The number is positive.
If number = -5, the code inside the if block would be skipped entirely.
Python automatically evaluates conditions like number > 0 as True or False.
2. What if the condition is not true? (else)
The else block provides an alternative action when the if condition is false.
number = -3
if number > 0:
print("The number is positive.")
else:
print("The number is zero or negative.")
Output:
The number is zero or negative.
3. Handling multiple options: elif
elif stands for "else if" and lets you test multiple conditions in sequence. You can use as many elif branches as you like, but only one else.
score = 72
if score >= 90:
print("Excellent!")
elif score >= 70:
print("Very good.")
elif score >= 50:
print("Satisfactory.")
else:
print("Failed.")
Output:
Very good.
Python runs only the first true condition – all others are skipped.
4. Combining conditions: and, or, not
Sometimes you want to evaluate multiple conditions at once. You can use logical operators:
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("Access granted.")
else:
print("Access denied.")
and – both conditions must be true
or – at least one condition must be true
not – inverts the condition (not True becomes False)
5. Indentation matters in Python!
Unlike many other languages, Python does not use curly braces {} to define code blocks. Instead, it uses indentation (spaces or tabs).
Recommended by LinkedIn
x = 10
if x > 5:
print("Greater than 5")
print("Still inside the if block")
print("Outside the condition")
Incorrect indentation will result in an error:
if True:
print("Error!") # ❌ IndentationError
6. Nested conditions – decisions inside decisions
You can place one condition inside another to create more precise logic:
age = 25
is_student = True
if age >= 18:
if is_student:
print("Adult student")
else:
print("Adult non-student")
else:
print("Minor")
Output:
Adult student
7. You don't always need else
It’s totally fine to use just an if when you only care about a single situation:
language = "Python"
if language == "Python":
print("This is our favorite language!")
No else is needed if there's nothing to handle otherwise.
8. Common mistakes and how to avoid them
❌ Using = instead of ==:
if x = 5: # Error!
✅ Use:
if x == 5:
❌ Incorrect indentation:
if x > 5:
print("Wrong")
✅ Use:
if x > 5:
print("Correct")
❌ Forgetting the colon :
if x > 5 # Missing colon!
✅ Use:
if x > 5:
Summary
„Technologie jsou srdce, tým je duše.“
1moZačal jsi programovat v Excelu? Nebo si hraješ s arduinem ?