if语句

if语句

条件测试

每条if语句的核心都是一个值为True或者False的表达式,这种表达式被称为条件测试。根据条件测试的值为True还是False来决定是否执行if语句中的代码。如果条件测试的值为true则执行紧跟在if语句后面的代码,如果为false则忽略这些代码

检查是否相等

#相等运算符==在它两边值相等时返回True,否则返回False
x='bmw'
x=='bmw'
True
#在检查是否相等时区分大小写,两个大小写不同的值会被视为不相等
x='bmw'
x=='bmw'
True
x=='BMW'
False
#如果大小写无关紧要只是想检查变量的值时,可以将变量的值转换为小写在进行比较。使用.lower()不会修改存储在变量x中的值,因此进行比较时不会影响原来的变量
x='AUDI'
x.lower()=='audi'
True

检查是否不等

#!表示不 所以不等符为!=
requested_drink='coca cola'
if requested_drink !='coffee':
    print('Hold the coffee!')
    
Hold the coffee!

使用and检查多个条件
要检查是否两个条件都为True可使用关键字and将两个条件测试合二为一:如果每个测试都通过了则整个表达式为True,如果至少有一个测试没通过则整个表达式为False

age1=22
age2=3
age1>50 and age2>60
False
age1<30 and age2>1
True

使用or检查多个条件
至少一个条件满足就能通过,仅当两个测试都没通过时才为false

age1=22
age2=3
age1>50 or age2>2
True

检查特定值是否包含在列表中
使用关键字in能够检查列表中是否包含特定的值

requested_drink=['coffee','tea','cocacola']
'water' in requested_drink
False

检查特定值是否不包含在列表中
使用关键字not in

banned_words = ['fuck', 'asshole', 'shit']
comment = 'fuck'
if comment not in banned_words:
    print(comment)
else:
    print('your comment is not allowed')


if语句

最简单的if语句

最简单的if语句只有一个测试和一个操作。如果测试通过,则执行if语句后面所有缩进的代码行,否则忽略他们。在紧跟在if语句后面的代码块中可以包含任意数量的代码行

age = 22
if age >= 18:
    print('you are old enough to vote')
    print('have you registered yet?')


you are old enough to vote
have you registered yet?

if-else语句

在条件测试通过时执行一个操作,而在没有通过时执行另一个操作。适用于只存在两种情况的时候

age = 6
if age >= 18:
    print('you are old enough to vote')
    print('have you registered yet?')
else:
    print('sorry,you are too young to vote')
    print('please register until you are 18')


sorry,you are too young to vote
please register until you are 18

if-elif-else语句

依次检查每个条件测试,直到遇到通过了的条件测试。测试通过后,执行紧跟在它后面的代码,并跳过余下的测试。适用于检查超过两个的情形

# 根据年龄段收费 4岁以下免费 4-18岁收费5美元 18岁或以上收费10美元
age = 38
if age < 4:
    print('your cost is $0')
elif age < 18:
    print('your cost is $5')
else:
    print('your cost is $10')



your cost is $10

#可以根据需要使用任意数量的elif代码块
# 根据年龄段收费 4岁以下免费 4-18岁收费5美元 18岁或以上收费10美元 65岁或以上的老人可以打半价即5美元
age = 88
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
else:
    price = 5
print('your cost is $' + str(price))



your cost is $5


#省略else代码块——在某些情况下,使用elif处理特定的情形更清晰。因为else是一条包罗万象的语句,只要不满足任何if或者elif中的条件测试,else后面的代码就会被执行,这可能引入无效甚至恶意的数据,如果知道最终要测试的条件,应考虑使用一个elif代码块代替else代码块,这样就可以肯定仅当通过了相应的测试时代码才会执行

age = 88
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
elif age>=65:
    price = 5
print('your cost is $' + str(price))

一系列独立的if语句

在可能有多个条件为True且需要在每个条件为True时执行相应操作时,适合使用一系列独立简单的if语句

requested_toppings = ['mushroom', 'extra cheese']
if 'mushroom' in requested_toppings:
    print('Adding mushroom')
if 'sausage' in requested_toppings:
    print('Adding sausage')
if 'extra cheese' in requested_toppings:
    print('Adding extra cheese')

print('finished making your pizza')


Adding mushroom
Adding extra cheese
finished making your pizza
#使用三个独立的简单if语句,不管前一个测试是否通过结果如何,都会进行三个独立的测试。如果使用if-elif-else结构,则只要有一个测试通过后就会跳过余下的测试

使用if语句处理列表

#检查列表中的特殊值并对其做合适的处理
requested_toppings = ['mushroom', 'extra cheese']
for x in requested_toppings:
    if x == 'mushroom':
        print('sorry,we are out of mushroom right now')
    else:
        print('Adding ' + x)
print('finished making your pizza')



#确定列表是不是空的——在if语句中将列表名字用在条件表达式中时,python将在列表至少包含一个元素时返回True,在列表为空时返回False
requested_toppings = []
if requested_toppings:
    for x in requested_toppings:
        print('Adding ' + x)
    print('finished making your pizza')
else:
    print('Are you sure you want a plain pizza?')


Are you sure you want a plain pizza?


#使用多个列表
available_toppings = ['mushroom', 'olives', 'green peppers', 'extra cheese', 'pineapple']
requested_toppings = ['mushroom', 'french fries']
for x in requested_toppings:
    if x in available_toppings:
        print('Adding ' + x)
    else:
        print('sorry we dont have ' + x)
print('finished making your pizza')




Adding mushroom
sorry we dont have french fries
finished making your pizza


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值