美文网首页
Boolean Operations

Boolean Operations

作者: whenitsallover | 来源:发表于2018-10-17 11:36 被阅读0次

    official link:
    https://docs.python.org/2/library/stdtypes.html#boolean-operations-and-or-not
    https://docs.python.org/2.7/reference/expressions.html#boolean-operations

    Boolean Operations

    These are the Boolean operations, ordered by ascending priority:


    image.png

    Notes:

    This is a short-circuit operator, so it only evaluates the second argument if the first one is false.
    This is a short-circuit operator, so it only evaluates the second argument if the first one is true.
    not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error.

    image.png

    In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets, and frozensets). All other values are interpreted as true. (See the __nonzero__() special method for a way to change this.)

    The operator not yields True if its argument is false, False otherwise.

    The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

    The expression xor y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

    (Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value. Because not has to invent a value anyway, it does not bother to return a value of the same type as its argument, so e.g., not 'foo' yields False, not ''.)

    相关文章

      网友评论

          本文标题:Boolean Operations

          本文链接:https://www.haomeiwen.com/subject/ucilzftx.html