Python编程:从入门到实践(第五章)

if语句

cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
  if car == 'bmw':
   print(car.upper())
else:
  print(car.title()
Audi
BMW
Subaru
Toyota

检查不等( !=)

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

使用and 检查多个条件

 >>> age_0 = 22
>>> age_1 = 18>>> age_0 >= 21 and age_1 >= 21
False
❸ >>> age_1 = 22
>>> age_0 >= 21 and age_1 >= 21
True
>>> age_0 = 22
>>> age_1 = 18>>> age_0 >= 21 or age_1 >= 21
True
❶ >>> age_0 = 18
>>> age_0 >= 21 or age_1 >= 21
False

检查特定值是否包含在列表中
要判断特定的值是否已包含在列表中, 可使用关键字in 。

>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']>>> 'mushrooms' in requested_toppings
True
❷ >>> 'pepperoni' in requested_toppings
False

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

还有些时候, 确定特定的值未包含在列表中很重要; 在这种情况下, 可使用关键字not in 。

banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users://if语句+  not int 语句
print(user.title() + ", you can post a response if you wish.")
Marie, you can post a response if you wish.

if:

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 else 后面的 “ : ”,后面的语句也要缩进

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.")
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) + ".")// str 返回一个字符串

测试多个条件
if-elif-else 结构功能强大, 但仅适合用于只有一个条件满足的情况: 遇到通过了的测试后, Python就跳过余下的测试。 这种行为很好, 效率很高, 让你能够测试一个特定的条件。

然而, 有时候必须检查你关心的所有条件。 在这种情况下, 应使用一系列不包含elif 和else 代码块的简单if 语句。

 requested_toppings = ['mushrooms', 'extra cheese']if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
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?")

在这里, 我们首先创建了一个空列表, 其中不包含任何配料(见❶) 。 在❷处我们进行了简单检查, 而不是直接执行for 循环。 在if 语句中将列表名用在条件表达式中时, Python将在列表至少包含一个元素时返回True , 并在列表为空时返回False 。 如果requested_toppings 不为空, 就运行与前一个示例相同的for 循环; 否则, 就打印一条消息, 询问顾客是否确实要点不加任何配料的普通比萨(见❸) 。在这里, 这个列表为空, 因此输出如下——询问顾客是否确实要点普通比萨:

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!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值