码迷,mamicode.com
首页 > 其他好文 > 详细

4.if语句

时间:2021-03-11 10:31:26      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:else   if语句   变量   运算   不同   测试的   遇到   you   ann   

4.1 一个简单示例

cars = [audi,bmw,subaru,toyota]

for car in cars:
     if car == bmw:
         print(car.upper())
     else:
           print(car.title())

#结果如下:
#Audi
#BMW
#Subaru
#Toyota

4.2 条件测试

4.2.1 检查是否相等(==)

大多数条件测试将一个变量的当前值同特定值进行比较。最简单的条件测试检查变量的值是否与特定值相等。

car = bmw
car == bmw

#结果如下:
#True


car = audi
car == bmw

#结果如下:
#False

4.2.2 检查是否相等时忽略大小写

在Python中检查是否相等时区分大小写。例如,两个大小写不同的值被视为不相等。

car = Audi
car == audi
#结果如下:
#False

car = Audi
car.lower() == audi
#结果如下:
#True

4.2.3 检查是否不相等(!=)

requested_topping = mushrooms

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

#结果如下:
#Hold the anchovies!

4.2.4 数值比较

age = 18
age ==18
#结果如下:
#True

answer = 17
if answer != 42:
    print("That is not the correct answer.Please try angin!")
#结果如下:
#That is not the correct answer.Please try angin!

age = 19
age < 12
age <= 21
age > 21
age >= 21

4.2.5 检查多个条件

1.使用and检查多个条件

两个条件都为True,整个表达式为True;如果至少一个测试没有通过,整个表达式就为False。

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检查多个条件

至少有一个条件为True,整个表达式就为True。

age_0 = 22
age_1 = 18
age_0 >=21 and age_1 >=21
#结果如下:
#True

age_0 = 18
age_0 >=21 and age_1 >=21
#结果如下:
#False

4.2.6 检查特定值是否包含在列表中(in)

requested_toppings = [mushrooms,onions,pineapple]
mushrooms in requested_toppings
#结果如下:
#True

pepperoni in requested_toppings 
#结果如下:
#False

4.2.7 检查特定值是否不包含在列表中(not in)

banned_user = [andrew,carolina,david]
user = marie

if user not in banned_user:
     print(f"{user.title()},you can post a response if you wish.")
#结果如下:
#marie,you can post a response if you wish.

4.2.8 布尔表达式

game_active = True
can_edit = False

4.3 if语句

4.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?")

#结果如下:
#You are old enough to vote!
#Have you registered to vote yet?

4.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 to young to vote.")
    print("Please register to vote as soon as you turn 18!")

#结果如下:
#Sorry,you are to young to vote.
#Please register to vote as soon as you turn 18!

4.3.3 if-else-else结构

Python只执行if-else-else结构中的一个代码块。它依次检查每个条件测试,直到遇到通过了的条件测试。测试通过后,Python将执行紧跟在它后面的代码,并跳过余下的测试。

age = 12

if age < 4:
    print("Your admission  cost is $0.")
else age < 18:
    print("Your admission cost is $25.")
else:
    print("Your admission cost is $40.")
#结果如下:
#Your admission cost is $25.

#为了使代码更简洁
age = 12
if age <4: price = 0 else age < 18: price = 25 else: price = 40 print(f"Your admission cost is ${price}.")

4.3.4 使用多个elif代码块

age = 12
if age <4: price = 0 elif age < 18: price = 25 elif age <65: price = 40 else: price = 20 print(f"Your admission cost is ${price}.")

4.3.5 省略else代码块

age = 12

if age < 4:
    price = 0
elif age < 18:
    price = 25
elif age <65:
    price = 40
elif age >=65:
    price = 20

print(f"Your admission cost is ${price}.")

4.3.6 测试多个条件

requested_toppings =[mushrooms,extra cheese]

if mushrooms in requested_toppings:
    print("add mushrooms.")
if pepperoni in requested_toppings:
    print("add pepperoni.")
if extra cheese in requested_toppings:
    print("add extra cheese.‘‘)

print("\nFinished making your pizza!")

#结果如下:
#add mushrooms.
#add extra cheese.

Finished making your pizza!
#使用if-elif-else结构,代码将不能正确运行

requested_toppings =[mushrooms,extra cheese]

if mushrooms in requested_toppings:
    print("add mushrooms.")
elif pepperoni in requested_toppings:
    print("add pepperoni.")
elif extra cheese in requested_toppings:
    print("add extra cheese.‘‘)

print("\nFinished making your pizza!")

#结果如下:
#add mushrooms.

Finished making your pizza!

 4.4 使用if语句处理列表

4.4.1 检查特殊元素

 

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 =[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!

4.4.2 确定列表不是空的

在运行for循环前确定列表是否为空很重要。

requested_toppings = []

if requested_toppings:
    for requested_topping in requested_toppings:
         print(f"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?

4.4.3 使用多个列表

available_toppings = [mushrooms,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!

4.5 设置if语句的格式

在条件测试的格式设置方面,PEP8提供的唯一建议是,在诸如==、>=、<=等比较运算符两边各添加一个空格。

4.if语句

标签:else   if语句   变量   运算   不同   测试的   遇到   you   ann   

原文地址:https://www.cnblogs.com/nana12138/p/14510377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!