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, a < b == 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 A and not B 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, A and B 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.

最新文章

  1. crawler: 常用的一些工具
  2. 【Beta】第一次任务发布
  3. 【转】七年IT经验的七个总结
  4. elipse插件整理
  5. PR 不能手动修改素材尺寸的解决方法
  6. linux/shell 文本文件删除/删掉空行
  7. redis在Java web项目的简单应用(转载)
  8. Linux内核源代码情景分析-中断半
  9. php设计模式----工厂模式
  10. Java Socket编程基础(1)
  11. JavaSE之绘制菱形
  12. 把项目放到码云上,通过git 进行项目管理
  13. .Net Core部署IIS
  14. SpringMVC 使用 MultipartFile 实现文件上传
  15. 第五十六 css选择器和盒模型
  16. mybatis执行批量更新数据
  17. 理解DeepBox算法
  18. flask-appbuilder +echarts 展示数据笔记
  19. mybatis*中DefaultVFS的logger乱码问题
  20. jquery ajax 全局事件

热门文章

  1. hdu-----(3746)Cyclic Nacklace(kmp)
  2. HDUOJ-------1052Tian Ji -- The Horse Racing(田忌赛马)
  3. python——使用readline库实现tab自动补全
  4. SQL Server之字符串函数
  5. 推荐10款非常优秀的 HTML5 开发工具
  6. mysql 5.7 zip 文件在 windows下的安装
  7. 利用开源框架Volley来下载文本和图片。
  8. 解决visualsvn监听ip 错误的问题
  9. HDU 1394 Minimum Inversion Number
  10. 二模 (2) day2