Python If语句

简单的if语句

age=19
if age>=10:
	print("You are Old!")

简单的If-else语句

cars=['audi','bmw','subaru','toyota']
for car in cars:
	if car=='bmw':
		print(car.upper())
	else:
		print(car.title())
#结果:
Audi
BMW
Subaru
Toyota

if-elif-else结构

age=10
if age<4:
	print("Your admission cost is $0.")
elif age<18:
	print("Your admission cost is $5.")
else:
	print("Your admission cost is $10.")
检查是否相等
car='bmw'
car=='bmw' #True

检查是否相等时区分大小写

car='Audi'
car=='audi' # False

将变量的值转为小写再比较

car='Audi'
car.lower()=='audi' #True

是否不相等

car='bwm'
if car!='audio':
	print("不相等")

使用and和or检查多个条件

>>> age_1=20
>>> age_2=24
>>> age_1>=22 and age_2>=22
False
>>> age_1>=21 or age_2>=22
True

检查特定值是否不包含再列表中

cars=['audi','bmw','subaru','toyota']
car='jeep'
if car not in cars:
	print("Not in cars!")
# Not in cars!

布尔表达式

can_eat=False
can_move=True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值