Python elif Statement
elifis a keyword in Python for multi-condition judgment; it is an abbreviation of "else if".
When multiple conditions need to be processed,elifit allows us to chain multiple condition checks to form a complete branching structure.
Word Definition: elifis an abbreviation of "else if", meaning "otherwise if".
Basic Syntax and Parameters
elifMust be used withifin combination; cannot exist independently.
Syntax Format
if 条件1:
代码块1
elif 条件2:
代码块2
elif 条件3:
代码块3
else:
代码块4
Syntax Description
- Condition evaluation order: Evaluate sequentially from top to bottom; when the first True condition is encountered, execute the corresponding code block and skip the remaining branches.
- Quantity Limit:
elifThere is no limit on the number; you can add as many as needed. - Optional else:
elseis optional and executes when none of the conditions are satisfied.
Examples
Example 1: Basic Multi-Condition Judgment
Example
# Score grading system
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(f"Grade: {grade}") # Output: Grade: B
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(f"Grade: {grade}") # Output: Grade: B
Expected output:
成绩等级: B
Code analysis:
- score = 85, first check
score >= 90(False)。 - continue checking
score >= 80(True), execute the corresponding code block. - Once a match succeeds, immediately exit the entire if-elif chain.
Example 2: String Matching
Example
# Simple menu selection
choice = "2"
if choice == "1":
print("You selected New File")
elif choice == "2":
print("You selected Open File")
elif choice == "3":
print("You selected Save File")
elif choice == "4":
print("You selected Exit")
else:
print("Invalid choice")
choice = "2"
if choice == "1":
print("You selected New File")
elif choice == "2":
print("You selected Open File")
elif choice == "3":
print("You selected Save File")
elif choice == "4":
print("You selected Exit")
else:
print("Invalid choice")
Expected output:
You chose to open the file
Code analysis:
- elif is used for branching among multiple options.
- Each branch is mutually exclusive; only the first matching condition is executed.
Example 3: Complex Condition Combinations
Example
# Multi-condition combination check
age = 25
income = 5000
if age < 18:
print("Minor")
elif age >= 18 and income < 3000:
print("Adult, low income")
elif age >= 18 and income >= 3000 and income < 10000:
print("Adult, middle income")
elif age >= 18 and income >= 10000:
print("Adult, high income")
# Use elif to implement clearer logic
status = "error"
if status == "success":
print("Operation successful")
elif status == "error":
print("Operation failed")
elif status == "warning":
print("Warning")
elif status == "info":
print("Information")
age = 25
income = 5000
if age < 18:
print("Minor")
elif age >= 18 and income < 3000:
print("Adult, low income")
elif age >= 18 and income >= 3000 and income < 10000:
print("Adult, middle income")
elif age >= 18 and income >= 10000:
print("Adult, high income")
# Use elif to implement clearer logic
status = "error"
if status == "success":
print("Operation successful")
elif status == "error":
print("Operation failed")
elif status == "warning":
print("Warning")
elif status == "info":
print("Information")
Expected output:
Adult, middle income Operation failed
elif makes the code for multi-condition branching clearer and more readable.
Other Extensions