If statements let your Python programs make decisions, running different code depending on whether a condition is true or false. This guide covers if, elif, and else, step by step, with the comparisons that make conditions actually work.
If you’ve worked through our guides on variables and data types and for loops, you’re ready for the piece that makes programs genuinely responsive: the ability to check a condition and act differently depending on the answer.
This guide covers the basic if statement, adding an else for the opposite case, chaining multiple conditions with elif, and the comparison operators that make conditions meaningful in the first place.
The Basic if Statement
An if statement runs a block of code only when a specific condition is true. According to Python’s official tutorial, Python’s if statement is a standard control flow tool, borrowed conceptually from other languages but relying on indentation, rather than braces, to mark which code belongs inside it.
age = 20
if age >= 18:
print("You are eligible to vote.")Here, age >= 18 is the condition. If it evaluates to True, the indented line underneath runs. If it’s False, Python simply skips that line and moves on — nothing prints, and no error occurs.
Adding an else for the Opposite Case
Often you want something to happen in both cases — one outcome if the condition is true, a different one if it’s false. That’s what else provides:
age = 15
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not old enough to vote yet.")According to W3Schools’ guide to Python’s else statement, the else block acts as a fallback — a default action carried out whenever the condition (or conditions) above it turn out to be false, without needing to write out the opposite condition explicitly yourself.
Chaining Conditions With elif
Sometimes two options aren’t enough — you need to check several possibilities in sequence. That’s what elif (short for “else if”) is for:
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Your grade is: {grade}")As freeCodeCamp’s guide to conditional statements explains, Python checks each condition from top to bottom and stops at the first one that’s true — the remaining elif and else blocks are skipped entirely once a match is found, even if a later condition would also technically be true.
The Comparison Operators You’ll Use Constantly
Conditions rely on comparison operators to produce a True or False result:
- == — equal to (note the double equals sign; a single
=is for assignment, not comparison) - != — not equal to
- > and < — greater than / less than
- >= and <= — greater than or equal to / less than or equal to
The double-equals-versus-single-equals confusion is one of the most common early mistakes — if x = 5: is actually a syntax error in Python, since = can only be used for assignment, which helps catch this particular mistake immediately rather than letting it fail silently.
Combining Conditions With and / or
Sometimes a decision depends on more than one condition at once. Python’s and and or keywords let you combine them:
age = 25
has_license = True
if age >= 18 and has_license:
print("Eligible to drive.")
else:
print("Not eligible to drive.")and requires both conditions to be true; or requires only one of them to be true. Understanding which one your logic actually needs — both conditions, or just one of several — is a common source of subtle bugs when first combining conditions.
A Practical Example
Here’s a slightly larger example combining if statements with a loop from earlier in this series:
numbers = [3, -1, 0, 8, -5]
for num in numbers:
if num > 0:
print(f"{num} is positive")
elif num < 0:
print(f"{num} is negative")
else:
print(f"{num} is zero")This loops through a list and, for each number, checks whether it's positive, negative, or exactly zero — a genuinely common pattern combining loops and conditions together.
What Counts as "True" Beyond True and False
Python conditions don't only work with explicit True and False values — many other values are automatically treated as true or false when used in a condition. Empty strings, the number zero, empty lists, and None are all treated as "falsy," while most other values are treated as "truthy":
name = ""
if name:
print(f"Hello, {name}!")
else:
print("No name provided.")Here, an empty string is falsy, so the else branch runs. This pattern — checking a variable directly rather than comparing it explicitly to an empty value — is extremely common in real Python code, and worth recognizing even before you start using it yourself regularly.
A Shorter Way to Write Simple Conditions
For simple cases where you're just choosing between two values, Python offers a compact one-line form called a conditional expression:
age = 20
status = "adult" if age >= 18 else "minor"
print(status)This is functionally identical to a full if/else block that assigns a value to status, just written on a single line. It's worth knowing this form exists since you'll see it in other people's code, but a full if/else block is usually clearer while you're still learning — reach for the shorthand once the basic pattern feels completely natural.
Nested Conditions in Practice
Sometimes a decision genuinely depends on a condition inside another condition. Here's a realistic example:
age = 25
has_license = True
if age >= 18:
if has_license:
print("You can drive.")
else:
print("You need a license first.")
else:
print("You're too young to drive.")This checks age first, and only checks the license if the age requirement is already met. Notice this could also be written with a single and in some cases — nesting is most useful when the inner condition genuinely only makes sense to check after the outer one has already passed.
Keeping Conditions Readable
As conditions get more complex, it's worth prioritizing readability over cleverness. Comparing a variable to True or False explicitly, like if is_valid == True:, is unnecessary — the variable is already a boolean, so if is_valid: says exactly the same thing more directly. Similarly, breaking a genuinely complex condition into a well-named intermediate variable often reads far more clearly than cramming everything into one long line:
# Harder to read at a glance
if age >= 18 and has_license and not is_suspended:
print("Can drive")
# Slightly clearer with an intermediate variable
can_drive = age >= 18 and has_license and not is_suspended
if can_drive:
print("Can drive")Neither approach is strictly "correct" — but as your conditions grow more elaborate, favoring clarity over compactness tends to save real debugging time later, especially when you return to code you wrote weeks or months earlier.
Common Questions Beginners Ask About If Statements
Do I always need an else? No. An if statement without an else is completely valid — it simply does nothing when the condition is false, rather than requiring an explicit fallback.
Can I nest if statements inside each other? Yes — an if statement can contain another if statement inside its indented block. This is called nesting, and while it's sometimes necessary, deeply nested conditions can get hard to read, so it's often worth looking for a simpler structure (like combining conditions with and) when nesting starts getting more than two or three levels deep.
What if I forget the colon at the end of my if line? Python will raise a SyntaxError immediately, pointing at the missing colon. This is one of the more beginner-friendly errors, since Python tells you exactly what's missing rather than failing silently somewhere else in your program.
Can I use if statements to validate user input? Yes, and it's one of the most common real-world uses — checking whether input meets certain requirements before proceeding, and giving a clear message back to the user if it doesn't.
Quick-Reference Python If Statement Guide
- if condition: — runs the indented block only if the condition is true.
- else: — a fallback that runs when the if condition is false.
- elif condition: — checks an additional condition if the earlier ones were false.
- == vs = — double equals compares; single equals assigns.
- and / or — combine multiple conditions into one check.
- Only one block runs — Python stops at the first true condition in an if/elif/else chain.
Conclusion
If statements are what let a program actually respond to different situations, rather than running the same fixed sequence of steps every single time it's executed. Understanding the basic if, adding else for the opposite case, and chaining additional conditions with elif covers the overwhelming majority of decision-making logic you'll write as a beginner.
The best way to build comfort here is direct practice: write a few small conditions yourself, checking things like whether a number is even, whether a password meets a minimum length, or whether a value falls within a range.
In the next guide in this series, we'll cover Python functions — how to package up reusable blocks of code, often built around the exact conditions covered in this guide.
Explore More Python for Beginners Guides →

When not writing, Alex is probably debugging someone else’s code.