Python编程从入门到实践~if语句

这篇博客探讨了Python中的条件判断语句,包括检查相等、忽略大小写比较、不相等判断以及多个条件的组合。示例涵盖了if-elif-else结构以及列表操作,如检查列表是否为空和在列表中查找元素。此外,还涉及了布尔表达式的应用。
#检查是否相等
cars = ["bmw", "audi", "toyota", "subaru"]
for car in cars:
	if car == 'bmw':
		print(car.upper())
	else:
		print(car.title())

#忽略大小写是否相等
car = "Audi"
if (car.lower()  == "audi"):
	print(True)


#检查是否不相等
request_topping = 'mushrooms'
if request_topping != 'anchovies':
	print("Hold the anchovies!")


#多个条件
age_0 = 22
age_1 = 18
if age_0 >= 21 and age_1 >= 21:
	print(f"and True")
else:
	print(f"and False")


if age_0 >= 21 or age_1 >= 21:
	print(f"or True")
else:
	print(f"or False")


#布尔表达式
game_active = True
can_edit = False


#if-elif-else
age = 12
if age < 4:
	print("You admission cost is $0.")
elif age < 18:
	print("You admission cost is $25.")
else:
	print("You admission cost is $40.")


#确定列表不是空的
request_topping = []
if request_topping:
	for req in request_topping:
		print(req)
else:
	print("Are you sure you want a plain pizza?")



#使用多个列表
available_toppings = ["mushrooms","olives","green peppers","pepperoni","pineapple","extra cheese"]
request_topping = ["mushrooms","french fries","extra cheese"]
for req in request_topping:
	if req in available_toppings:
		print(f"Adding {req}")
	else:
		print(f"Sorry, we don't have {req}")

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值