python基础知识整理if语句(五)

                                                                               ------ if语句------

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

cars = [‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
for car in cars:
if car == ‘bmw’:
print(car.upper())
else:
print(car.title())

检查是否不相等
requested_topping = ‘mushrooms’
if requested_topping != ‘anchovies’:
print(“Hold the anchovies!”)

检查是否相等
requested_topping = ‘mushrooms’
if requested_topping == ‘anchovies’:
print(“您输入的相等”)
else:
print(‘您输入的不相等’)

检查数字
answer = 17
if answer != 42:
print(“That is not the correct answer. Please try again!”)

检查特定值是否不包含在列表中
banned_users = [‘andrew’, ‘carolina’, ‘david’]
user = ‘marie’
if user not in banned_users:
print(user.title() + “, you can post a response if you wish.”)

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将执行紧跟在它后面的代码,并跳过余下的测试

age = 12
if age < 4:
print(“Your admission cost is $0.”)
elif age < 18:
print(“Your admission cost is $5.”)
else:
print(“Your admission cost is $10.”)

使用多个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 代码块
Python并不要求if-elif 结构后面必须有else 代码块。在有些情况下,else 代码块很有用;
而在其他一些情况下,使用一条elif 语句来处理特定的情形更清晰

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) + “.”)

使用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 right now.”)
else:
print(“Adding " + requested_topping + “.”)
print(”\nFinished making your pizza!")

确定列表是否为空
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?”)

嵌套多个列表理解
available_toppings = [‘mushrooms’, ‘olives’, ‘green peppers’,‘pepperoni’, ‘pineapple’, ‘extra cheese’]
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(”\nFinished making your pizza!”)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值