Menu
Try in Playground

Python if, elif, else: Conditional Statements and Ternary Expressions

How Python's if, elif, and else statements work — including truthy values, nested conditions, and the ternary if expression.

A Decision, Shaped Like English

if is the first tool most programs reach for. It lets your code take one path when a condition is true and a different path when it isn't. Python spells this out directly:

main.py
Output
Click Run to see the output here.

Two lines, one decision. The if line ends in a colon. The indented line below is the body of the if — the code that runs if the condition is true. If hour were 15, the body would be skipped entirely and nothing would print.

Indentation is the structure here. Python knows which lines belong to the if by looking at their leading whitespace. Four spaces per level is the convention; be consistent.

Adding an else Branch

Often you want something to happen either way: this when the condition is true, that otherwise. else is how:

main.py
Output
Click Run to see the output here.

The else line has no condition — it catches everything the if didn't.

Multiple Branches With elif

For more than two paths, stack elif ("else if") clauses between if and else:

main.py
Output
Click Run to see the output here.

Python walks the conditions top-down and runs the block for the first one that's true. The rest are skipped. This top-down order matters: if you put score >= 60 before score >= 90, everybody gets a D — the first true condition wins.

A tidy structure to keep in mind: the conditions should be mutually exclusive for any given input, and the order should reflect how you naturally think about the problem. Put the most specific cases first, the most general last.

Truthy Values in Conditions

The expression in if doesn't have to evaluate to a literal True or False. Python accepts any value and decides its truthiness:

main.py
Output
Click Run to see the output here.

if items: reads as "if the list has anything in it." Falsy values include:

  • False
  • 0, 0.0
  • None
  • Empty containers: "", [], {}, set(), ()

Everything else is truthy. Using truthiness makes code read more naturally, with one caveat: if zero is a meaningful value (a counter that's legitimately at zero), compare explicitly with is not None or != 0 to avoid the "empty = zero" confusion.

Combining Conditions

and, or, and not let you build compound conditions:

main.py
Output
Click Run to see the output here.

Chained comparisons are tidier than an explicit and when the endpoints share a middle value:

main.py
Output
Click Run to see the output here.

That's the same as 18 <= age and age < 65, but it reads like a math inequality.

Nested if Statements

You can put if inside if. It's legal, and sometimes the clearest structure — but it's also the main way conditional code becomes hard to follow. If you find yourself three levels deep, consider flattening with early returns (once you're writing functions) or by combining conditions:

main.py
Output
Click Run to see the output here.
main.py
Output
Click Run to see the output here.

Both work. Pick whichever version reads better to a reader who hasn't seen this code before.

The Ternary Expression

When you just want a value to be one of two things depending on a condition, Python has a compact form:

main.py
Output
Click Run to see the output here.

Read it left to right: "warm if temp is at least 70, else cool." It's great for short either/or assignments. Stop using it once the branches get long or contain multiple operations — at that point, a full if/else block is easier on the eyes.

if Is an Expression in One Place Only

Unlike some languages, Python's if statement itself doesn't produce a value. Only the ternary form does. You can't write x = if ...: the way you can in Rust or Kotlin. If you want a conditional value, use the ternary; if you want conditional behavior, use a full if block.

A Small Complete Example

A quick program that classifies a temperature:

main.py
Output
Click Run to see the output here.

Notice that the logic is mostly just deciding what advice should be. You can tell at a glance which temperature range maps to which message. That's the shape you're aiming for when you write conditions — each branch should be small and the overall intent obvious.

What's Next

Conditions let your code choose a path. Loops let it repeat. Next up: for loops, the most common loop in Python by a wide margin, and the natural partner to the collections you'll meet a few pages later.

Frequently Asked Questions

How does an if statement work in Python?

if condition: followed by an indented block runs the block only when the condition is true. Optional elif (else if) clauses let you check additional conditions, and an optional else runs when nothing else matched.

Does Python have a switch statement?

Python 3.10 added match/case for structural pattern matching, which covers most switch-statement use cases and more. For simple branching, a chain of if/elif is still common and perfectly fine.

What is a ternary if in Python?

A one-line conditional expression: result = a if condition else b. It reads left-to-right — evaluate a when condition is true, otherwise evaluate b. Handy for short either/or assignments; less readable once the branches get long.

Learn to code with Coddy

GET STARTED