Python中的if语句

目录

for循环中使用if语句 

if in、if no in 

多条件判断:if or、if and

if-else

if-elif-else

if-elif-elif-else


 

for循环中使用if语句 

arr = ["A", "B", "C"]

for item in arr:
    print(item)
print("--------------")
# 等于
for item in arr:
    if item == "A":
        print(item, "arr 中有 A")

print("=============")
# 不等于
for item in arr:
    if item != "F":
        print(item, "不是 F")

if in、if no in 

arr = ["A", "B", "C"]

# 列表存在判断
if "A" in arr:
    print("arr 中有 A")

if "M" not in arr:
    print("arr 中没有 M")

多条件判断:if or、if and

# 多个条件判断
a = 5
b = 3
if a == 5 or b > 6:
    print("a == 5 or b > 6")

if a > 0 and a != 1 and b < 6:
    print("a > 0 and a != 1 and b < 6")

if-else

score = 75

# if-else
if score < 60:
    print("不及格")
else:
    print("及格")

if-elif-else

score = 75

# if-else
if score < 60:
    print("不及格")
elif score >= 60 and score < 90:
    print("良好")
else:
    print("优秀")

if-elif-elif-else

score = 98

# if-else
if score < 60:
    print("不及格")
elif score >= 60 and score < 90:
    print("良好")
elif score > 90 and score < 95:
    print("优秀")
else:
    print("非常优秀")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值