Python-5.1-if语句

if语句

  • if语句让你能够检查程序的当前状态,并据此采取相应的措施

一个简单示例

#if语句案例
#遍历变量a中列表的元素,如果有元素是apple时,则以大写打印出。其余全部首字母大写打印出
a = ['apple','banana']
for b in a:
    if b == 'apple':
        print(b.upper())
    else:
        print(b.title())

APPLE
Banana
Tiger

条件测试

  • 检查是否相等
    • 每条if语句的核心都是一个值为True或False的表达式,这种表达式被称为条件测试
#检查是否相等
a = 'apple'
a == 'apple'

True

  • 检查是否相等时考虑大小写
    • 在Python中检查是否相等时区分大小写
#检查时区分大小写
a = 'apple'
a == 'Apple'

False

  • 检查是否不相等
    • 要判断两个值是否不等,可结合使用惊叹号和等号(!=),其中惊叹号表示不
#检查是否不相等
a = 'apple'
a != 'banana'

True

  • 比较数字
    • 等于 ( == )
    • 大于 ( > )
    • 小于 ( < )
    • 大于等于 ( >= )
    • 小于等于 ( <= )
#比较数字
a = 21
a >= 20

True

  • 检查多个条件
    • 使用and检查多个条件
      • 要检查是否两个条件都为True,可使用关键字and将两个条件测试合而为一
    • 使用or检查多个条件
      • 要检查是否有一个条件为True,可使用关键字or
#关键字and检查条件
age_0 = 18
age_1 = 20
age_0 > 16 and age_1 > 19

#关键字or检查条件
age_0 = 18
age_1 = 20
age_0 > 18 or age_1 > 19

True
True

  • 检查特定值是否包含在列表中
    • 有时候,执行操作前必须检查列表是否包含特定的值
#检查特定值是否包含在列表中
a = ['apple','banbna']
'apple' in a

True

  • 检查特定值是否不包含在列表中
    • 确定特定值是否不包含在列表中
#检查特定值是否不包含在列表中
a = ['apple']
if 'banana' not in a:
    print("None")

None

  • 布尔表达式
    • 布尔表达式的结果要么为True,要么为False
#布尔表达式
game_active = True
web_edit = False

if语句

  • if-else语句
    • 在条件测试通过了时执行一个操作,并在没有通过时执行另一个操作
#if_else案例
a = ['apple']
b = 'apple'
if b in a:
    print(b.title()+ " in it")
else:
    print('None')

Apple in it

  • if-elif-else
    • Python只执行if-elif-else结构中的一个代码块,它依次检查每个条件测试,直到遇到通过了的条件测试
      • 测试通过后,Python将执行紧跟在它后面的代码,并跳过余下的测试
#if-elif-else案例:
#4岁以下免费
#4-18岁收费5元
#18岁(含)以上收费10元
age = 16
if age < 4:
    print("Free of charge")
elif age < 18:
    print("You should pay for 5 RMB")
else :
    print("You should pay for 10 RMB")

print('\n')
age = 50
if age < 4:
    price = 0
elif age <18:
    price = 5
else:
    price = 10
print("You should pay for " + str(price) +" RMB")

You should pay for 5 RMB

You should pay for 10 RMB

  • 使用多个elif
    • 可根据需要使用任意数量的elif代码块
#使用多个elif
age = 60
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 50:
    price = 8
else :
    price = 6
print("You should pay for " + str(price) +" RMB")

You should pay for 6 RMB

  • 省略else代码块
    • Python并不要求if-elif结构后面必须有else代码块
#省略else代码块
age = 55
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 50:
    price = 10
elif age < 60:
    price = 8
elif age >= 60:
    price
print("You shoule pay for " + str(price) + " RMB")

You shoule pay for 8 RMB

  • 测试多个条件
    • 检查多个条件应使用一系列不包含elif和else代码块的简单if语句
#测试多个条件
fruits = ['apple','banana']
if 'apple' in fruits:
    print('apple')
if 'pear' in fruits:
    print('pear')
if 'banana'in fruits:
    print('banana')
    
print('\n')
fruits = ['apple','banana']
one_fruits = ['apple','banana','pear']
for one_fruit in one_fruits :
    if one_fruit in fruits :
        print(one_fruit + " in")

apple
banana

apple in
banana in

使用if语句处理列表

  • 检查特殊元素
#检查特殊元素
#检查特殊元素
animals = ['tiger','monkey','bear']
for animal in animals:
    if animal == 'tiger':
        print("Tiger is cruel animal")
    else :
        print(animal.title() + " cute  animal")
print('\n')
print("Welcome to zoo")

Tiger is cruel animal
Monkey cute animal
Bear cute animal

Welcome to zoo

  • 确定列表不是空的
#确定列表不是空的
a = []
if a :
    for b in a :
        print("ADD")
    print("Finsh")
else :
    print("None")

None

  • 使用多个列表
#使用多个列表
one_animal = ['tiger','monkey']
two_animal = ['bear','tiger']
for animal in one_animal:
    if animal in two_animal:
        print("one_animal and two_animal common animal is " + animal)
    else :
        print("Sorry,no other common animals")

one_animal and two_animal common animal is tiger
Sorry,no other common animals

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值