if 语句
且 : and
或: or
属于:in
不属于 : not in
一:if语句
1.if-else语句
# 假设有一个表示某个人年龄的变量,而你想知道这个人是否到了投票的年龄
age=19
if age>18:
print("可以投票")
可以投票
age=18
if age>18:
print("可以投票")
else:
print("不可以投票")
不可以投票
2.if-elif-else语句
可叠加多个elif 模块
可省略else模块
age=15
if age>18:
print("可以投票")
elif age>14:
print("有投票权")
else:
print("不可以投票")
有投票权
3.练习
alien_color='green'
if alien_color=="green":
print("玩家获得了 5 分")
elif alien_color=="yellow":
print("玩家获得了 10 分")
else:
print("玩家获得了 15 分")
玩家获得了 5 分
age=8
if age<4:
print("这个人是幼儿")
elif age>=4 and age<13:
print("这个人是儿童")
elif age>=13 and age<18:
print("这个人是少年")
elif age>=18 and age<65:
print("这个人是中青年人")
else:
print("这个人是老年人")
这个人是儿童
二:使用 if 语句处理列表
1.检查特殊元素
# 比萨店在制作比萨时,每添加一种配料都打印一条消息。
# 如果比萨店的青椒(green peppers)用完了,打印青椒用完
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for dd in requested_toppings:
if dd=="green peppers":
print("青椒用完了")
else:
print(f"添加{dd}") # 格式化输出
添加mushrooms
青椒用完了
添加extra cheese
2.确定列表非空
#制作比萨前检查顾客点的配料列表是否为空。
#如果列表为空,就向顾客确认是否要点原味比萨(plain pizza);
#如果列表非空,就像前面的示例那样制作比萨:
requested_toppings1=[]
if requested_toppings1: # if requested_toppings==[]
print("是否要点原味比萨")
else:
for dd in requested_toppings1:
if dd=="green peppers":
print("青椒用完了")
else:
print(f"添加{dd}") # 格式化输出
requested_toppings1=[]
if requested_toppings1==[]:
print("是否要点原味比萨")
else:
for dd in requested_toppings1:
if dd=="green peppers":
print("青椒用完了")
else:
print(f"添加{dd}") # 格式化输出
是否要点原味比萨
print(bool(requested_toppings1))
False
3.各数据类型真假判断
4.使用多个列表
# 定义了两个列表,其中第一个包含比萨店供应的配料,第二个则包含顾客点的配料。
# 这次对于requested_toppings 中的每个元素,都先检查它是否是比萨店供应的配料,
# 再决定是否在比萨中添加它:
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(f"Adding {requested_topping}")
else:
print(f"we don't have {requested_topping}")
Adding mushrooms
we don't have french fries
Adding extra cheese
5. 练习
names=["xixi","gofy","haha","good","admin"]
for name in names:
if name=="admin":
print("Hello admin, would you like to see a status report?")
else:
print(f"Hello {name}, thank you for logging in again.")
Hello xixi, thank you for logging in again.
Hello gofy, thank you for logging in again.
Hello haha, thank you for logging in again.
Hello good, thank you for logging in again.
Hello admin, would you like to see a status report?
names=[]
if names:
for name in names:
if name=="admin":
print("Hello admin, would you like to see a status report?")
else:
print(f"Hello {name}, thank you for logging in again.")
else:
print("We need to find some users!")
We need to find some users!
current_users=["xixi","gofy","haha","good","admin"]
new_users=["xixi","gofy","Cc","bb","aa"]
copy_new_users=[user.lower() for user in new_users] # 列表推导式
for new_user in copy_new_users:
if new_user in current_users:
print("用户名已使用")
else:
print("用户名未使用")
用户名已使用
用户名已使用
用户名未使用
用户名未使用
用户名未使用
numbers=list(range(1,10))
for number in numbers:
if number==1:
print("1st")
elif number==2:
print("2nd")
elif number==3:
print("3rd")
else:
print(f"{number}th")
1st
2nd
3rd
4th
5th
6th
7th
8th
9th