if语句

每条if语句的核心都是一个值为true或false的表达式,这
种表达式被称为条件测试

条件测试

    car='audi'
    car=='bmw'
    1.一个等号是陈述,可解读为“将变量car的值设置为‘audi’”
    2.两个等号是发问,可解读为”变量car的值是‘bmw’吗”
    3.在python中检查是否相等时区分大小写,两个大小写不
       同的值会被视为不相等
    4.检查特定值是否包含在列表中
            if a in 列表:
    5.检查特定值是否不包含在列表中
            if a not in 列表:

if 语句

1.简单的if语句
    if conditional_test:
        do sth
2.if-else语句
    age=17
        if age>=18:
            print("You are old enough to vote!")
            print("Have you registered to vote yet?")
        else:
            print("Sorry,you are too young to vote.")
            print("Please register to vote as soon as you turn 18!")
    3.if-elif-else结构
        - 可根据需要使用任意数量的elif代码块
        - 如果执行一个代码块,就使用if-elif-else结构;要运行多个代
          码块,就使用一系列独立的if语句。
    4.使用多个列表
        available_toppings=['mushrooms','oloves','green peppers',
                                        'pepperoni','pineapple','extra cheese']
        requested_toppings=['mushrooms','green peppers','extra cheese']
        for requested_topping in requested_toppings:
            if requested_topping in available_toppings:
                print("Adding"+requested_topping+".")
            else:
                print("Sorry,we don't have"  + requested_topping + ".")
        print("
Finished making your pizza!")
原文地址:https://www.cnblogs.com/rener0424/p/10069693.html