if 语句

cars = ['audi','bmw','subaru','toyota']

for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())
Audi
BMW
Subaru
Toyota

条件测试

  检查是否相等(考虑大小写)

car = 'bmw'       # 一个 = 变量复制
car == 'bmw'      # 两个 = 判断是否相等
True
car = 'audi'
car == 'AUdi'
False

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

car = 'audi'
car == 'AUdi'
False
car = 'Audi'
print(car.lower() == 'audi')
print(car)
True
Audi

  检查是否不相等

requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
    print("Hold the anchovies!")
Hold the anchovies!

  检测多个条件

  and (与),同真为真,其余为假

  如果每个测试值都通过了,整个表达式为True,如果至少有一个测试没有通过,整个表达式为False

age_0 = 22
age_1 = 18 
(age_0 >= 21) and (age_1 >= 21)
False
  or(或),同假为假,其余为真

  只要至少有一个条件满足,就能通过整个测试

age_0 = 22
age_1 = 18 
(age_0 >= 21) or (age_1 >= 21)
True

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

requested_toppings = ['mushrooms','onions','pineapple']
print('mushrooms' in requested_toppings)
print('pepperoni' in requested_toppings)
True
False

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

banned_users = ['andrew','carolina','david']
user = 'marie'
if user not in banned_users:
    print(user.title() + ", you can post a response if you wish.")
Marie, 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!")
Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!

  if-elif-else结构

  依次检查每个条件测试,直到遇到通过了的条件测试,测试通过后,将执行其后面的代码,跳过余下的测试

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.")
YOur admission cost is $5.

代码简化:

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

  使用多个 elif 代码块

age = 70

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) + ".")
Your admission cost is $5.

  省略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) + ".")
Your admission cost is $5.

使用if语句处理列表

  检查特殊元素

requested_toppings = ['mushrooms','green peppers','extra cheese']

for requested_topping in requested_toppings:
    print("Adding " + requested_topping + ".")

print("\nFinished making your pizza!")
Adding mushrooms.
Adding green peppers.
Adding extra cheese.

Finished 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!")
Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding extra cheese.

Finished 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?")
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!")
Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.

Finished making your pizza!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值