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:
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:
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:
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:
if items: reads as "if the list has anything in it." Falsy values include:
False0,0.0None- 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:
Chained comparisons are tidier than an explicit and when the endpoints share a middle value:
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:
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:
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:
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.