python学习笔记2
来源:互联网 发布:必达转运淘宝店真假 编辑:程序博客网 时间:2024/04/27 11:18
下面这一章有些疑问:
5.7 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, 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'
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.
主要需要理解的是:
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
会输出:
'Trondheim'
- python学习笔记2
- python学习笔记(2)
- Python学习笔记(2)
- Python学习笔记【2】
- python学习笔记--2
- python学习笔记2
- Python学习笔记[2]
- python学习笔记--2
- python学习笔记2
- Python 学习笔记 -2
- python 学习笔记2
- python 学习笔记-2
- python 学习笔记2
- python 学习笔记2
- Python学习笔记(2)
- Python学习笔记2
- Python学习笔记2
- Python学习笔记(2)
- 如何去掉 Dev cxGrid
- 关于Linux AT&T Asm的零星小结
- 一个清爽便利的Windows游戏窗口
- 从0到n的排列的一种求法
- JNI设计实践之路
- python学习笔记2
- 一个简单的tomcat部署方案
- 请高手改一下“教职工管理信息系统”程序(有1个错误)
- 循环报数问题
- 关于Asp程序的Server.CreateObject错误解决方法
- ASP操作Excel技术总结
- [分享]Linux系统初学者的常见问题解决集结大全
- hello,csdn.blog
- Delphi 字体修改一例