if, elif and else
The else
statement in Python is used to define a block of code that will execute if
none of the preceding if
or elif
conditions are True
. The else
statement provides a fallback mechanism in your conditional logic, ensuring that your program can handle cases that don’t match any of your specified conditions.
How else Works
An else
statement is typically placed at the end of an if-elif chain. When Python encounters an if-elif-else structure:
- It first evaluates the
if
condition. IfTrue
, the code inside theif
block is executed, and Python skips the rest of the conditions. - If the
if
condition isFalse
, Python moves on to evaluate theelif
conditions in order, when there are any. - If none of the
if
orelif
conditions areTrue
, the code inside the else block is executed.
Examples of else Usage
Here’s a basic example:
a = 15.0
if a > 20 :
print("a is larger than 20")
elif a > 15 :
print("a is larger than 15")
else:
print("a is smaller than 15)
# Output:
# a is larger than b
temperature = 25
if temperature > 30:
print("It's a hot day.")
elif temperature > 20:
print("It's a nice day.")
elif temperature > 10:
print("It's a bit chilly.")
else:
print("It's cold outside.")
# Output:
# It's a nice day.
``
`else` can also directly follow an `if`-statement without `elif`.
```python
a = 15.0
b = 20.0
if a < b:
print("a is smaller than b")
else:
print("a is not smaller than b")
# Output:
# a is smaller than b