python入门4:条件测试、if语句、if语句处理列表

本文详细介绍了Python中的条件测试、相等与不相等运算符、比较数字的方法,以及如何使用if、elif、else和逻辑运算符(and、or)进行多条件判断。涵盖了列表操作,如检查元素存在性、遍历和空列表的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

条件测试(条件语句)

语句核心:一个True或False的表达式,表达式就被称为条件测试;
相等运算符:作为判断两个值是否一致的标准,==,true就继续运行代码;
区分/不区分大小写的判断:如不需要区分则可先改成小写;
不相等运算符:判断两个值不相等,则为true,!=;
比较数字:>,<,≥,≤,!=,==,一般涉及到的符号;
检查多个条件:≥2个条件都满足执行下一步,()and(),()or();
检查列表中的特定值:关键字in;
检查列表中不存在特定值:关键字 not in; 布尔表达式:结果要么true,要么false;
true或者false的结果也可以用print输出

#相等运算符,判断
you = "idiot"
you == "idiot"

#区分/不区分大小写的判断
car = "Bmw"
car == "bmw"
print(car.lower() == "bmw")

#比较数字
age = 22
age <= 25

#and检查多个条件,都要满足
a = 21
b = 22
(a > 20) and (b < 25)

#or检查多个条件,至少满足一个
a = 21
b = 22
(a > 20) or (b > 25)

#in检查列表中的特定值
a = [1,2,3]
3 in a

#not in检查列表中的特定值
a = [1,2,3]
4 not in a

True

if语句

应用原则:if后面的语句返回true,继续执行缩进的语句;
if-else语句:if不符合,执行else,两种情形;
if-elif-else语句:超过两种情形,可以使用任意数量的elif,else可以无;
if-if语句:多个独立的条件判断,只用if,可执行多个代码块;

a = 9
if a >= 1:
    print("Right!")
    
#if-else简单语句
a = 9
if a >= 10:
    print("Right!")
else:
    print("Wrong-_-")
    
#if-elif-else语句
age = 17
if age <= 4:
    print("the charge is 0.")
elif  4 < age < 18:
    print("the charge is 5 dollars.")
else:
    print("the charge is 10 dollars.")
Right!
Wrong-_-
the charge is 5 dollars.
color = "green"
if color == "green":
    print("The player will get 5 points.")
    
color = "yellow"
if color == "green":
    print("The player will get 5 points.")
else:
    print("The player will get 10 points.")
    
color = "red"
if color == "green":
    print("The player will get 5 points.")
elif color == "yellow":
    print("The player will get 10 points.")
else:
    print("The player will get 15 points.")
    
age = 22
if age < 2:
    print("He is a baby.")
elif 2 <= age < 4:
    print("He is learning walking.")
elif 4 <= age < 13:
    print("He is a child.")
elif 13 <= age < 20:
    print("He is a teenager.")
elif 20 <= age < 65:
    print("He is an adult.")
elif age >= 65:
    print("He is an elder.")
    
fruits = ["orange","mango","apple"]
if "orange" in fruits:
    print("yeah!orange")
if "mango" in fruits:
    print("yeah!mango")
if "banana" in fruits:
    print("yeah!banana")
The player will get 5 points.
The player will get 10 points.
The player will get 15 points.
He is an adult.
yeah!orange
yeah!mango

if语句处理列表

检查特殊元素:加入for循环,进行遍历;
确定列表是不是空的:for循环+if else的搭配;
使用多个列表:for循环+if else的搭配;

#检查特殊元素
fruits = ["orange","mango","apple","banana"]
for fruit in fruits:
    if fruit == "orange":
        print("Sorry, the orange was saled.")
    else:
        print("You can buy " + fruit + ".")

#确定列表为空
fruits = []
if fruits:
    for fruit in fruits:
        print("You can buy " + fruit + ".")#确定列表为空才能执行else内容
else:
    print("We do not have the things you wanna buy.")
    
#两个列表对比检查,有无的元素
available_fruits = ["orange","mango","apple","banana"]
requested_fruits = ["apple","grape"]
for requested_fruit in requested_fruits:
    if requested_fruit in available_fruits:
        print("You can buy " + requested_fruit + ".")
    else:
        print("You can go somewhere to buy " + requested_fruit + ".")
Sorry, the orange was saled.
You can buy mango.
You can buy apple.
You can buy banana.
We do not have the things you wanna buy.
You can buy apple.
You can go somewhere to buy grape.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值