All courses › Programming with Python › Conditionals: if/elif/else
Conditionals: if/elif/else
if, elif and else let the program choose between branches based on conditions. Python checks the branches in order and runs only the first one that is true. Chained comparisons like 5 < x < 10 work directly.
the first true decides which branch runs
Symbols
| conditions checked in order |
Example
x = 18
if x < 4: ... elif x < 7: ... else: print("C")
Since and , "C" is printed.
elif is only checked if all earlier conditions were false — order matters.← while loop · break and continue →