python学习笔记三.if语句

示例1:

cars = ['audi','bmw','subaru','toyota']
for car in cars:                       #记住要加:
    if car == 'bmw':                   #使用两个等号(==)检查car的值是否为'bmw'
        print(car.upper())
    else:				               #记住要加:
        print(car.title())

条件测试

  • 每条if语句的核心都是一个值为True或False的表达式,被称为条件测试。
  • 检查是否相等时区分大小写:如果大小写无关紧要,而只想检查变量的值,可将变量的值转换为小写,再进行比较
car = 'Audi'
car.lower() == 'audi'                 #将变量的值转换为小写,再进行比较,且不会修改存储在变量car的值
  • 检查是否不相等
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':   #用'!=' 表示不等
    print("Hold the anchovies!")
  • 检查多个条件
    1.使用and检查多个条件
    如果每个测试都通过了,整个表达式就为True
>>> age_0 = 22 
>>> age_1 = 18 
>>> age_0 >= 21 and age_1 >= 21         #为改善可读性可以放在括号里:(age_0 >= 21) and (age_1 >= 21)
False 

2.使用or检查多个条件
只要至少有一个条件满足,就能通过整个测试。

>>> age_0 = 22 
>>> age_1 = 18 
>>> age_0 >= 21 or age_1 >= 21 
True
  • 检查特定值是否包含在列表中
requested_toppings = ['mushrooms','onions','pineapple']
if 'mushrooms' in requested_toppings:   #可使用关键字in判断特定的值是否已包含在列表中
    print("true")
  • 检查特定值是否不包含在列表中
#检查被禁言用户是否包含在列表中
banned_users = ['andrew','carolina','david']
user = 'marie'
if user not in banned_users:             #使用not in 检查
    print(user.title())
  • 布尔表达式 是条件测试的别名,布尔表达式的结果要么为True,要么为False。

if-else结构

  • if-else语句块类似于简单的if语句,但其中的else语句
    让你能够指定条件测试未通过时要执行的操作。
#判断年龄是否能参加选举
age = 17
if age >= 18:
    print("you are old enough to vote!")
    print("have you registered to vote yet?")
else:
    print("sorry,you are too young to vote.")
    print("please register to vote as soon as you turn 18.")

if-elif-else 结构

  • Python只执行if-elif-else结构中的一个代码块,它依次检查每个条件测试,直到遇到通过了的条件测试。
age = 3
if age < 4:
    print("your admission cost is free!")
elif age < 18:
    print("your admission cost is $5")
else :
    print("your admission cost is $10")

或者:

age = 12 
if age < 4: 
	price = 0    				 #简化代码
elif age < 18: 
	price = 5 
else: 
	price = 10 
print("Your admission cost is $" + str(price) + ".")

使用多个 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) + ".")

省略 else 代码块

  • else是一条包罗万象的语句,只要不满足任何if或elif中的条件测试,其中的代码就会执行,这可能会引入无效甚至恶意的数据。如果知道最终要测试的条件,应考虑使用一个elif代码块来代替else代码块。这样就可以肯定仅当满足相应的条件时,代码才会执行。
age = 12 
if age < 4: 
	price = 0 
elif age < 18: 	
	price = 5 
elif age < 65:
	price = 10
elif age >=65
	price = 5
print("Your admission cost is $" + str(price) + ".")

测试多个条件

有时候必须检查你关心的所有条件。在这种情况下,应使用一系列不包含elif和else代码块的简单if语句。在可能有多个条件为True,且你需要在每个条件为True时都采取相应措施时,适合使用这种方法。

使用if语句处理列表

1.检查特殊元素是否在列表:for ()in ():
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese'] 
for requested_topping in requested_toppings:  #检查元素是否在列表的语句
	print("Adding " + requested_topping + ".") 
print("\nFinished making your pizza!")

考虑青椒用完的情况:

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
    if requested_topping == 'green peppers':  #在青椒用完的情况下判断是否在需求中加入了青椒
        print("Sorry, we are out of green peppers right now.")
    else:
        print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")

2.确定列表不为空的:

requested_toppings = [] 
if requested_toppings:   #判断需求列表是否为空
	for requested_topping in requested_toppings: 
		print("Adding " + requested_topping + ".") 
		print("\nFinished making your pizza!") 
else: 
	print("Are you sure you want a plain pizza?")

3.使用多个列表

available_toppings = ['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheesse']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested_toppings :
    if requested_topping in available_toppings :
        print("Adding " + requested_topping + ".")
    else:
        print("Sorry, we don't have " + requested_topping + ".")
print("\nfinishing making your pizza!")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值