第5章--if 语句

5.1 一个简单的示例

cars = ['audi','bmw','subaru','toyota']
for car in cars:
	if car == 'bmw':
		print(car.upper())
	else:
		print(car.title())
  • 结果为
    在这里插入图片描述

5.2 条件测试

5.2.1 检查是否相等

>>> car = 'bmw'
>>> car == 'bmw'
Ture

>>> car = 'audi'
>>> car == 'bmw'
False

5.2.2 检查是否相等是不考虑大小写

  • python 对大小写敏感
>>> car = 'Audi'
>>> car == 'audi'
False
  • 将变量值处理成想要的临时值
>>> car = 'Audi'
>>> car.lower() == 'audi'
True
>>> car
'Audi' 

5.2.3 检查是否不相等

requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
	print("Hold the anchovies")
  • 结果为
    在这里插入图片描述

5.2.4 比较数字

  • 与C 语言逻辑相同

5.2.5 检查多个条件

    1. 使用and检查多个条件
>>> age_0 = 22
>>> age_1 = 18
>>> (age_0 >= 21) and (age_1>= 21)
False 
    1. 使用 or 检查多个条件
>>> age_0 = 22
>>> age_1 = 18
>>> (age_0 >= 21) or (age_1>= 21)
True

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

>>>requested_toppings = ['mushrooms','onins','pineapple']
>>> 'mushrooms' in requested_toppings
True

>>> 'peperoni' in requested_toppings
False

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

  • 使用 if value_name not in 列表名
banned_users = ['andrew','carolina','advid']
user = 'marie'

if user not in banned_users:
	print(user.title() + ", you can post a response if you wish.")
  • 结果为
    在这里插入图片描述

5.2.8 布尔表达式

  • 通常用于记录文件
game_active = True
can_edit = False

5.3 if 语句

5.3.1 简单的if 语句

* 格式:
if conditional_test:
	do something
age = 19
if age >= 18:
	print("YOU are old enough to vote!")
	print("Have you registered to vote yet?")
  • 结果为
    在这里插入图片描述

5.3.2 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!")
  • 结果为
    在这里插入图片描述

5.3.3 if-elif-else 结构

age = 12
if age < 4:
	price = 0
elif age < 18:
	price = 5
else :
	price = 10
print("Your admission cost is $"+ str(price)+ ".")
  • 结果为
    在这里插入图片描述

5.3.4 使用多个elif代码块

	age = 54
	if age < 4:
		price = 0
	elif age < 18:
		price = 5
	elif age < 65:
		price = 7.5
	else :
		price = 10
	print("Your admission cost is $"+ str(price)+ ".")
  • 结果为
    在这里插入图片描述

5.3.5 省略 else 代码块

  • 可以使用if - elif -elif-elif … 结构,末尾的else可省略

5.3.6 测试多个条件

  • 使用多个 if 并列 条件语句,他们同等关系由缩进TAB键确定,
  • 也可以使用if - elif -elif-… 语句 使其连接,只能有一条满足条件并执行一条结果

5.4 使用 if 语句处理列表

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 rightnow.")
	else:
		print("Adding " + requested_topping + ".")
print("\n Finished making your pizza")
  • 结果为
    在这里插入图片描述

5.4.2 确定列表不是空的

requested_toppings = []
# 判断列表是否为空
if requested_toppings:
	for requested_topping in requested_toppings :
		print("Adding " + requested_topping + ".")
else:
	print("are you sure you want a plain pizza?")

  • 结果为
    在这里插入图片描述

5.4.3 使用多个列表

available_toppings = ['mushrooms','olives','green pppers',
					'pepperoni','pineapple','extra cheese']
requested_toppings = ['mushrooms','fresh 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 + ".")
  • 结果为
    在这里插入图片描述

5.5 设置 if 语句的格式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值