Conditional statements are how Python programs make decisions. With if, elif, and else, your code can follow different paths depending on what is true at the moment it runs.
Before Python can run a block of code, it needs to know whether that block should run at all. Conditional statements answer that question. They evaluate a condition — something that is either True or False — and then execute one block of code or another based on the result. This is control flow in its simplest form, and it appears in virtually every non-trivial Python program.
What Is a Conditional Statement?
A conditional statement gives your program a choice. Instead of executing every line from top to bottom without question, Python pauses at the condition, evaluates it, and then takes one of two or more possible paths forward.
In Python, conditions are boolean expressions — expressions that evaluate to either True or False. A comparison like age >= 18 produces a boolean. So does a function call like isinstance(x, int). You can even pass a plain value: Python treats 0, empty strings, empty lists, and None as False, and treats everything else as True.
In Python, True and False are capitalized. Writing true or false (lowercase) will raise a NameError because Python treats them as undefined variable names, not booleans.
The simplest conditional statement has two parts: the condition and the body. The body is the indented block of code that runs only when the condition is True. If the condition is False, Python skips the body entirely and continues with whatever comes after it.
temperature = 35
if temperature > 30:
print("It is hot outside.")
print("This always runs.")
In this example, temperature > 30 evaluates to True because 35 is greater than 30. Python runs the indented print call, then continues past the block to the final print, which is not conditional.
Change temperature to 20 and the condition becomes False. The indented line is skipped. The final print still runs because it is outside the block.
Python uses indentation — not curly braces — to define blocks. The standard is four spaces per level. Mixing tabs and spaces will cause an IndentationError. Configure your editor to insert four spaces when you press Tab.
Build a valid if statement that prints "Access granted" when age is 18 or older:
if statement starts with the keyword if, followed by the variable being tested (age), then the comparison operator (>=), then the value to compare against, and finally a colon (18:). The body (what runs when True) comes after, indented on the next line. else: and == are distractors — else is a separate keyword and == tests equality, not "greater than or equal to."
The if, elif, and else Keywords
A plain if handles two outcomes: the condition is True (run the block) or False (skip it). When you need more than two branches, Python provides elif — short for "else if" — and else as a final fallback.
score = 74
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Grade: {grade}")
Python evaluates the chain from top to bottom. The moment it finds a condition that is True, it runs that block and skips every remaining elif and else in the chain. In this example, score >= 90 is False and score >= 80 is also False, but score >= 70 is True, so grade is set to "C".
"The if statement is used for conditional execution." — Python Language Reference
A few rules to keep in mind when building an if/elif/else chain. Every chain must start with if. You may follow it with as many elif clauses as your logic requires. The else clause is always optional, and there can be at most one. Once a branch runs, the chain is done — order matters.
- Purpose
- Opens a conditional chain. Always required as the first keyword.
- Condition required?
- Yes. The expression after
ifmust be something Python can evaluate as True or False.
- Purpose
- Checks an additional condition, but only if all previous conditions in the chain were False.
- Condition required?
- Yes. Like
if, it must be followed by a boolean expression.
- Purpose
- A catch-all fallback that runs when every preceding condition in the chain was False.
- Condition required?
- No.
elsetakes no condition — it simply ends the chain with a colon.
This function is supposed to return "pass" if a score is 60 or above, and "fail" otherwise. One line contains a bug. Click it, then hit check.
"pass", not "fail". The if condition checks whether score >= 60, which is the passing condition. The block under that if should therefore return "pass". The else branch — reached only when the score is below 60 — should return "fail". The return values are swapped.
Combining Conditions with and, or, and not
Real programs rarely test a single fact. Python provides three logical operators — and, or, and not — that let you combine boolean expressions into more precise conditions.
username = "admin"
is_logged_in = True
# and — both sides must be True
if username == "admin" and is_logged_in:
print("Welcome to the admin panel.")
# or — at least one side must be True
role = "editor"
if role == "admin" or role == "editor":
print("You can edit posts.")
# not — inverts the boolean
is_banned = False
if not is_banned:
print("Access allowed.")
and requires both conditions to be True. or requires at least one to be True. not flips a boolean: not True is False and not False is True. You can chain these operators and use parentheses to control evaluation order, just like arithmetic.
A frequent beginner error is writing if x == 1 or 2: instead of if x == 1 or x == 2:. The first version always evaluates to True because 2 alone is a non-zero integer, which Python treats as True regardless of x. Always repeat the variable on both sides of or.
How to Write Conditional Statements in Python
The following steps walk through writing an if/elif/else statement from a blank file to working code.
-
Write the if keyword and your condition
Type
iffollowed by a boolean expression and a colon. The expression can use comparison operators such as==,!=,<,>,<=, or>=to compare values, or you can pass any value Python can evaluate as True or False. Example:if temperature > 30: -
Indent the code that should run when the condition is True
Press Tab or add four spaces and write the statement or statements that should execute when the condition is True. Every line in the block must use the same indentation level. Python uses whitespace to determine where the block begins and ends — there are no curly braces.
-
Add elif and else clauses as needed
Directly below the
ifblock (at the same indentation level as theif), writeeliffollowed by another condition and a colon to handle additional cases. Writeelse:(with no condition) as a final fallback. Bothelifandelseare optional. You may chain as manyelifclauses as your logic requires.
Python Learning Summary Points
- A conditional statement runs a block of code only when its condition evaluates to True. Python skips the block entirely when the condition is False.
elifis checked only when all conditions above it in the same chain were False. The first True branch wins and the rest of the chain is skipped.- Python uses indentation (four spaces by default) to define blocks. Every statement inside an
if,elif, orelsemust be indented at the same level. - The logical operators
and,or, andnotlet you combine conditions. Usingorcorrectly requires repeating the variable on both sides:x == 1 or x == 2, notx == 1 or 2. - Python treats
0,None, empty strings, empty lists, and empty dicts as False. Everything else is True. This means you can writeif my_list:to check whether a list is non-empty without comparing its length.
Conditional statements are one of the first places where Python programs start to feel interactive and responsive. Once you are comfortable with if, elif, and else, you have the foundation to move into loops, functions, and exception handling — all of which depend on the same idea of evaluating a condition and branching accordingly.
Frequently Asked Questions
A conditional statement in Python is a block of code that runs only when a specified condition evaluates to True. The most basic form uses the if keyword followed by a boolean expression and a colon. Python then executes the indented block beneath it if the condition is met.
if checks the first condition. elif (short for "else if") checks additional conditions only when the if above it was False. else runs as a fallback when none of the preceding conditions were True. You can have many elif clauses but only one else, and else is always optional.
No. Python uses indentation instead of curly braces to define code blocks. The body of an if, elif, or else must be indented consistently — four spaces is the standard. Inconsistent indentation causes an IndentationError.
Yes. Python allows nested if statements, where an if block sits inside another if block. Each level must be indented further than the one containing it. However, deeply nested conditions are harder to read, so it is often better to combine conditions with and or or.
Python treats the following as False: the boolean False, the integer 0, the float 0.0, an empty string '', an empty list [], an empty dict {}, an empty tuple (), None, and any object whose __bool__ method returns False. Everything else is treated as True.
Python allows writing a simple if statement on a single line when the body is a single expression: if x > 0: print('positive'). This is valid syntax but generally reserved for very short, obvious checks. Multi-statement or complex logic should stay on separate indented lines for clarity.
A ternary expression (also called a conditional expression) lets you return one of two values in a single line: value_if_true if condition else value_if_false. For example, label = 'even' if n % 2 == 0 else 'odd'. It is useful for simple assignments but should not replace multi-branch logic.
Python evaluates elif conditions from top to bottom and runs the first one that is True, then skips the rest of the chain. Once a branch runs, no further conditions in that if/elif/else chain are checked. This means the order of your elif clauses matters.