美文网首页
4Python if语句

4Python if语句

作者: 图南99 | 来源:发表于2021-05-10 09:31 被阅读0次

一、条件测试

每条 if 语句的核心都是一个值为 True 或 False 的表达式,这种表达式被称为 条件测试,常见的比较有以下几种

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

检查是否相等时不考虑大小写:在 Python 中检查是否相等时区分大小写,两个大小写不同的值会被视为不相等

检查是否不相等:要判断两个值是否不等,可结合使用惊叹号和等号( != ),其中的惊叹号表示 不

比较数字:相等或者不相等

检查多个条件:使用 and 检查多个条件,要检查是否两个条件都为 True ,可使用关键字 and 将两个条件测试合而为一;如果每个测试都通过了,整个表达式就为 True :如果至少有一个测试没有通过,整个表达式就为 False

使用 or 检查多个条件:关键字 or 也能够让你检查多个条件,但只要至少有一个条件满足,就能通过整个测试。仅当两个测试都没有通过时,使用 or 的表达式才为 False
检查特定值是否包含在列表中
要判断特定的值是否已包含在列表中,可使用关键字 in
检查特定值是否不包含在列表中
还有些时候,确定特定的值未包含在列表中很重要;在这种情况下,可使用关键字 not in

二、if 语句

2.1简单的if语句

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

if的条件为真,就会执行下面的操作

score=61
if score>=60:
  print("You got a passing grade")

在 if 语句中,缩进的作用与 for 循环中相同。如果测试通过了,将执行 if 语句后面所有缩进的代码行,否则将忽略它们

score=61
if score>=60:
    print("Congratulations on your")
    print("You got a passing grade")

2.2if-else 语句

if-else 语句块类似于简单的 if 语句,但其中的 else 语句让你能够指定条件测试未通过时要执行的操作

score=59
if score>=60:
    print("Congratulations on your")
    print("You got a passing grade")
else:
    print('Sorry, please try harder next time')

2.3 if-elif-else 结构
多个条件判断

score=81
if 80>score>=60:
    print("Congratulations on your")
    print("You got a passing grade")
elif score>=80:
    print("Your grades are excellent")
else:
    print('Sorry, please try harder next time')

2.3使用多个 elif 代码块

score=91
if 80>score>=60:
    print("Congratulations on your")
    print("You got a passing grade")
elif 90>score>=80:
    print("Your grades are average")
elif 90<score<= 100:
    print("Your grades are excellent")
else:
    print('Sorry, please try harder next time')

2.5省略 else 代码块

score=59
if 80>score>=60:
    print("Congratulations on your")
    print("You got a passing grade")
elif 90>score>=80:
    print("Your grades are average")
elif 90<score<= 100:
    print("Your grades are excellent")
print('Sorry, please try harder next time')

2.6检查多个条件

有时候必须检查你关心的所有条件,这个时候教使用多个if语句了

fruit=['grapes','banana','strawberry','apple','orange']
if 'grapes' in fruit:
    print("Please give me some grapes")
if 'banana' in fruit:
    print("Please give me some banana")
if 'strawberry' in fruit:
    print("Please give me some strawberry")

三、使用 if 语句处理列表

3.1检查特殊元素

fruits=['grapes','banana','strawberry','apple','orange','Mango.']
for fruit in fruits:
    if fruit == 'Mango.':
        print("Allergic to mangoes")
    else:
        print('Please give me some'+ ' '+fruit)

3.2确定列表不是空的

fruits=[]
if fruits:
    for fruit in fruits:
        if fruit == 'Mango.':
            print("Allergic to mangoes")
    else:
        print('Please give me some'+ ' '+fruit)
else:
    print("Would you like some fruit?")

3.3使用多个列表

fruits=['grapes','banana','strawberry','apple','orange','tomatoes']

vegetables=['cucumber','tomatoes','cantaloup']

for fruit in fruits:

if fruit in vegetables:

    print(fruit + "is not a fruit")

else:

    print('Please give me some'+ ' '+fruit)

相关文章

网友评论

      本文标题:4Python if语句

      本文链接:https://www.haomeiwen.com/subject/qcfvdltx.html