More on Conditions - To Compare -Comparing Sequences and Other Types
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,
>>> 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 TypeError
exception.
More on Conditions - To Compare -Comparing Sequences and Other Types的更多相关文章
- Python Tutorial 学习(五)--Data Structures
5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...
- [译]The Python Tutorial#5. Data Structures
[译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...
- Oracle Database 11g express edition
commands : show sys connect sys as sysdba or connect system as sysdba logout or disc clear screen or ...
- python自动化运维二:业务服务监控
p { margin-bottom: 0.25cm; line-height: 120% } a:link { } p { margin-bottom: 0.25cm; line-height: 12 ...
- [ difflib] simple1.py
#!/usr/bin/env python # _*_ coding:utf-8 _*_ import difflib text1 = """text1: # 定义字符串 ...
- PatentTips - Scheduling compute kernel workgroups to heterogeneous processors based on historical processor execution times and utilizations
BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention relates generally to h ...
- python运维开发常用模块(四)文件对比模块difflib
1.difflib介绍 difflib作为 Python的标准库模块,无需安装,作用是对比文本之间的差异,且支持 输出可读性比较强的HTML文档,与Linux下的diff命令相似.我们可以 使用dif ...
- SAP T CODE : Description (Program)
SAP T CODE : Description (Program) V : Quickstart RKCOWUSL (RKCOWUSL)V+01 : Create Sales Call (SAPMV ...
- cvpr2015papers
@http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...
随机推荐
- 找不到 -lz解决方法
sudo apt-get install libghc-zlib-dev
- 爬虫基础学习 转【http://www.cnblogs.com/huangxincheng/archive/2012/11/08/2759752.html】
这一篇我们聊聊在页面抓取时应该注意到的几个问题. 一:网页更新 我们知道,一般网页中的信息是不断翻新的,这也要求我们定期的去抓这些新信息,但是这个“定期”该怎么理解,也就是多长时间需要 抓一次该页面, ...
- J2EE开发实战基础系列一 HelloWorld【转】
开始咱们的第一个程序,首先是配置环境,按照上一章所描述的方式下载开发工具,然后配置Java环境变量,给大家看下具体的结构: 环境变量配置OK的提示,如上图. Eclipse和Tomcat的文件目录 ...
- [原创]VM虚拟机安装centos6.4详细图文教程
1.启动虚拟机,新建虚拟机. 2.选择从镜像安装,选择centos6.4的路径. 3.设置用户名和密码.注:只能用小写字母. 4.选择安装路径. 5.配置磁盘大小. 6.准备创建. 如果需要自定义 ...
- 从客户端中检测到有潜在危险的Request.Form值的解决方法
描述:从客户端中检测到有潜在危险的Request.Form值的解决方法asp.net 2.0 通常解决办法将.aspx文件中的page项添加ValidateRequest="false&qu ...
- C# 检测网络链接
;//Local system uses a modem to connect to the Internet. ; //Local system uses a local area network ...
- 本地调试WordPress计划终告失败
小猪本来想把博客的网站数据迁移到自己的电脑上面,mysql数据库还是放在主机供应商,这样就能缓解一下每次写博客时访问速度捉急的状况. 计划是美满的,但是只到实施的时候才发现各种问题.先是直接运行程序时 ...
- 如何在 Ubuntu 15.04 系统中安装 Logwatch
大家好,今天我们会讲述在 Ubuntu 15.04 操作系统上如何安装 Logwatch 软件,它也可以在各种 Linux 系统和类 Unix 系统上安装.Logwatch 是一款可定制的日志分析和日 ...
- cf 307
一开始我是不想打的 又因为307这个数字太特殊了 毕竟307 希望今天的考试不要挂掉 http://codeforces.com/contest/551/problem/A #include< ...
- 第一次编辑JAVA
其实就是根据例题来进行编辑,但并不是一次就对了的,有大小写输入出错的问题,有漏掉标点符号的问题,值得注意的是,所有的标点符号都需要是在英文输入法状态下输入的,最后的问题——例题所用的C盘给我们带来了系 ...