Skip to content

Python if, elif and else: Conditional Statements With Examples

Python’s if, elif and else statements let a program choose which block to execute. This beginner-friendly guide explains the syntax, truthiness, nesting, common errors and working examples you can copy into Python 3.

Python if statement: syntax and first example

An if statement runs its indented body only when its condition is truthy. End the header with a colon and use consistent indentation (four spaces is conventional).

temperature = 28
if temperature > 25:
    print("It is warm")
# It is warm

Python evaluates the condition once when it reaches the if. If the condition is false, execution continues after the indented block. The expression need not literally return True or False: for example, a nonempty string is truthy and an empty list is falsy.

if and else: choose between two branches

Use else for the fallback when the if condition is falsy. The else header has no condition of its own.

number = 7
if number % 2 == 0:
    print("Even")
else:
    print("Odd")
# Odd

if, elif and else: check several conditions

An elif branch is tested only when all earlier branches in the same chain are false. Only the first matching branch executes. You can have multiple elif branches; else is optional and must come last.

score = 82
if score >= 90:
    grade = "A"
elif score >= 75:
    grade = "B"
elif score >= 60:
    grade = "C"
else:
    grade = "D"
print(grade)  # B

Order matters: checking score >= 60 before score >= 90 would incorrectly classify a score of 95 as C. Put the more restrictive threshold first.

Nested if statements and logical operators

You can nest an if inside another block. For simple checks, and, or and not may be easier to read. Both approaches are valid, but avoid unnecessary nesting.

age = 20
has_ticket = True
if age >= 18:
    if has_ticket:
        print("Entry allowed")
# Entry allowed

if age >= 18 and has_ticket:
    print("Entry allowed")

Truthy and falsy values in Python

Conditions use Python’s truth-value rules. 0, None, False, empty strings and empty collections are falsy; most other objects are truthy. To check specifically for a missing value, use is None rather than if not value, because zero and an empty string may be legitimate values.

name = ""
if not name:
    print("Please enter a name")

result = None
if result is None:
    print("No result yet")

Common Python conditional errors and troubleshooting

Missing colon: if x > 0 without : raises SyntaxError. Indentation: the body must be indented consistently; mixing tabs and spaces can cause TabError. Assignment versus comparison: use == to compare values, not =. Separate if statements: two independent if blocks can both run, whereas an if/elif chain selects at most one branch.

x = 10
if x > 5:
    print("Greater than five")
if x > 8:
    print("Greater than eight")
# Both lines print

Frequently asked questions

Is else required? No. An if can stand alone, and an if/elif chain can omit else.

Can an if statement have several elif branches? Yes. Python checks them from top to bottom until one matches.

Can I write a one-line condition? A short conditional expression such as label = "adult" if age >= 18 else "minor" is useful for choosing a value. Prefer normal blocks for multiple statements.

What should I learn next? Practice conditions with Python operators and see the official Python if-statement documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *