Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

elif

elif introduces an additional condition in an if statement. Conditions are checked in order, and only the suite for the first true condition executes.

How it works

elif adds another condition to an if statement. Python evaluates the conditions in order, executes the suite belonging to the first true condition, and skips the remaining clauses.

Syntax

if assignment_expression:
    suite
elif assignment_expression:
    suite
else:
    suite

Parameters

NameTypeDescription
assignment_expressionexpressionA condition evaluated for truth before its associated indented suite runs.
suitesuiteThe indented block of statements controlled by the condition.

Key points

  • elif means “else if” and provides another conditional branch without adding another nesting level.
  • Branches are tested from top to bottom, and the statement selects at most one suite.
  • Evaluation stops as soon as a true condition is found.
  • When every condition is false, the optional else suite runs.
  • An elif chain begins with an if header whose condition is an assignment expression.

Common mistakes

  • Only the first matching branch runs; later conditions and suites are not evaluated.
  • An elif chain may contain any number of elif clauses, and it does not require an else clause.

Example: Selecting the first matching condition

score = 87

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "D"

print(grade)

Checks conditions from top to bottom and assigns the grade for the first true condition.

When to use it

Range-based classification

Choose one outcome from ordered ranges, such as assigning a grade from a score.

Mutually exclusive routing

Map a value to one of several mutually exclusive categories.

Fallback handling

Provide a fallback when no tested condition matches.

Interview questions

QHow does Python choose which elif branch to execute?

elif is the chained alternative branch of an if statement. Python evaluates the conditions in sequence and executes exactly one suite: the first whose condition is true.

Follow-up: What happens when multiple conditions are true? Only the suite for the earliest true condition executes; later conditions are not evaluated.

QHow many elif clauses can an if statement contain?

The grammar permits zero or more elif clauses after the initial if, followed by an optional else. Each elif has its own condition and suite.

Follow-up: Can an if statement contain only elif clauses? No; an elif must follow an initial if.

QWhat role does else play after an elif chain?

If every tested condition is false, Python executes the else suite when one is present. If there is no else, the whole statement simply has no selected suite to execute.

Follow-up: Does Python evaluate an else condition? No; else has no condition and is selected only after all preceding tests are false.

  • if
  • else
  • assignment expressions
  • suite
  • conditional execution

Source: 8. Compound statements. Checked 2026-10-16.