In this Python tutorial, you will learn how the elif statement works, how Python evaluates an if-elif-else chain, and how to use multiple conditions with practical examples.

Python elif statement

The Python elif keyword means “else if.” It lets a program test another condition when the preceding if or elif condition is false.

An if-elif-else chain is useful when a program must choose one action from several alternatives. Python checks the conditions from top to bottom and executes only the first block whose condition evaluates to True.

Python elif extends the Python if-else statement by allowing additional conditional checks between the initial if block and the optional else block.

Syntax of Python elif

The syntax of a Python if-elif-else statement is shown below.

</>
Copy
 if expression_1:
    statement(s)
 elif expression_2:
    statement(s)
 elif expression_3:
    statement(s)
 else:
    statement(s)

The first condition follows the if keyword. Every additional condition follows an elif keyword. The final else block has no condition because it handles every case that was not matched earlier.

How Python evaluates an if-elif-else chain

  1. Python evaluates expression_1.
  2. If it is True, Python executes the corresponding if block and skips the remaining branches.
  3. If it is False, Python evaluates expression_2.
  4. Python continues checking each elif condition until one evaluates to True.
  5. If every condition is false, Python executes the optional else block.

At most one branch in a single if-elif-else chain is executed. Even when more than one condition could be true, only the first matching branch runs.

Indentation rules for elif blocks

Python uses indentation to define each conditional block. The if, elif, and else keywords must be aligned at the same indentation level, while the statements inside each branch must be indented consistently.

</>
Copy
temperature = 24

if temperature > 30:
    print("Hot")
elif temperature >= 20:
    print("Warm")
else:
    print("Cool")

A colon is required after every if, elif, and else header. Incorrect alignment or inconsistent indentation can result in an IndentationError or cause statements to belong to the wrong block.

Python elif examples

1. Select an action with multiple elif conditions

In the following example, the program checks the current day and selects an action for that day.

Python Program

</>
Copy
today='Monday'

if today=='Sunday':
	print('eat apple.')
elif today=='Monday' or today=='Tuesday':
	print('eat banana.')
elif today=='Wednesday':
	print('eat cherry.')
elif today=='Thursday':
	print('eat mango.')
else:
	print('eat nothing.')

Output

eat banana.

The first condition is false because today is not equal to 'Sunday'. The next condition is true because today is equal to 'Monday'. Python prints the corresponding message and skips the remaining branches.

2. Use Python elif without an else block

The else block is optional. When it is omitted and every condition is false, Python skips the entire conditional chain and continues with the next statement after it.

In the following example, the if-elif statement does not contain an else block.

Python Program

</>
Copy
today='Monday'

if today=='Sunday':
	print('eat apple.')
elif today=='Monday' or today=='Tuesday':
	print('eat banana.')
elif today=='Wednesday':
	print('eat cherry.')
elif today=='Thursday':
	print('eat mango.')

Output

eat banana.

3. Classify a number with if, elif, and else

This example determines whether a number is positive, negative, or zero.

</>
Copy
number = -8

if number > 0:
    print("The number is positive.")
elif number < 0:
    print("The number is negative.")
else:
    print("The number is zero.")

Output

The number is negative.

The three branches represent mutually exclusive cases. A number cannot be positive, negative, and zero at the same time, so an if-elif-else chain is appropriate.

4. Check a score range with ordered elif conditions

The order of conditions matters when ranges overlap. More restrictive or higher-value conditions should usually appear before broader conditions.

</>
Copy
score = 76

if score >= 90:
    grade = "A"
elif score >= 75:
    grade = "B"
elif score >= 60:
    grade = "C"
elif score >= 40:
    grade = "D"
else:
    grade = "F"

print(grade)

Output

B

The value 76 does not satisfy score >= 90, but it satisfies score >= 75. Python assigns "B" and does not check the remaining conditions.

5. Combine conditions inside an elif branch

An elif condition can use comparison operators, membership tests, and logical operators such as and, or, and not.

</>
Copy
age = 19
has_id = True

if age < 18:
    print("Entry denied: minimum age not met.")
elif age >= 18 and has_id:
    print("Entry allowed.")
else:
    print("Entry denied: identification required.")

Output

Entry allowed.

Why elif condition order matters in Python

Python stops at the first true condition. Therefore, placing a broad condition before a narrower condition can make the narrower branch unreachable.

Consider this incorrectly ordered example:

</>
Copy
score = 95

if score >= 40:
    print("Pass")
elif score >= 90:
    print("Excellent")

The output is Pass, not Excellent, because score >= 40 is already true. The higher threshold should be checked first.

</>
Copy
score = 95

if score >= 90:
    print("Excellent")
elif score >= 40:
    print("Pass")

Python elif versus multiple if statements

An if-elif-else chain and a series of independent if statements behave differently.

  • Use ifelifelse when only one branch should execute.
  • Use separate if statements when several conditions may independently trigger several actions.

In this example, only the first matching branch runs:

</>
Copy
value = 12

if value > 0:
    print("Positive")
elif value % 2 == 0:
    print("Even")

Output

Positive

Although 12 is both positive and even, the elif branch is skipped after the first condition succeeds. To print both properties, use two independent if statements.

</>
Copy
value = 12

if value > 0:
    print("Positive")

if value % 2 == 0:
    print("Even")

Output

Positive
Even

Nested if statements versus elif

Use elif to test alternative conditions at the same decision level. Use a nested if statement when a second decision should be made only after an outer condition succeeds.

</>
Copy
is_registered = True
age = 20

if is_registered:
    if age >= 18:
        print("Eligible participant")
    else:
        print("Registered but below the age requirement")
else:
    print("Registration required")

Here, the age condition is relevant only when is_registered is true, so nesting expresses the dependency clearly.

Common mistakes with Python elif

Writing else if instead of elif

Python uses the single keyword elif. Writing else if causes a syntax error.

Placing elif before an if statement

An elif block cannot start a conditional structure. It must follow an if block or another elif block.

Adding a condition after else

The else keyword does not accept a condition. Use elif condition: when another condition must be checked.

Using the wrong condition order

When conditions overlap, put the most specific condition first. Otherwise, an earlier broad condition may prevent a later branch from ever running.

Using assignment instead of comparison

Use == to compare values. The assignment operator = cannot be used as a normal comparison inside an if or elif condition.

Python elif frequently asked questions

What does elif mean in Python?

elif means “else if.” It checks another condition when all preceding conditions in the same chain are false.

Can Python have more than one elif statement?

Yes. An if statement can be followed by any number of elif branches. Python checks them in the order in which they are written.

Is else required after elif in Python?

No. The else block is optional. Without it, no branch runs when every if and elif condition is false.

Can elif be used without if?

No. An elif block must be part of a conditional chain that begins with if.

Does Python check every elif condition?

Python checks conditions only until it finds the first true one. After executing that branch, it skips all remaining elif and else blocks in the chain.

Python elif summary

The Python elif statement adds alternative conditions to an if statement. Conditions are evaluated from top to bottom, and only the first matching branch is executed. Use elif when the alternatives are mutually exclusive, order overlapping conditions carefully, and use an optional else block to handle unmatched cases.

In this Python Tutorial, we learned the syntax and execution flow of Python elif, how it differs from multiple independent if statements, and how to use it in practical Python programs.