if语句

1. 一个简单的事例:

如果汽车的名是“audi” 就全部大写打印出汽车的名称,如果不是就首字母大写打印汽车名称

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

 输出

AUDI
Bmw
Subaru
Toyota

1.1 条件测试

1.1.2 检查是否相等

1 >>> car = 'bmw'
2 >>> car == 'bmw'

上述一个等号是赋值,将“bmw”赋值给一个叫car 的变量,两个等号是发问,变量car的值为“bmw”吗

1.1.3  不等号 !=

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

输出

Hold the anchovies!

1.1.4  比较数字

age = 18
print(age == 18)

 输出  True

answer =17
if answer !=42 :
    print("okokok")

 输出  okokok

条件语句中可包含各种数学比较,如小于、小于等于、大于、大于等于:
>>> age = 19
>>> age < 21
True
>>> age <= 21
True
>>> age > 21
False
>>> age >= 21
False

1.1.5  检查多个条件

1. 使用and检查多个条件

要检查是否两个条件都为True,可使用关键字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 

2. 使用or检查多个条件
关键字or也能够让你检查多个条件,但只要至少有一个条件满足

>>> 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 

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

mofa = ["zhangsan","lisi","wangwu","xiaomeng","zhaoliu"]
print(  "xiaomeng" in mofa )

 输出   True

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

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.

1.1.8  布尔表达式

布尔值通常用于记录条件,如游戏是否正在运行,或用户是否可以编辑网站的特定内容:
game_active = True
can_edit = False

在跟踪程序状态或程序中重要的条件方面,布尔值提供了一种高效的方式。

2. if语句

2.1 简单的if语句

最简单的if语句只有一个测试和一个操作:

if conditional_test:
 do something

 在第1行中,可包含任何条件测试,而在紧跟在测试后面的缩进代码块中,可执行任何操作。
如果条件测试的结果为True,Python就会执行紧跟在if语句后面的代码;否则Python将忽略这些
代码。

2.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!")

 输出

Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!

2.3  if-elif-else 结构

在现实世界中,很多情况下需要考虑的情形都超过两个。例如,来看一个根据年龄段收费的
游乐场:
  4岁以下免费;
  4~18岁收费5美元;
 18岁(含)以上收费10美元。
如果只使用一条if语句,如何确定门票价格呢?

age = 12
1 if age < 4:
 print("Your admission cost is $0.")
2 elif age < 18:
 print("Your admission cost is $5.")
3 else:
 print("Your admission cost is $10.")

 输出   Your admission cost is $5.

1处的if测试检查一个人是否不满4岁,如果是这样,Python就打印一条合适的消息,并跳
过余下的测试。2处的elif代码行其实是另一个if测试,它仅在前面的测试未通过时才会运行。
在这里,我们知道这个人不小于4岁,因为第一个测试未通过。如果这个人未满18岁,Python将
打印相应的消息,并跳过else代码块。如果if测试和elif测试都未通过,Python将运行3处else
代码块中的代码。

代码可以这样,更加简洁

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

 2.4   使用多个 elif 代码块

可根据需要使用任意数量的elif代码块,例如,假设前述游乐场要给老年人打折,可再添加
一个条件测试,判断顾客是否符合打折条件。下面假设对于65岁(含)以上的老人,可以半价(即
5美元)购买门票:

age =77
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.

2.5 省略 else 代码块

age =77
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)+".")

 else是一条包罗万象的语句,只要不满足任何if或elif中的条件测试,其中的代码就会执行,
这可能会引入无效甚至恶意的数据。如果知道最终要测试的条件,应考虑使用一个elif代码块来
代替else代码块。这样,你就可以肯定,仅当满足相应的条件时,你的代码才会执行。
2.6  测试多个条件

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(" Finished making your pizza!")

 输出

Adding mushrooms.
Adding extra cheese.
Finished making your pizza!

3 . 使用if语句处理列表

假如我们在制作披萨需要很多的材料

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese'] 
for requested_topping in requested_toppings: 
 print("Adding " + requested_topping + ".") 
print("
Finished 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("
Finished making your pizza!") 

 当列表遍历到青椒的时候 就要告诉我们Sorry, we are out of green peppers right now.

输出

Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding extra cheese.
Finished making your pizza!

3.1 确定列表不是空的

原文地址:https://www.cnblogs.com/cuixiaomeng/p/10383573.html