if / elif / else
Runs different code based on a condition.
Syntax
if condition:
...
elif condition:
...
else:
...Description
If the if condition is true, the indented block below it runs. elif specifies other conditions, else specifies the block to run if none match. Indentation (whitespace) is required.
Example
Loading editor…
Expected output
Passed
When to use?
Input validation, grade calculation, branching the flow.
Common mistakes
- ⚠️ A colon (:) after the condition and indentation on the next line are required.
- ⚠️ Use == for equality, not =.