Himanshu SharmaSoftware engineer
Let’s talk

Python Tutorial · Lesson 15 of 70

Python decisions: if, elif and else

Make your Python programs choose between actions with if, elif and else, using comparisons, indentation, nested conditions and the conditional expression.

13 minControl flowUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonWrite if, elif and else statements that run different code depending on a condition, and indent them correctly.

After this lesson you can make a program choose. Instead of running every line every time, your code can ask a question and take different paths depending on the answer. That is the first big step from a list of instructions to real logic.

A single if

An if statement runs a block of code only when its condition is true:

python
stock = 3
if stock < 5:
    print("Low stock, please reorder")
print("Check finished")
result
Low stock, please reorder
Check finished

Look at the shape. The if line ends with a colon. The line below is indented by four spaces. Everything indented under the if is its block, and the block runs only if the condition is true. The last line is not indented, so it belongs to the main program and runs either way.

If the condition is false, Python skips the block:

python
stock = 40
if stock < 5:
    print("Low stock, please reorder")
print("Check finished")
result
Check finished

Only the last line appears.

Conditions

A condition is anything that comes out as true or false. The usual tools are comparisons:

python
price = 50
print(price == 50)
print(price != 50)
print(price > 20)
print(price <= 20)
result
True
False
True
False

Use == to test for equality. A single = is assignment, and writing it inside an if gives a syntax error. You can join conditions with and, or and not. The earlier lessons on comparisons and logical operators cover these in detail.

else: the other path

Add else for the case where the condition is false:

python
age = 16
if age >= 18:
    print("Adult ticket")
else:
    print("Child ticket")
result
Child ticket

Exactly one of the two blocks runs. else has no condition of its own, only a colon.

elif: more than two paths

When there are several cases, use elif, short for "else if". Python tests each condition from the top and runs the first block whose condition is true. If none is true, else runs:

python
marks = 72
if marks >= 90:
    grade = "A"
elif marks >= 75:
    grade = "B"
elif marks >= 60:
    grade = "C"
else:
    grade = "D"
print("Grade:", grade)
result
Grade: C

Because 72 fails the first two tests but passes the third, the grade is C. Once a branch has run, Python skips all the rest of the chain.

Order matters

Since the first true branch wins, the order of conditions changes the result. Here the conditions are in the wrong order:

python
marks = 95
if marks >= 60:
    grade = "C"
elif marks >= 90:
    grade = "A"
print("Grade:", grade)
result
Grade: C

A mark of 95 is at least 60, so the first branch runs and the better test is never reached. Put the most specific or strictest condition first.

Combining conditions

Use and when both tests must be true and or when either may be:

python
age = 25
has_card = True
if age >= 18 and has_card:
    print("Entry allowed")
else:
    print("Entry refused")
result
Entry allowed

You can also place an if inside another if. The inner one is indented a further level:

python
is_member = True
total = 1200
if total >= 1000:
    if is_member:
        print("Free delivery and a gift")
    else:
        print("Free delivery")
else:
    print("Delivery charge applies")
result
Free delivery and a gift

Deep nesting gets hard to read, so combining conditions with and is often clearer.

A one-line choice

For a simple either-or value there is a compact form, the conditional expression:

python
quantity = 12
label = "bulk" if quantity >= 10 else "single"
print(label)
result
bulk

Read it as "the value is bulk if the condition holds, otherwise single". Use it only for short, simple choices.

What if the indentation is wrong

Indentation is how Python knows what belongs in the block. Leave it out and you get an error:

python
if 5 > 3:
print("yes")
result
IndentationError: expected an indented block

Thinking about conditions before you type

Decisions are easier to code when you first describe them in plain words. Write the rules as a list: "if the order is over 1000, delivery is free; otherwise if it is over 500, delivery costs 40; otherwise it costs 80". Each phrase becomes one branch, and the order of the list is the order of the code.

A second habit is to check the edges. Ask what happens when the value is exactly on a boundary, such as an order of exactly 1000 or an age of exactly 18. Whether you write > or >= decides which side the boundary falls on, and mistakes here are among the most common bugs in real programs. Test one value below the boundary, one on it and one above it.

If you find yourself writing a very long chain of elif lines that all compare the same thing to different values, a dictionary, which you will meet in the collections section, is often a tidier answer.

Common mistakes

  • Missing the colon at the end of if, elif or else.
  • Using = instead of == in a condition.
  • Putting conditions in the wrong order so that a broad one hides a narrow one.
  • Mixing up indentation. The block must be indented, and all lines of one block must line up.
  • Writing else if. Python spells it elif.
  • Repeating a test. After if x > 5:, the else already means "not greater than 5", so you do not need to test it again.

Exercise

A cinema charges 100 for children under 12, 150 for ages 12 to 59 and 80 for ages 60 and over. Write code that sets age to 65 and prints the ticket price.

Show one possible answer
python
age = 65
if age < 12:
    price = 100
elif age < 60:
    price = 150
else:
    price = 80
print("Ticket price:", price)
result
Ticket price: 80

The second test only runs when the age is 12 or more, so age < 60 really means "12 to 59". Each branch relies on the ones above it, which is why the order is so important.

Next: repeating work with while loops.

Key points

  • if runs a block only when its condition is true. The line ends with a colon and the block is indented.
  • elif adds more conditions, checked in order. else catches everything that is left.
  • Only the first matching branch runs, so the order of your conditions matters.
  • Combine tests with and, or and not, and use == for equality, never a single =.

Check yourself

1. What must follow the condition on an if line?
2. If several conditions in an if/elif chain are true, how many branches run?
3. Which line correctly tests whether score equals 50?
4. When does the else block run?
5. Why is the order of conditions important in an if/elif chain?