python › Conditional statements in Python
phoolchandra · Oct 23, 2026 · Published
Every useful program makes decisions: if the password is correct, log in; if marks are above 90, give grade A; otherwise show an error. In Python, these decisions are written with if, elif and else. In this blog you will learn conditional statements from the basics to pro tricks like the ternary operator and match-case.
Watch the video first, then use this blog as your notes. Every section has runnable examples, and at the end you will find a "Predict the Output" quiz, practice challenges, a self-check list, interview questions and a quick summary.
How to use this blog: Open the Python shell (type python in your terminal) or any online Python editor and type every example yourself. Change the values, break the code and see what happens — that is how concepts really stick.
Conditional statements let your program run different code depending on whether a condition is True or False. Think of a traffic signal: red → stop, yellow → slow down, green → go.
condition?
/ \
True False
| |
run if-block run else-block
\ /
rest of programConditions are built using these operators. Each returns True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| > | Greater than | 7 > 9 | False |
| < | Less than | 2 < 4 | True |
| >= | Greater than or equal | 5 >= 5 | True |
| <= | Less than or equal | 3 <= 1 | False |
| and | Both must be True | 5 > 2 and 3 > 1 | True |
| or | At least one True | 5 > 9 or 3 > 1 | True |
| not | Reverses the result | not 5 > 2 | False |
| in | Membership | "a" in "cat" | True |
age = 20
if age >= 18:
print("You are eligible to vote")
print("Program finished")Rules: the condition ends with a colon :, and the block below is indented (4 spaces). All indented lines belong to the if.
number = int(input("Enter a number: "))
if number % 2 == 0:
print(number, "is Even")
else:
print(number, "is Odd")Use elif (short for "else if") to check several conditions. Python checks them top to bottom and runs only the first one that is True.
marks = 82
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
elif marks >= 60:
grade = "C"
elif marks >= 33:
grade = "D"
else:
grade = "Fail"
print("Grade:", grade) # Grade: BOrder matters: If you check marks >= 33 first, a student with 95 marks would get grade D! Always put the most specific / highest condition first.
age = 25
has_licence = True
if age >= 18:
if has_licence:
print("You can drive")
else:
print("Get a licence first")
else:
print("Too young to drive")The same logic is often cleaner with and:
if age >= 18 and has_licence:
print("You can drive")temp = 28
if temp > 20 and temp < 35:
print("Pleasant weather")
# Python allows chained comparisons - more readable!
if 20 < temp < 35:
print("Pleasant weather")
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("Weekend!")
# shorter with "in"
if day in ("Saturday", "Sunday"):
print("Weekend!")A condition does not need a comparison. Python treats 0, "", [], {}, None and False as False; everything else is True.
name = input("Enter name: ")
if name: # True if not empty
print("Hello,", name)
else:
print("You didn't enter anything")
cart = []
if not cart:
print("Your cart is empty")Syntax: value_if_true if condition else value_if_false
age = 16
status = "Adult" if age >= 18 else "Minor"
print(status) # Minor
x = 7
print("Even" if x % 2 == 0 else "Odd") # OddWhen you compare one variable against many fixed values, match-case is cleaner than a long elif chain.
command = "stop"
match command:
case "start":
print("Starting...")
case "stop":
print("Stopping...")
case "pause" | "wait":
print("Pausing...")
case _:
print("Unknown command") # _ is the default case
# Stopping...Python does not allow an empty block. Use pass as a placeholder while you are still writing code.
if marks > 100:
pass # TODO: handle invalid marks later| Mistake | Problem | Correct |
|---|---|---|
| if x = 5: | SyntaxError — = is assignment | if x == 5: |
| if x > 5 | Missing colon | if x > 5: |
| Body not indented | IndentationError | Indent with 4 spaces |
| else if x > 5: | SyntaxError | elif x > 5: |
| if day == "Sat" or "Sun": | Always True! "Sun" is truthy | if day in ("Sat", "Sun"): |
| else x > 5: | else takes no condition | else: |
A year is a leap year if it is divisible by 4, except century years, which must be divisible by 400.
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(year, "is a Leap Year")
else:
print(year, "is NOT a Leap Year")
# 2024 -> Leap Year, 1900 -> NOT a Leap Year, 2000 -> Leap Yeara = float(input("First number: "))
op = input("Operator (+, -, *, /): ")
b = float(input("Second number: "))
if op == "+":
print(a + b)
elif op == "-":
print(a - b)
elif op == "*":
print(a * b)
elif op == "/":
print(a / b if b != 0 else "Cannot divide by zero")
else:
print("Invalid operator")Don't run the code yet! Write your answers in a notebook, then check the Answers section at the end of this blog.
Q1. What is the output?
x = 5
if x > 3:
print("A")
elif x > 1:
print("B")
else:
print("C")Q2. What is the output?
if 0:
print("yes")
else:
print("no")Q3. What is the output?
a = 5
if a > 2 and a < 4:
print("in")
else:
print("out")Q4. What is the output?
x = 7
if x > 5:
print("big")
if x > 6:
print("bigger")Q5. What is the output?
print(1 < 2 < 3, 3 > 2 > 2)Q6. What is the output?
day = "Monday"
if day == "Saturday" or "Sunday":
print("Weekend")
else:
print("Weekday")Q7. What is the output?
n = 15
print("Fizz" if n % 3 == 0 else "Buzz")Q8. What happens?
x = 10
if x = 10:
print("ten")Solve these from easy to tricky:
Solutions for challenges 2, 4 and 5 are in the Answers section.
Tick off each skill once you are confident:
Q: What is the difference between if-elif and multiple if statements?
In an if-elif chain, only the first True branch runs. With separate ifs, every condition is checked and every True block runs.
Q: Does Python have a switch statement?
Python 3.10 introduced match-case (structural pattern matching). Before that, if-elif chains or dictionaries were used.
Q: What is short-circuit evaluation?
With and, Python stops at the first False; with or, at the first True. E.g. x != 0 and 10 / x > 1 never divides by zero.
Q: What is the ternary operator in Python?
a if condition else b — a one-line if-else expression.
Q: What is the use of pass?
It is a null statement used as a placeholder where a block is syntactically required.
| Concept | Syntax / Key point |
|---|---|
| if | if x > 0: |
| if-else | if ...: ... else: ... |
| elif | elif x == 0: |
| Ternary | "Yes" if ok else "No" |
| Range check | if 1 <= x <= 10: |
| Membership | if x in [1, 2, 3]: |
| Pattern match | match x: case 1: ... |
# Challenge 2
a, b, c = 12, 45, 30
if a >= b and a >= c:
print("Largest:", a)
elif b >= c:
print("Largest:", b)
else:
print("Largest:", c) # Largest: 45
# Challenge 4
units = 250
if units <= 100:
bill = units * 5
elif units <= 200:
bill = 100 * 5 + (units - 100) * 7
else:
bill = 100 * 5 + 100 * 7 + (units - 200) * 10
print("Bill: Rs", bill) # Bill: Rs 1700
# Challenge 5
n = 15
if n % 3 == 0 and n % 5 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n) # FizzBuzzYour programs can now think and decide. Next, learn for and while loops so your programs can repeat tasks automatically.
If this blog helped you, watch the full video above, subscribe to the channel and share it with a friend who is learning Python. Happy coding!