Executing When One of Multiple Conditions is Met Using the elif Statement
elif
is short for else if
, and it defines a code block that executes when one of multiple conditions is true.
The elif
is positioned between the if
and else
statements, providing additional conditions to check if the if
condition is false.
Structure of the elif Statement
You can use multiple elif
statements in a single conditional structure.
When one of the elif
conditions is true, the code in that block is executed.
Like other conditional keywords, the elif
keyword always ends with a colon (:
) to indicate the start of a code block.
number = 5 if number > 10: print("The number is greater than 10.") elif number > 5: print("The number is greater than 5 but less than or equal to 10.") elif number > 0: print("The number is greater than 0 and less than or equal to 5.") else: print("The number is 0 or negative.")
When is the elif statement used in Python?
When all conditions are true
When there are no conditions
When one of multiple conditions is true
When the condition is always false
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result