Range-based classification
Choose one outcome from ordered ranges, such as assigning a grade from a score.
Python · Const
elif introduces an additional condition in an if statement. Conditions are checked in order, and only the suite for the first true condition executes.
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.
if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite| Name | Type | Description |
|---|---|---|
assignment_expression | expression | A condition evaluated for truth before its associated indented suite runs. |
suite | suite | The indented block of statements controlled by the condition. |
elif means “else if” and provides another conditional branch without adding another nesting level.else suite runs.elif chain begins with an if header whose condition is an assignment expression.elif chain may contain any number of elif clauses, and it does not require an else clause.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.
Choose one outcome from ordered ranges, such as assigning a grade from a score.
Map a value to one of several mutually exclusive categories.
Provide a fallback when no tested condition matches.
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.
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.
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.
ifelseassignment expressionssuiteconditional execution