python基础:4、if语句

#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.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值