Welcome to our lesson on Elif Statements! This topic is crucial because it allows you to create more complex and dynamic conditional statements in your Python code. By the end of this lesson, you'll be able to use elif
effectively in your scripts, improving their functionality and readability.
An Elif statement (short for "else if") is a part of a multi-condition if
structure in Python. It lets you check multiple conditions one after another without the need to nest if
statements. The general syntax looks like this:
if condition_1:
# code block for condition_1
elif condition_2:
# code block for condition_2
elif condition_3:
# code block for condition_3
else:
# default code block
The elif
statement tests the specified condition. If it's True
, the corresponding code block is executed, and the rest of the conditions are ignored. The process continues until a condition evaluates to True
or the else
block is reached if none of the conditions are True
.
Let's consider an example where we want to check a user's age and display a message based on their category:
age = 18
if age < 3:
print("You are a baby.")
elif age < 12:
print("You are a child.")
elif age < 18:
print("You are a teenager.")
elif age < 60:
print("You are an adult.")
else:
print("You are a senior citizen.")
In this example, the message "You are a teenager." would be displayed because the user's age is between 13 and 17 (inclusive).
What causes it:
Missing a colon at the end of an elif
statement.
if condition:
# code block for condition
elif:
# incorrect syntax
# code block for other condition
Error message:
File "example.py", line X
elif:
^
SyntaxError: invalid syntax
Solution:
Add a colon at the end of the elif
statement.
if condition:
# code block for condition
elif condition_else:
# corrected syntax
# code block for other condition
Why it happens: Python expects a colon to denote the start of a new code block, and without it, it cannot recognize the elif
statement.
How to prevent it: Always include a colon at the end of your if
, elif
, and else
statements.
What causes it:
Incorrect indentation in the code blocks after an elif
.
if condition:
# correct indentation
print("This block is fine.")
elif condition_else:
# incorrect indentation
print("This block has wrong indentation.")
Error message:
File "example.py", line X
print("This block has wrong indentation.")
^
IndentationError: expected an indented block
Solution:
Correct the indentation of the problematic code blocks so they are aligned with the condition that controls them.
Why it happens: Python uses indentation to determine the structure of your code, and incorrect indentation can lead to errors.
How to prevent it: Ensure consistent and correct indentation throughout your code. A common convention is using four spaces for each level of indentation.
What causes it:
Comparing incompatible types (e.g., comparing a string with an integer).
if "10" < 5:
# incorrect comparison
print("This should not happen.")
Error message:
File "example.py", line X
if "10" < 5:
TypeError: unorderable types: str() < int()
Solution:
Convert the incompatible types to a common one before comparing or use an appropriate comparison operator for strings.
Why it happens: Python uses different rules for comparing different data types, and some comparisons are not possible without explicit conversion or using specific operators (e.g., <
for numbers and <
for strings in lexicographical order).
How to prevent it: Always ensure that you're comparing compatible types or use appropriate comparison operators when working with mixed data types.
elif
statements to improve the readability of your code by avoiding nested if
statements.elif
usage.elif
statements.Next steps: Practice writing your own multi-condition statements with if
, elif
, and else
to reinforce your understanding of this topic. As you gain experience, consider exploring other advanced Python topics like functions, classes, or modules. Happy coding!