Conditionals in Python (if, elif, else)
Conditionals in Python let a program run different code depending on a condition, using if to run code when true, elif to check additional possibilities in order, and else as a catch-all default.
Conditionals in Python — if, elif, and else — are how you teach a program to choose. Once you can build boolean expressions with comparison operators, conditionals are what actually put those questions to use, letting a program run different code depending on what it finds.
What you will learn
- How
ifruns code only when a condition is true - The
passstatement for planning branches you haven't written yet - How
elsehandles the opposite case - Chaining multiple possibilities with
elif - Nested conditionals — and when to flatten them with
and/orinstead
Conditional execution: teaching your program to choose
The if statement is how you tell Python: run this only if the condition is true.
if x > 0:
print('x is positive')if— the keyword that starts a decisionx > 0— the condition, a boolean expression that Python evaluates:— the colon tells Python "here comes the body of this decision"print('x is positive')— the body, indented four spaces, running only if the condition wasTrue
If the condition is False, Python completely skips the indented body and moves on to whatever comes next — no error, no message, just silence. This structure looks just like function definitions: a header ending in a colon, followed by an indented body. That is not a coincidence — Python uses this same pattern for almost everything that has "a header and a block that belongs to it."
pass: a placeholder for code you haven't written yet
Sometimes you want to plan out a branch but have not written the code for it yet. Python provides pass for exactly this situation — a statement that does nothing at all, but satisfies Python's requirement that the body cannot be empty:
if x < 0:
pass # TODO: handle negative numbers laterThink of pass as a sticky note on an empty shelf — the shelf is there, the label is there, but you have not put anything on it yet.
Alternative execution: what about the other case?
An if on its own only handles the case where the condition is true. Sometimes you want something to happen in both cases — that is what else is for:
if x % 2 == 0:
print('x is even')
else:
print('x is odd')Now there are two paths: if x % 2 == 0 is True, the first block runs; if it is False, the else block runs instead. One of these two will always run — there is no situation where neither happens. These two paths are called branches, like a fork in a road — you go one way or the other, never both.
Chained conditionals: handling more than two possibilities
Two branches covers a lot of situations, but not all of them. What if you have three, four, or more possible outcomes? There is a cleaner way than separate if statements: elif, short for "else if."
if x < y:
print('x is less than y')
elif x > y:
print('x is greater than y')
else:
print('x and y are equal')Python works through these from top to bottom, checking each condition in order. The moment it finds one that is True, it runs that branch and completely skips the rest — even if a later condition would also be true. The else at the end is optional, acting as a catch-all that runs if nothing above it matched.
Here is a real-world-style example — responding to a user's menu choice:
if choice == 'a':
draw_a()
elif choice == 'b':
draw_b()
elif choice == 'c':
draw_c()You can have as many elif branches as you need — there is no limit. Only the first matching one will run, so put the most specific or most important checks at the top.
Nested conditionals: conditions inside conditions
You can place an if statement inside another if statement. This is called nesting:
if x == y:
print('x and y are equal')
else:
if x < y:
print('x is less than y')
else:
print('x is greater than y')This works, but notice how each layer of nesting pushes the code further to the right. Add a few more levels and it becomes very hard to read — every level asks the reader to track another "are we inside this condition?" in their head.
Whenever you find yourself nesting, ask whether logical operators could flatten things out instead:
# Nested — two layers of indentation, harder to follow
if 0 < x:
if x < 10:
print('x is a positive single-digit number.')
# Flattened with and — one layer, reads almost like English
if 0 < x and x < 10:
print('x is a positive single-digit number.')Both do the exact same thing. The second one is just much easier to read. As a general rule: if you can avoid nesting by using and or or, do it.
Common mistakes
- Forgetting the colon at the end of an
if,elif, orelseline - Inconsistent indentation, which causes an
IndentationError - Writing an
elifthat can never be reached because an earlier condition already covers it - Nesting three or four levels deep when
and/orwould flatten the logic - Forgetting a default
elsebranch and silently doing nothing for unexpected input
Common questions
What is the difference between if, elif, and else in Python?
if checks a condition and runs its body if true. elif (else if) checks another condition only if the previous ones were false. else is a catch-all that runs if none of the if/elif conditions matched.
What does the pass statement do in Python?
pass does nothing — it's a placeholder used when Python requires a body (like inside an if block) but you haven't written the logic yet.
Can you have multiple elif statements in Python?
Yes, there's no limit on the number of elif branches. Python checks them top to bottom and runs only the first one that matches, skipping the rest.
What is a nested conditional in Python?
A nested conditional is an if statement placed inside another if statement's body. It works, but deep nesting hurts readability — combining conditions with and or or often flattens the same logic into one level.
What happens if no if/elif condition matches and there's no else?
If none of the conditions are true and there is no else block, Python simply skips the entire if/elif chain and continues with the next statement — no error, no output.
Explore
Related topics
Keep going — these sit next to this concept in a real learning path.
Comparison Operators in Python
Building the boolean expressions conditionals rely on.
The match-case Statement in Python (Python's Switch)
match/case, default cases, and dictionary dispatch.
Recursion in Python
Base cases, stack frames, factorial, and Fibonacci.
Python course hub
All free Python explainers and the path into Agentic practice.
Try/Except and Exception Handling in Python
Catching errors gracefully with try, except, else, finally.