if 语句
1. 条件测试/布尔表达式
- 值为True或False的表达式,是if语句的核心
- True/False 首字母大写
- == / != 检查是否 相等/不相等
- 区分大小写
- ==, !=, <, <=, >, >= 比较数字
- 建议比较运算符两边各加一个空格
- and, or检查多个条件
- 为改善可读性,可将每个测试分别放在一对括号内
(age_0 >= 21) and (age_1 >= 21)
- 为改善可读性,可将每个测试分别放在一对括号内
- in / not in 检查特定值是否 包含/不包含 在列表中
2. if 语句
(1) if
age = 19
# 无括号,有冒号
# 若测试通过,将执行if语句后面所有缩进的代码行
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
# 若要运行多个代码块,就使用一系列独立的if语句
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
=>
Adding mushrooms.
Adding extra cheese.
Finished making your pizza!
(2) if-else
age = 17
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
# 前面不缩进,末尾有冒号
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")
(3) if-elif-else
age = 12
if age < 4:
print("Your admission cost is $0.")
# 可根据需要使用任意数量的elif代码块
elif age < 18:
print("Your admission cost is $5.")
# 不要求必须有else代码块
# else包罗万象,可能会引入无效甚至恶意的数据
else:
print("Your admission cost is $10.")
3. 处理列表
(1) 检查特殊元素
在for循环中包含一条if语句
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
# 在for循环中包含一条if语句
if requested_topping == 'green peppers':
print('Sorry, we are out of green peppers right now.')
else:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
=>
Adding mushrooms.
Sorry, we are out of green peppers right now
Adding extra cheese.
Finished making your pizza!
(2) 确定列表不是空的
“if 列表名”:列表不为空返回True,否则返回False
requested_toppings = []
# "if 列表名":列表不为空返回True,否则返回False
if requested_toppings:
for requested_topping in requested_toppings:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
=>
Are you sure you want a plain pizza?
(3) 使用多个列表
available_toppings = ['mushrooms', 'olives', 'green peppers', 'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'French fries', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print("Adding " + requested_topping + ".")
else:
print("Sorry, we don't have " + requested_topping + ".")
print("\nFinished making your pizza!")
=>
Adding mushrooms.
Sorry, we don't have French fries.
Adding extra cheese.
Finished making your pizza!
练习
已有用户名列表为current_users,新用户名列表元素变量为new_user,使用一个if语句不区分大小写地检查出new_user是否已在current_users中存在
current_users = ['Sally', 'Jingru', 'Queena']
new_users = ['SALLY', 'J', 'Lisa']
for new_user in new_users:
# 思路:
# 1. 获取全小写的 current_users 列表以实现“不区分大小写”:[current_user.lower() for current_user in current_users]
# 2. 将 new_user 也变成全小写方式用 in 以检查是否已存在于全小写的 current_users 列表中:if new_user.lower() in [...]
if new_user.lower() in [current_user.lower() for current_user in current_users]:
print("'" + new_user + "'" + ' is an existed username.')
else:
print("'" + new_user + "'" + ' is a valid new username.')
=>
'SALLY' is an existed username.
'J' is a valid new username.
'Lisa' is a valid new username.