zoukankan      html  css  js  c++  java
  • 006: 基本控制语句 if 与 bool 数据类型

    1. bool类型与其他数据类型之间存在一个隐式的转换关系,与javascript极其相似:

    Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

    • None

    • False

    • zero of any numeric type, for example, 00.00j.

    • any empty sequence, for example, ''()[].

    • any empty mapping, for example, {}.

    • instances of user-defined classes, if the class defines a __bool__() or __len__() method, when that method returns the integer zero or bool value False[1]

    All other values are considered true — so objects of many types are always true.

    Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

    2. 布尔类型逻辑运算有三种:

    These are the Boolean operations, ordered by ascending priority:

    OperationResultNotes
    or y if x is false, then y, else x (1)
    and y if x is false, then x, else y (2)
    not x if x is false, then True, else False (3)

     

     

     


    (1). This is a short-circuit operator, so it only evaluates the second argument if the first one is 
    False.

    (2). This is a short-circuit operator, so it only evaluates the second argument if the first one is True.

    (3). not has a lower priority than non-Boolean operators, so not == b is interpreted as not (a == b), and == not b is a syntax error.

    3. 比较语句:

    There are eight comparison operations in Python. They all have the same priority (which is higher than that of the Boolean operations). Comparisons can be chained arbitrarily; for example, <= z is equivalent to and <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when y is found to be false).

    This table summarizes the comparison operations:

    OperationMeaning
    < strictly less than
    <= less than or equal
    > strictly greater than
    >= greater than or equal
    == equal
    != not equal
    is object identity
    is not negated object identity

     

    Objects of different types, except different numeric types, never compare equal. Furthermore, some types (for example, function objects) support only a degenerate notion of comparison where any two objects of that type are unequal. The <<=> and >= operators will raise a TypeError exception when comparing a complex number with another built-in numeric type, when the objects are of different types that cannot be compared, or in other cases where there is no defined ordering.

    Non-identical instances of a class normally compare as non-equal unless the class defines the __eq__() method.

    Instances of a class cannot be ordered with respect to other instances of the same class, or other types of object, unless the class defines enough of the methods __lt__()__le__()__gt__(), and __ge__() (in general, __lt__() and __eq__() are sufficient, if you want the conventional meanings of the comparison operators).

    The behavior of the is and is not operators cannot be customized; also they can be applied to any two objects and never raise an exception.The operators is and is not test for object identity: is y is true if and only if x and y are the same object. is not y yields the inverse truth value.

    Two more operations with the same syntactic priority, in and not in, are supported only by sequence types (below).

     4. if语句的结构比较简单,下面练习中很清楚。在练习中无意涉及了class与函数的定义,非常简单,暂时只做了解,以后深入学习。

    none = None
    false = False 
    true = True 
    zero_int = 0
    one_int = 1
    negetive_int = -1
    empty_string = ''
    empty_list = []
    empty_tuple = ()
    empty_mapping = {}
    
    class X:
        def __bool__(self):
            return True
    
    class Y:
        def __bool__(self):
            return False    
    
    class M:
        def __len__(self):
            return 1
    
    class N:
        def __len__(self):
            return 0            
    
    def judgeTrueFalse(name,value):
        if value:
            print(name + ' is a true value.')    
        else:
            print(name + ' is a false value.')
            
    judgeTrueFalse('none', none) 
    judgeTrueFalse('false', false) 
    judgeTrueFalse('true', true) 
    judgeTrueFalse('zero_int', zero_int) 
    judgeTrueFalse('one_int', one_int) 
    judgeTrueFalse('negetive_int', negetive_int) 
    judgeTrueFalse('empty_string', empty_string) 
    judgeTrueFalse('empty_list', empty_list) 
    judgeTrueFalse('empty_tuple', empty_tuple) 
    judgeTrueFalse('empty_mapping', empty_mapping) 
    
    x = X()
    y = Y()
    z = x
    m = M()
    n = N()
    
    judgeTrueFalse('instance of X', x)
    judgeTrueFalse('instance of Y', y) 
    judgeTrueFalse('instance of M', m)
    judgeTrueFalse('instance of N', n) 
    
    judgeTrueFalse('false or true', false or true) 
    judgeTrueFalse('false and true', false and true) 
    judgeTrueFalse('not false', not false) 
    
    judgeTrueFalse('1 > 0', 1 > 0) 
    judgeTrueFalse('1 >= 0', 1 >= 0) 
    judgeTrueFalse('1 == 0', 1 == 0) 
    judgeTrueFalse('1 != 0', 1 != 0) 
    judgeTrueFalse('1 is int', 1 is int)
    judgeTrueFalse('1 is 1', 1 is 1) 
    judgeTrueFalse('x is X', x is X) 
    judgeTrueFalse('x is z', x is z) 
    
    # if statements practice: get the last day in the month for a date
    
    date = '2016-01-04'
    year = int(date[0:4])
    month = int(date[5:7])
    day = int(date[8:10])
    
    last_day = 0
    
    print(year)
    print(month)
    print(day)
    
    if month in (1,3,5,7,8,10,12):
        last_day = 31
    elif month in(4,6,11):
        last_day = 30
    else:
        if year % 4 == 0 and year % 100    != 0:
            last_day = 29
        else:
            last_day = 28
    print(last_day)        

    运行结果:

    none is a false value.
    false is a false value.
    true is a true value.
    zero_int is a false value.
    one_int is a true value.
    negetive_int is a true value.
    empty_string is a false value.
    empty_list is a false value.
    empty_tuple is a false value.
    empty_mapping is a false value.
    instance of X is a true value.
    instance of Y is a false value.
    instance of M is a true value.
    instance of N is a false value.
    false or true is a true value.
    false and true is a false value.
    not false is a true value.
    1 > 0 is a true value.
    1 >= 0 is a true value.
    1 == 0 is a false value.
    1 != 0 is a true value.
    1 is int is a false value.
    1 is 1 is a true value.
    x is X is a false value.
    x is z is a true value.
    2016
    1
    4
    31

  • 相关阅读:
    vue生命周期钩子函数
    mongodb window安装配置
    git 添加远程仓
    webpack + vue + node 打造单页面(入门篇)
    git 命令
    javascript 利用FileReader和滤镜上传图片预览
    javascript 一些特殊的字符运算
    Es6 Symbol.iterator
    配置 github 上的程序
    Redis 的数据类型
  • 原文地址:https://www.cnblogs.com/jcsz/p/5099431.html
Copyright © 2011-2022 走看看