python学习笔记2

原创 2006年05月19日 00:24:00

下面这一章有些疑问:

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

  • 2016-09-27 20:56
  • 1.06MB
  • 下载

Python学习笔记(2)

根据academy的Python课程边学边练习,学习手册参考Python官方教程,中文版见http://python.usyiyi.cn/python_278/tutorial/appetite.ht...

python简明教程学习笔记2--函数

<!-- @page {margin:0.79in} h1 {margin-bottom:0.08in} h1.western {font-family:"Liberation Se...

Python爬虫学习笔记(2):多线程入门

引言:Python爬虫的爬取过程中可以利用多线程来提高爬取效率,通过调节线程时间降低爬取失败的风险。 共同编写者:OOFFrankDura 首先介绍两个方法的区别: 1.setDaemon():...

Python学习笔记2

1.标识符规则 标识符是由字母、数字和下划线(_)构成的字符序列 标识符必须以字母或下划线(_)开头,不能以数字开头 标识符不能是关键字。 标识符可以为任意长度 2.变量、赋值语句和赋值表达式Pyth...

python学习笔记--2

(13)局部变量与全局变量(用global语句声明是全局的) def func(x):     print 'x is', x     x = 2  ...

Python学习笔记[2]

【备注:本文是学习http://blog.csdn.net/ithomer/article/details/8907268过程中记下的笔记】 【EX:所注为代码示例】 【Tips:所注为标识,可能...

python数据分析与挖掘学习笔记(3)_小说文本数据挖掘part2

接上一节。 注:本文的所有路径都才用的是相对路径,读者请自行换成自己的绝对路径,保证文件可读取。 要进行文本挖掘,需要引入gensim这个工具包,需要用到其中的语料库,模型,相似度等包。 from g...

python学习笔记(2)网络编程

套接字地址:主机-端口对    一个网络地址由主机地址和端口号组成,有效的端口号地址是0-65535(0-1024端口号预留给系统),在POSIX兼容系统中,可以在/etc/services中找到预留...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)