栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python if语句

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python if语句

5.1简单示例
cars = {"audi", "bmw", "subaru", "toyota"}
for car in cars:
    if car == "bmw":
        print(car.upper())
    else:
        print(car)

5.2条件测试

5.2.1检查是否相等

car = "audi"
print(car == "BMW")

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

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

car = "Audi"

car == "audi"

5.2.3检查是否不相等

requsted_topping = "mushrooms"
if requsted_topping != "anchovies":
    print("hold the anchovies")                #程序执行结果为两者不相等,输出结果

5.2.4数值比较

age = 18
age ==19

5.2.5检查多个条件

1.使用and检查多个条件

age_0 =22
age_1 =18
print(age_0>=21 and age_1>=21)

2.使用or检查多个条件

age_0 =22
age_1 =18
print(age_0>=21 or age_1>=21)             #有一个为真则为真    

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

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

requsted_topping = ["mushroom", "onions", "pineapple"]
print("onions" in requsted_topping)

结果为True

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

requsted_topping = ["mushroom", "onions", "pineapple"]
print("onions" not in requsted_topping)

5.2.8布尔表达式

布尔表达式不过是测试条件的别名,其结果要么为True,要么为False

game_active = True
can_edit = False

5.3if语句

5.3.1最简单的if语句

age = 18
if age>=19:
    printf("111")

5.3.2if-else语句

age = 17
if age >= 18:
    print("You are old enough to vote")
else:
    print("You are too young to vote")

5.3.3if-elif-else结构

age = 12
if age < 4:
    print("Your admission cost is $0")
elif age < 18:
    print("Your admission cost is 25$")
else:
    print("Your admission cost is $40")

##

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

#这个代码与前面的例子相同,但其结构的作用更小:它只确定门票价格,修订后的代码更容易修改,只需修改一个而不是三个函数调用print()

5.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}")

5.3.5省略else代码块

Python并不要求if-elif后面必须有else代码块。在有些情况下,使用else代码块很有用

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 25
elif age < 65:
    price = 40
elif age > 65:  #else是一条保罗万象的语句,这样写可能会引入无效或者恶意的数据
    price = 20
print(f"Your admission cost is ${price}")

5.3.6测试多个条件

对于满足多个条件的结构,使用if-elif-else的话,一旦遇到满足条件的,就会跳过其余测试

因此对于这种情况,就需要使用一系列独立的if语句。

5.4使用if语句处理特殊列表

5.4.1检查特殊元素

requested_toppings = ["mushrooms", "green peppers", "extra cheese"]
for requested_topping in requested_toppings:
    print(f"Adding {requested_topping}")
print("nFinishing making your pizza!")

#将所需的配料放到列表之中,并使用一个循环来支出添加到披萨中的配料。

然而,当列表中的青椒用完了,该如何处理呢,此时可以添加一个if语句

print(f"Your admission cost is ${price}")
requested_toppings = ["mushrooms", "green peppers", "extra cheese"]
for requested_topping in requested_toppings:
    if requested_topping == "green peppers":
        print("Green peppers has sold out!")
    else:
        print(f"Adding {requested_topping}")
print("nFinishing making your pizza!")

5.4.2确定列表不是空的

在运行之前检查 列表是否为空很重要

requested_toppings = []
if requested_toppings:                #if后面的列表至少包含一个元素时返回True
    for requested_topping in requested_toppings:
        print(f"Adding {requested_topping}")
    print("Finishing making your pizza")
else:                                 #并在列表为空时返回False
    print("Are you sure you want a pizza?")

5.4.3使用多个 列表

available_topping = ["mushrooms", "olives", "green peppers", "pepperoin", "pineapple", "extra cheese"]
requested_toppings = ["mushrooms", "french fries", "extra cheese"]
for requested_topping in requested_toppings:
    if requested_topping in available_topping:
        print(f"Adding {requested_topping}")
    else:
        print(f"Sorry,we don't have {requested_topping}")
print("nFinished making your pizza")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339402.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号