• 《Python编程从入门到实践》_第五章_if语句


     条件测试

    每条if语句的核心都是一个值为Ture或False的表达式,这种表达式被称为为条件测试。Python根据条件测试的值为Ture还是False来决定是否执行if语句中的代码。如果条件测试的值为Ture,Python就执行紧跟在if语句后面的代码;如果为False,Python就忽略这些代码。

    字符串比较

    字符串检查时是区分大小写的
    检查是否相等
    >>> car = "bmw"
    >>> car == "bmw"
    True
    >>> car = "audi"
    >>> car == "bmw"
    False

     检查是否不等

    >>> car = "bmw"
    >>> car != "bmw"
    False
    >>> car != "audi"
    True

     比较数字

    ==

    是否相等
    != 是否不等
    > 大于
    < 小于
    >= 大于等于
    <= 小于等于

     

    >>> age = 18
    >>> age == 18
    True
    >>> age != 19
    True
    >>> age < 21
    True
    >>> age > 21
    False
    >>> age <= 21
    True
    >>> age >= 21
    False

     

    检查多个条件

     and  多个条都成立则为True,否则为False

    >>> age = 19
    >>> age > 18 and age > 17 and age > 16
    True
    >>> age > 20 and age > 17 and age < 20
    False

     or 多个条件成立一个则为True,全部不成立则为False

    >>> age = 19
    >>> age > 18 or age > 20 or age > 100
    True
    >>> age > 100 or age > 20 or age > 200
    False

     in 要判断特定的值是否在已包含的列表中

    >>> name = ['Frank','Alex','Bob']
    >>> 'Frank' in name
    True
    >>> 'May' in name
    False

     not in 要判定特定的值不在包含的列表中

    >>> name = ['Frank','Alex','Bob']
    >>> 'Frank' not in name
    False
    >>> 'May' not in name
    True

     

    布尔表达式

     结果要么真,要么假,True  or  False。

     

    if-n个elif-else结构

    这是最长情况下的if结构了
    最短的情况下,仅仅只是用 if 就行了
    #选举
    age = 19
    if age >= 18:
        print("You are old enough to vote!")
    #运行结果
    You are old enough to vote!

      两种选择情况下的,if-else

    #选举
    age = 17
    if age >= 18:
        print("You are old enough to vote!")
    else:
        print("Sorry,you are too young to vote!")
    #运行结果
    Sorry,you are too young to vote!

    多种情况下if-elif-else,elif可以有多个

    #游乐园卖票
    age = 12
    if age < 4:
        print("You admission cost is $0.")
    elif age < 18:
        print("Your admission cost is $5.")
    else:
        print("Your admission cost is $10.")
    python并不要求if-elif结构后面必须有else代码块,else是一条包罗万象的语句,只要不满足就会执行else下的语句,可能会引起无效甚至恶意的数据,如果知道最终要测试的条件,应该考虑是用elif代替else!
    不管if-n个elif-else,只要满足一个,就只会执行判断为True下面的条件,不会执行其他的。

    python将在列表至少包含一个元素的时候返回True,并在列表为空时返回False!

    name = []
    if name:
        print("Name in")
    else:
        print("No name")
    #运行结果
    No name
    name = ["Frank"]
    if name:
        print("Name in")
    else:
        print("No name")
    #运行结果
    Name in

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    matplotlib油漆基础
    使用ant编译项目技能
    [Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose
    [Recompose] Make Reusable React Props Streams with Lenses
    [Recompose] Compose Streams of React Props with Recompose’s compose and RxJS
    [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream
    [Recompose] Stream Props to React Children with RxJS
    [Recompose] Merge RxJS Button Event Streams to Build a React Counter Component
    [Recompose] Handle React Events as Streams with RxJS and Recompose
    [Recompose] Stream a React Component from an Ajax Request with RxJS
  • 原文地址:https://www.cnblogs.com/liubinsh/p/6938905.html
Copyright © 2020-2023  润新知