zoukankan      html  css  js  c++  java
  • More on Conditions

    The conditions used in while and if statements can contain any operators, not just comparisons.

    The comparison operators in and not in check whether a value occurs (does not occur) in a sequence. The operators is and is not compare whether two objects are really the same object; this only matters for mutable objects like lists. All comparison operators have the same priority, which is lower than that of all numerical operators.

    Comparisons can be chained. For example, == c tests whether a is less than b and moreover b equals c.

    Comparisons may be combined using the Boolean operators and and or, and the outcome of a comparison (or of any other Boolean expression) may be negated with not. These have lower priorities than comparison operators; between them, not has the highest priority and or the lowest, so that and not or C is equivalent to (A and (not B)) or C. As always, parentheses can be used to express the desired composition.

    The Boolean operators and and or are so-called short-circuit operators: their arguments are evaluated from left to right, and evaluation stops as soon as the outcome is determined. For example, if A and C are true but B is false, and and C does not evaluate the expression C. When used as a general value and not as a Boolean, the return value of a short-circuit operator is the last evaluated argument.

    It is possible to assign the result of a comparison or other Boolean expression to a variable. For example,

     >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
    >>> non_null = string1 or string2 or string3
    >>> non_null
    'Trondheim'

    >>>non_null = string1 and string2 and string3
    >>>non_null

    ''

    Note that in Python, unlike C, assignment cannot occur inside expressions. C programmers may grumble about this, but it avoids a common class of problems encountered in C programs: typing = in an expression when == was intended.

    Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode code point number to order individual characters. Some examples of comparisons between sequences of the same type:

    (1, 2, 3)              < (1, 2, 4)
    [1, 2, 3]              < [1, 2, 4]
    'ABC' < 'C' < 'Pascal' < 'Python'
    (1, 2, 3, 4)           < (1, 2, 4)
    (1, 2)                 < (1, 2, -1)
    (1, 2, 3)             == (1.0, 2.0, 3.0)
    (1, 2, ('aa', 'ab'))   < (1, 2, ('abc', 'a'), 4)
    

    Note that comparing objects of different types with < or > is legal provided that the objects have appropriate comparison methods. For example, mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc. Otherwise, rather than providing an arbitrary ordering, the interpreter will raise a TypeErrorexception.

  • 相关阅读:
    板邓:php获取数组元素个数
    板邓:PHP解决Using $this when not in object context in【转】
    板邓:MYSQL distinct关键字查询重复值只显示一条记录
    板邓: php 过滤文章的html标签方法
    板邓:PHP使用mb_strimwidth截取中文字符串方法大全
    板邓:php str_replace单双引号的替换问题
    板邓:PHP下利用PHPMailer配合QQ邮箱下的域名邮箱发送邮件(转)
    板邓:wordpress自定义伪静态 WP_Rewrite
    板邓:wordpress获取当前页面的id、别名
    获取文件夹名
  • 原文地址:https://www.cnblogs.com/dltts/p/5984140.html
Copyright © 2011-2022 走看看