#if语句
cars=["audi","bmw","subaru","toyota"]
for car in cars:
if car == "bmw":
print(car.upper())
else:
print(car.title())
Audi
BMW
Subaru
Toyota
#5.2条件测试:每条if语句的核心都是一个值为True或False的表达式,这种表达式被称为 条件测试。
#5.2.1检查是否相等
car="www"
car=="www"
True
#5.2.2 python中测试区分大小写 大小写不同会被视为不相等 也可改为小写进行比较
car="Audi"
car.lower()=="audi"
True
print(car)
Audi
#5.2.3检查是否不相等
aaa="bbb"
if aaa != "ccc":
print("yeah!")
yeah!
#5.2.4比较数字
age=18
age==18
True
age=18
age<=21
True
#5.2.5检查多个条件 1、and 2、or
age=18
age<21 and age>17
True
age=10
age_1=27
age<11 or age_1<29
True
#5.2.6检查特定值是否在列表里
aaa=["sss","kkk","lll"]
"sss" in aaa
True
aaa=["sss","kkk","lll"]
user="ooo"
if user not in aaa:
print("hhh")
hhh
#布尔表达式
#动手414
#5-2
car="nnn"
car=="mmm"
False
car="nnn"
if car != "mmm":
print("yeah!")
yeah!
car="BMW"
car.lower()=="bmw"
True
age=11
age==22
False
age=11
if age != 12:
print(11)
11
age=11
age>10
True
age=18
age<=18
True
age=11
age_1=90
age<12 or age_1>100
True
age=11
age_1=99
age==11 and age_1==99
True
age=[11,1,3,4]
1 in age
True
age=[11,12,13]
nnn=123
if nnn not in age:
print(999)
999
#if 语句
age=19
if age==19:
print("yeah!")
print("happy")
yeah!
happy
age=90
if age==99:
print("okk")
else:
print("noo")
noo
#if-elif-else
age=13
if age<4:
price=0
elif age<18:
price=5
else:
price=10
print("Your admission cost is $"+str(price)+".")
Your admission cost is $5.
#使用多个elif
age=12
if age<4:
price=0
elif age<18:
price=5
elif age<65:
price=10
else:
price=5
print("Your admission cost is $"+str(price)+".")
Your admission cost is $5.
python基础:4、if语句
最新推荐文章于 2024-10-09 22:10:43 发布