7. if语句

1 引入

if语句能够让你检查程序的当前状态,并采取相应的措施。
如何检查程序的当前状态呢?我们通过条件测试来检查。

magic_words=('Hogwarts','Hogsmeade','Dumbledore','Grindelwald')

for magic_word in magic_words:
	if magic_word=='Dumbledore':
		print(magic_word.upper())
	else:
		print(magic_word.lower())
----------
hogwarts
hogsmeade
DUMBLEDORE
grindelwald

以上示例中的循环,先检查当前的magic_word是否为Dumledore。如果是,就以全大写的方式输出。如果不是,就以全小写的方式输出。

2 什么是条件测试

条件测试就是一个值只为TureFalse的表达式
if语句引导的条件测试让Python根据条件测试的值为Ture或False来决定是否执行if语句中的代码。
如果值为Ture,就执行紧跟在if语句后的代码。如果指为False,就忽略if语句后的代码。


3 有哪些条件测试

3.1 检查是否相等

使用相等运算符「==」

>>> principle='Dumbledore'
>>> principle=='Dumbledore'   ###
True

相等运算符区分大小写

>>> principle='Dumbledore'
>>> principle=='dumbledore'   ###
False

principle='Dumbledore'

if principle=='Dumbledore':
	print(f"Hogwarts' principle was {principle}")
----------
Hogwarts' principle was Dumbledore

3.2 检查是否不相等

使用不等运算符「!=」

>>> principle='Dumbledore'
>>> principle!='Voldemort'
True

同样的,不等运算符区分大小写


principle='Voldemore'

if principle!='Dumbledore':
	print(f"Hogwarts' principleis isn't {principle}")
----------
Hogwarts' principleis isn't Voldemore

3.3 数值比较

条件测试中包含各种数学比较,包括:等、不等、小于、小于等于、大于、大于等于

>>> 5==6  #等
False
>>> 5!=9  #不等
True
>>> 5<3   #小于
False
>>> 5<=5  #小于等于
True
>>> 5>3   #大于
True
>>> 5>=9  #大于等于
False

3.4 检查特定值是否包含在列表或元组内

使用关键字「in」

>>> magic_words=('Hogwarts','Hogsmeade','Dumbledore','Grindelwald')
>>> 'Hogsmeade' in magic_words
True
>>> 'Harry' in magic_words
False

new_word='ipad'
if new_word in magic_words:
	print(f"{new_word} is a magic word")
else:
	print(f"{new_word} isn't a magic word")
----------
ipad isn't a magic word

3.5 检查特定值是否不包含在列表或元组内

使用关键字「not in」

>>> 'Dumbledore' not in magic_words
False
>>> 'Potter' not in magic_words
True

new_word='ipad'
if new_word not in magic_words:
	print(f"{new_word} isn't a magic word")
else:
	print(f"maybe {new_word} is a magic word")
----------
ipad isn't a magic word

3.7 同时检查多个条件

① 使用关键字「and」同时检查多个条件,全部条件满足

>>> magic_words_1=('Hogwarts','Hogsmeade','Dumbledore','Grindelwald')
>>> magic_words_2=('gamekeeper','broomstick','Expecto Patronum')
>>> word='gamekeeper'
>>> (word in magic_words_1) and (word in magic_words_2)
False
>>> magic_words=('Hogwarts','Hogsmeade','Dumbledore','Grindelwald')
>>> word_1='gamekeeper'
>>> word_2='ipad'
>>> (word_1 in magic_words) or (word_2 in magic_words)
False

② 使用关键字「or」同时检查多个条件,存在条件满足

>>> magic_words_1=('Hogwarts','Hogsmeade','Dumbledore','Grindelwald')
>>> magic_words_2=('gamekeeper','broomstick','Expecto Patronum')
>>> word='gamekeeper'
>>> (word in magic_words_1) or (word in magic_words_2)
True
>>> magic_words=('Hogwarts','Hogsmeade','Dumbledore','Grindelwald')
>>> word_1='gamekeeper'
>>> word_2='ipad'
>>> (word_1 in magic_words) or (word_2 in magic_words)
False

4 布尔表达式

布尔表达式就是条件测试。
布尔表达式的结果只能为TureFalse


5 if语句

不论if语句的机构是什么,总是有下面的原则

  • 如果测试通过,就执行if语句后面所有缩进的代码行。否则,忽略他们。
  • 在一个if结构中中,测试通过,就执行测试后缩进的代码行,并跳过接下来结构中的所有测试。测试不通过,就接着进行下面的测试直到结束。
  • Python并不要求一定要有else代码块

5.1 if结构

包括一个测试,一个操作
if中的测试通过,则执行缩进的代码行。测试不通过,则无任何操作。

magic_words=['Hogwarts','Hogsmeade','Dumbledore','Grindelwald']

new_word='Hogwarts'

if new_word in magic_words:                 ###测试1
	print(f"{new_word} is a magic word")    ###操作1
----------
Hogwarts is a magic word
magic_words=['Hogwarts','Hogsmeade','Dumbledore','Grindelwald']

new_word='ipad'

if new_word in magic_words:                 ###测试1
	print(f"{new_word} is a magic word")    ###操作1
----------


5.2 if-else结构

包括一个测试,两个操作
if中测试通过,则执行缩进的代码行。测试不通过,则执行else后缩进的代码行。

magic_words=['Hogwarts','Hogsmeade','Dumbledore','Grindelwald']

new_word='ipad'

if new_word in magic_words:                  ###测试1
	print(f"{new_word} is a magic word")     ###操作1
else:
	print(f"{new_word} isn't a magic word")  ###操作2

5.3 if-elif-else结构

包括多个测试,多个操作
开始测试

测试不通过,就进行下面的测试。

当某个测试通过时,执行该测试后面缩进的代码行,并跳过接下来的所有测试。

所有测试都不通过,执行else后面缩进的代码行。

考虑如下示例:按年龄段进行收费
▷ 5岁以下免费
▷ 5~18岁收费30元
▷ 18~55岁收费60元
▷ 55岁以上收费40元

visitor_age=45

if visitor_age<=5:              ###测试1
	print("您应该交费0元")      ###操作1
elif visitor_age<=18:           ###测试2
	print("您应该交费30元")     ###操作2
elif visitor_age<=55:           ###测试3
	print("您应该交费60元")     ###操作3
else:
	print("您应该交费40元")
----------
您应该交费60
visitor_age=45

if visitor_age<=5:
	cost=0
elif visitor_age<=18:
	cost=30
elif visitor_age<=55:
	cost=60
else:
	cost=40
print(f"您应该交费{cost}")
----------
您应该交费60

5.4 测试多个if条件

我们希望能够检查每个测试条件,对于测试条件为True的都能进行操作。
而不是当条件测试为Ture时,就跳过接下来的测试。

考虑如下示例
商店可以提供的物品有[‘黑板’,‘平板’,‘插线板’,‘小风扇’,‘水壶’]
客人需购买的物品有[‘本子’,‘黑板’,‘台灯’]
我们需要为客人提供各项需求物品是否有货的回答

方法1:用多个if语句提供多个操作

commodities=['黑板','平板','插线板','小风扇','水壶']
requests=['本子','黑板','台灯']

if '本子' in commodities:
	print("本子有货")
else:
	print("本子没货")

if '黑板' in commodities:
	print("黑板有货")
else:
	print("黑板没货")

if '台灯' in commodities:
	print("台灯有货")
else:
	print("台灯没货")
----------
本子没货
黑板有货
台灯没货

方法2:for语句遍历

commodities=['黑板','平板','插线板','小风扇','水壶']
requests=['本子','黑板','台灯']

for request in requests:
	if request in commodities:
		print(f"{request}有货")
	else:
		print(f"{request}没货")
----------
本子没货
黑板有货
台灯没货

6 用if语句处理列表

6.1 检查特殊元素

考虑如下示例
商店可以提供的物品有[‘黑板’,‘平板’,‘插线板’,‘小风扇’,‘水壶’],其中黑板、水壶是瑕疵物品
客人需购买的物品有[‘本子’,‘黑板’,‘台灯’]
我们需要为客人提供各项需求物品是否有货的回答,以及有瑕疵物品的告知

commodities=['黑板','平板','插线板','小风扇','水壶']
requests=['本子','黑板','台灯']

for request in requests:
	if request in commodities:
		if request=='黑板' or request=='水壶':
			print(f"{request}有货,但是为瑕疵物品,您确定要购买吗?")
		else:
			print(f"{request}有货")
	else:
		print(f"{request}没货")
----------
本子没货
黑板有货,但是为瑕疵物品,您确定要购买吗?
台灯没货

6.2 确定列表不为空

在if语句中将列表名用作条件表达式时,Python在列表至少博涵一个元素时返回True

commodities=['黑板','平板','插线板','小风扇','水壶']
requests=[]

if requests:
	print("您已经下单成功")
else:
	print("您没有下单成功")
----------
您没有下单成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值