Need some simple python if else examples that actually make sense? Not the ones that leave you more confused than when you started? You’re in the right spot. Whether you’re learning Python for the first time, prepping for an interview, or just trying to get your head around how control flow works, this will help.
You’ll get to see how conditionals and loops help Python make decisions, how a while loop works differently from a for loop, and you’ll get real code you can mess with. No fluff. Just code, explanations, and exercises that show you what’s going on behind the scenes.
We’ll look at if, elif, and else. We’ll also walk through loops, then jump into fun stuff like break, continue, and pass. Oh and we’re not skipping Python 3.10’s match-case either. That bit makes your code much easier to read. Let’s crack on.
Python If Else Examples: The Basics
How to Use If Else in Python
If you want your program to make decisions, this is where conditionals come in. Here’s an easy one:
# Challenge: Check if a number is positive, negative, or zero
num = 5
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
Quick Exercise
Write a program that checks if a given number is odd or even.
Hint: use the modulo operator (%).
Python While Loop vs For Loop: What’s the Difference?
Both loops do the same job — repeat stuff — but they’re used differently:
- For loop – Great when you already know how many times something should run.
- While loop – Handy when you want something to keep going until a condition changes.
# For loop: print numbers 1 to 5
for i in range(1, 6):
print(i)
# While loop: same thing
i = 1
while i <= 5:
print(i)
i += 1
Try This
Write a loop that keeps asking the user for input until they type "exit".
Python Break, Continue, and Pass Explained
These little keywords change how your loop behaves. They’re small but mighty:
break– Gets out of the loop straight away. No questions asked.continue– Skips to the next round of the loop. Doesn’t finish the current one.pass– Does literally nothing. Just sits there like a reserved sign at a dinner table.
# Break example
for i in range(10):
if i == 5:
break
print(i)
# Continue example
for i in range(5):
if i == 2:
continue
print(i)
# Pass example
for i in range(3):
if i == 1:
pass # Placeholder for future code
print(i)
Python Match Case Examples
Starting from Python 3.10, match-case is a nicer way to check a bunch of options. No more messy if-elif-else ladders.
# Challenge: Simple calculator with match-case
op = "add"
a, b = 10, 5
match op:
case "add":
print(a + b)
case "subtract":
print(a - b)
case "multiply":
print(a * b)
case "divide":
print(a / b)
case _:
print("Unknown operation")
Helpful Resources to Keep Learning
Want to learn more about Python? These are great places to go next:
- Python Syntax and Indentation – See how blocks of code are grouped.
- Python Data Types Explained – Get to know the stuff Python works with.
- Printing in Python – Debug your code like a pro using print statements.
Frequently Asked Questions About Python If Else Examples
What is the purpose of if else in Python?
If-else lets Python pick what code to run based on a condition. It’s how your program makes choices.
How is elif different from else?
elif checks another condition. else only runs if nothing else was true.
When should I use a while loop instead of a for loop?
If you don’t know how long something should run, use a while loop. If you do, go with for.
What’s the difference between break and continue?
break ends the loop right then and there. continue skips the current turn and goes again.
What does pass do in Python?
pass is a placeholder. It doesn’t do anything but it stops your code from crashing while you figure stuff out.
Is match-case better than if-else?
If you’re checking lots of values, match-case keeps your code tidier. But if-else still works for more complicated stuff.
Keep Practicing and Building
You’ve now seen some practical python if else examples. You’ve looked at loops, break and continue, even the fancy new match-case thing. Now it’s all about putting this stuff into action. Start with small scripts, build your confidence, and then try bigger problems.
Play with variables, check your syntax, and keep using print to debug. It all adds up. And before long, this stuff will feel easy.