zoukankan      html  css  js  c++  java
  • if 语句

    if语句执行有个特点,它是从上往下判断,如果在某个判断上是True,把该判断对应的语句执行后,就忽略掉剩下的elif和else

    if 判断语句

    players = ['charles', 'martina', 'michael', 'florence', 'eli']
    for x in players:
        if x == "eli":
            print(x)
    
    ##注意缩进
    

    条件测试


    is检查两个对象是否相同(而不是相等)。
    ==用来检查两个对象是否相等,而is用来检查两个对象是否相同(是同一个对象)。

    1,将一个变量的当前值同特定值进行比较:
    ==
    2,检查是否相等时不考虑大小写
    两个大小写不同的值会被视为不相等

    >>> car = 'Audi'
    >>> car == 'audi'
    False 
    

    3,如果大小写无关紧要,而只想检查变量的值

     >>> car = 'Audi'
    >>> car.lower() == 'audi'
    True
     >>> car
    'Audi' 
    

    4,不相等 !=

    5,比较数字

    >>> age = 18
    >>> age == 18
    True 
    
    
    !=
    <
    <=
    >=
    >
    

    6,检查多个条件

    ##1. 使用and检查多个条件
    检查是否两个条件都为True,可使用关键字and将两个条件测试合而为一;如果每个测试都通过了,整个表达式就为True;如果至少有一个测试没有通过,整个表达式就为False.
    
     >>> age_0 = 22
    >>> age_1 = 18
    >>> age_0 >= 21 and age_1 >= 21
    False
    >>> age_1 = 22
    >>> age_0 >= 21 and age_1 >= 21
    True 
    
    或:
    (age_0 >= 21) and (age_1 >= 21) 
    
    ##2,使用or检查多个条件
    关键字or也能够让你检查多个条件,但只要至少有一个条件满足,就能通过整个测试
    
    
    
    

    7,检查特定值是否包含在列表中
    执行操作前必须检查列表是否包含特定的值,判断特定的值是否已包含在列表中,可使用关键字in

    players = ['charles', 'martina', 'michael', 'florence', 'eli']
    "eli" in players
    

    8,检查特定值是否不包含在列表中
    not in

    banned_users = ['andrew', 'carolina', 'david']
    user = 'marie'
     if user not in banned_users:
         print(user.title() + ", you can post a response if you wish.") 
    

    8,布尔表达式:条件测试的别名
    布尔表达式的结果要么为True,要么为False

    game_active = True
    can_edit = False
    

    if 语句

    if-else

    age = 22
    if age > 20 :
        print("age is" + str(age))
    else:
        print("kskksks")
    
    

    if-elif-else

    age = 12
    if age < 4:
     print("Your admission cost is $0.")
    elif age < 18:
     print("Your admission cost is $5.")
    else:
     print("Your admission cost is $10.") 
    
    或:
    
    if age < 4:
       price = 0
    elif age < 18:
       price = 5
    else:
       price = 10
       print("Your admission cost is $" + str(price) + ".") 
    
    

    多个elif 语句块

    age = 12
    if age < 4:
       price = 0
    elif age < 18:
        price = 5
    elif age < 65:
        price = 10
     else:
        price = 5
    

    Python并不要求if-elif结构后面必须有else代码块:
    else是一条包罗万象的语句,只要不满足任何if或elif中的条件测试,其中的代码就会执行
    如果知道最终要测试的条件,应考虑使用一个elif代码块来代替else代码块。这样,你就可以肯定,仅当满足相应的条件时,你的代码才会执行.

    使用 if 语句处理列表

    requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
    for requested_topping in requested_toppings:
        if requested_topping == 'green peppers':
            print("Sorry, we are out of green peppers right now.")
        else:
            print("Adding " + requested_topping + ".")
    print("
    Finished making your pizza!")
    

    1,确定列表不为空
    在运行for循环前确定列表是否为空很重要
    在if语句中将列表名用在条件表达式中时,Python将在列表至少包含一个元素时返回True,并在列表为空时返回False

    requested_toppings = []
    if requested_toppings:
     for requested_topping in requested_toppings:
         print("Adding " + requested_topping + ".")
         print("
    Finished making your pizza!")
    else:
     print("Are you sure you want a plain pizza?") 
    

    2,使用多个列表

    设置if 的语句格式

    在条件测试的格式设置方面,PEP 8提供的唯一建议是,在诸如==、>=和<=等比较运算符两边各添加一个空格,例如,if age < 4:要比if age<4:好。
    这样的空格不会影响Python对代码的解读,而只是让代码阅读起来更容易。

    input

    input()返回的数据类型是str,str不能直接和整数比较,先把str转换成整数。Python中 int()函数来完成
    ss = input("input a number:")
    xx = int(ss)

    断言

    if语句有一个很有用的“亲戚”,其工作原理类似于下面的伪代码:
    if not condition:
    crash program

    没有达到预定结果就报错

    >>> age = 10
    >>> assert 0 < age < 100
    >>> age = -1
    >>> assert 0 < age < 100
    Traceback (most recent call last):
     File "<stdin>", line 1, in ?
    AssertionError 
    

    如果知道必须满足特定条件,程序才能正确地运行,可在程序中添加assert语句充当检查点
    在条件后面添加一个字符串,对断言做出说明

    >>> age = -1
    >>> assert 0 < age < 100, 'The age must be realistic'
    Traceback (most recent call last):
     File "<stdin>", line 1, in ?
    AssertionError: The age must be realistic 
    
    

    列表推倒

    是一种从其他列表创建列表的方式,类似于数学中的集合推导

    >>> [x * x for x in range(10)]
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    
    
    >>> [x*x for x in range(10) if x 3 == 0] %
    [0, 9, 36, 81] 
    
    
    >>> [(x, y) for x in range(3) for y in range(3)]
    [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] 
    
    
    
    >>> squares = {i:"{} squared is {}".format(i, i**2) for i in range(10)}
    >>> squares[8]
    '8 squared is 64'
    

    使用 exec 和 eval 执行字符串及计算其结果

    exec

    函数exec将字符串作为代码执行,函数exec主要用于动态地创建代码字符串

    >>> exec("print('Hello, world!')")
    Hello, world! 
    

    eval

    eval是一个类似于exec的内置函数。exec执行一系列Python语句,而eval计算用字符串表示
    的Python表达式的值,并返回结果(exec什么都不返回,因为它本身是条语句)

    >>> eval(input("Enter an arithmetic expression: "))
    Enter an arithmetic expression: 6 + 18 * 2
    42 
    
  • 相关阅读:
    js 图片转base64上传图片
    小程序 分享之后,从分享点进去 input里面中文值被转化成字符,需再转化成中文方法
    uni-app map组件的marker
    Python小练习003
    Python小练习002
    Python小练习001
    耶鲁大学——心理学导论(这就是你的大脑)
    ORACLE 创建新表
    键盘事件
    VIDEO当前视频的总长度和视频进度
  • 原文地址:https://www.cnblogs.com/g2thend/p/11735178.html
Copyright © 2011-2022 走看看