Python else Statement
elseis a keyword in Python used to handle all other cases.
elseThe statement is used withifandelifWhen all previous conditions are not met, executeelsethe code in the code block.
Word Definition: elseMeans "otherwise", indicating the situation when all conditions are not met.
Basic Syntax and Parameters
elseMust be used withifas the closing of the entire conditional judgment structure.
Syntax Format
if 条件:
代码块1
else:
代码块2
Syntax Explanation
- Position:
elseMust be the last branch of the if-elif chain. - No condition required:
elseNo conditional expression needs to follow. - Will definitely execute: If all previous conditions are not met, the else code block will definitely execute.
Examples
Example 1: Basic Usage
Example
# Simple either-or choice
age = 15
if age >= 18:
print("Adult")
else:
print("Minor")
# Even/odd number check
num = 7
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
age = 15
if age >= 18:
print("Adult")
else:
print("Minor")
# Even/odd number check
num = 7
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
Expected output:
未成年人 7 是奇数
Code analysis:
- age = 15, does not satisfy age >= 18, executes the else branch.
- 7 % 2 = 1, not equal to 0, executes the else branch.
Example 2: Used with elif
Example
# Complete if-elif-else structure
score = 55
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"Score: {score}, Grade: {grade}")
# Output: Score: 55, Grade: F
score = 55
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"Score: {score}, Grade: {grade}")
# Output: Score: 55, Grade: F
Expected output:
分数: 55, 等级: F
else serves as the final "fallback" branch, handling all cases that do not satisfy the previous conditions.
Example 3: Handling Exception Cases
Example
# Login verification example
username = "admin"
password = "wrong"
if username == "admin" and password == "123456":
print("Login successful")
else:
print("Login failed, please check your username or password")
# Handle the case where the list is empty
items = []
if items:
print(f"The list has {len(items)} elements")
else:
print("The list is empty")
# else in file operations
try:
result = 10 / 2
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print(f"Calculation result: {result}")
username = "admin"
password = "wrong"
if username == "admin" and password == "123456":
print("Login successful")
else:
print("Login failed, please check your username or password")
# Handle the case where the list is empty
items = []
if items:
print(f"The list has {len(items)} elements")
else:
print("The list is empty")
# else in file operations
try:
result = 10 / 2
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print(f"Calculation result: {result}")
Expected output:
登录失败,请检查用户名或密码 列表为空 计算结果: 5.0
else can also be used in try-except structures, representing the handling when there is no exception.
Other Extensions
Python3 Conditional Control