python学习第五章:如何编写结果为True、False?如何编写if-elif-else?如何高效的利用for循环!

5.1 if语句的一个简单示例

目标任务:cars列表里面使用一个for循环,mitsubishi全大写其余全首字母大写

#输入:
cars = ['subaru','toyota','mitsubishi','honda','suzuki']
for car in cars:
	if car == 'mitsubishi':        
		print(car.upper())
	else: 
		print(car.title())





#输出:
Subaru
Toyota
MITSUBISHI
Honda
Suzuki
[Finished in 129ms]

5.2 条件测试

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

5.2.1 检查值是否相等

        在Python中,检查两个值是否相等通常使用==运算符。这个运算符会比较两个对象的值是否相等,返回TrueFalse注意,在python中检查值是否相等时会区分大小写的,如果相同的值大小写不同则会返回False。

5.2.2 检查值是否不相等

        要判断两个值不相等,可结合使用感叹号和等号(  !=  )

输入:

car = 'honda'
if car != 'toyota':
	print("unlikeness!")


numerical_value = 10
if numerical_value != 20:   #数值比较时可以把!= 换做小于、小于等于、大于、大于等于、恒等于
	print("error")

输出:

unlikeness!
error
[Finished in 59ms]

5.2.3 检查多个条件(and和or)

1)and是并列的值都满足条件时才输出True否则为False。

2)or是只要有一个条件满足,就能通过测试即为True,如果多个条件都没满足则为False。

5.2.4 检查特定值是否包含在列表中

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

输入:

cars = ['subaru','toyota','mitsubishi','honda','suzuki']
car = "mazda"
if car != cars:
	print("not inside!!!")

输出:

not inside!!!
[Finished in 59ms]

5.2.6 布尔表达式

        布尔表达式可以理解为条件测试的别名,与条件表达式一样,布尔表达式结果要么为True要么为False。

5.3 if语句

5.3.1 简单的if语句

任务:如果你的年龄大于等于18岁则可以去投票了

输入:

age = 20
if age >= 18:
	print("you are old enough to vete!")

输出:

you are old enough to vete!
[Finished in 61ms]

5.3.2 if-else

        if-else语句块类似于if语句,其中的else语句让我们在条件测试时未能满足条件时候的所要执行的操作。举例如下:当我们满了18岁则输出我们可以去投票了否则就不能投票。

输入:

age = 17
if age >= 18:
	print("you are old enough to vete!")
else:
	print("oh no you are too young vote!!")

输出:

oh no you are too young vote!!
[Finished in 62ms]

5.3.3 if-elif-else

        if-elif-else语句块类似于if-else语句,其中的elif语句让我们对于超过两个情形的条件测试。

举例:6岁以下免费,大于6小于18岁收费10元人民币,18元以上收25元人民币。

输入:

age = 6

if age <= 6:
	print("You need to pay 0 RMB")
elif age > 6 and age <18:  #这里其实可以直接(elif age <18: )条件测试会按照顺序执行下来
	print("You need to pay 10 RMB")
else:
	print("You need to pay 25 RMB")

输出:

free!
[Finished in 59ms]

结合之前所学知识可以用f字符串对输入加以改进得到如下代码:

age = 6

if age <= 6:
	price = 0
elif age <18:  #这里其实可以直接(elif age <18: )条件测试顺序执行下来
	price = 10
else:
	price = 25
print(f"You need to pay {price} RMB")

5.3.4 对于if-elif-else中的elif可以使用多个此代码块

        假设我们在上一个例子中再次增加一个条件为达到65岁以上15元人民币。

输入:

age = 99

if age <= 6 :
	price = 0
elif age <18:  
	price = 10
elif age < 65:
	price =25
else:
	price = 15

print(f"You need to pay {price} RMB")

输出:

You need to pay 15 RMB
[Finished in 57ms]

5.3.5 对与else我们其实可以省略替换为elif:

age = 99

if age <= 6 :
	price = 0
elif age <18:  
	price = 10
elif age < 65:
	price =25
elif age >= 65:        ###########这一句
	price = 15

print(f"You need to pay {price} RMB")

5.3.6 测多个条件

不管前一个测试是否通过都会进行下一个代码

输入:

customer_demand = ["cheese" , "sausage" , "pineapple" , "durian"]
if "durian" in customer_demand :
	print("adding durian")
if "durian" in customer_demand :
	print("adding cheese")
if "durian" in customer_demand :
	print("adding sausage")
if "durian" in customer_demand :
	print("adding pineapple")

print("finisheed customer  demand ")

输出:

adding durian
adding cheese
adding sausage
adding pineapple
finisheed customer  demand 
[Finished in 59ms]

5.4 使用if语句处理列表

5.4.1 检查特殊元素

举例:创建一个列表,在其中包含顾客点的配料,并使用一个循环来指出添加到比萨中的配料,如果比萨店菠萝用完了该如何处理:

输入:

customer_demand = ["cheese" , "sausage" , "pineapple" , "durian"]
for demand in customer_demand:
	if demand == "pineapple":
		print("f*ck,There's no pineapple")
	else:
		print(f"adding {demand}")
print("\nYour pizza is finished")

输出:

adding cheese
adding sausage
f*ck,There's no pineapple
adding durian

Your pizza is finished
[Finished in 55ms]

5.4.2 确定列表不是空的

        在制作披萨前检查顾客点的配料列表是否为空,如果列表为空,就向顾客确认是否要点原味披萨,如果列表部位空,就像前面的示例那样制作披萨。

输入:

customer_demand = []
if customer_demand:
	for demand in customer_demand:
		print(f"adding {demand}")
	print("finished making your pizza")
else:
	print("are you sure you want a plain pizza")

输出:

are you sure you want a plain pizza
[Finished in 59ms]

        在if语句中将列表名用作条件表达式时,Python将在列表至少包含一个元素时返回True,并在列表为空时返回False。

5.4.3 使用多个列表

下面是多个列表的举例嵌套

输入:

Menus = ["cheese" , "sausage" , "pineapple" , "durian" , "bacon" , "ham"]
customer_demand = ["cheese","bacon" , "ham" , "apple"]
for demand in customer_demand:
	if demand in Menus:
		print(f"adding{demand}")
	else:
		print(f"sorry,we do not have {demand}")
print("\nYour pizza is finished")

输出:

addingcheese
addingbacon
addingham
sorry,we do not haveapple

Your pizza is finished
[Finished in 77ms]

ok今天就到这里了!明天继续(●'◡'●)

                                 ———— 参考书籍Python编程:从入门到实践(第2版)埃里克·马瑟斯/袁国忠译

                                        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值