表达式--来自我的字典:expression: Mathematics a collection
of symbols that jointly express a
quantity : the expression for the
circumference of a circle is 2πr.
总的来说:表达式至少产生一个值。
在Python中,表达式在Python Language Reference中被广泛地覆盖一般来说,Python中的表达式由Atoms、Primaries和Operators的语法合法组合组成。
表达式示例:>>> 23
23
>>> 23l
23L
>>> range(4)
[0, 1, 2, 3]
>>> 2L*bin(2)
'0b100b10'
>>> def func(a): # Statement, just part of the example...
... return a*a # Statement...
...
>>> func(3)*4
36
>>> func(5) is func(a=5)
True
来自维基百科的声明:In computer programming a statement
can be thought of as the smallest
standalone element of an imperative
programming language. A program is
formed by a sequence of one or more
statements. A statement will have
internal components (e.g.,
expressions).
总的来说:语句做一些事情,通常由表达式(或其他语句)组成
“陈述做某事”和“表达产生价值”的区别可能变得模糊,但是:List Comprehensions被认为是“表达式”,但它们有循环结构,因此也会做一些事情。
if通常是一个语句,例如if x<0: x=0,但是也可以有一个类似于x=0 if x<0 else 1的conditional expression表达式。在其他语言中,如C,这种形式被称为像这样的运算符x=x<0?0:1;
可以通过编写函数来编写自己的表达式。def func(a): return a*a在使用时是一个表达式,但在定义时由语句组成。
返回None的表达式是Python中的一个过程:def proc(): pass语法上,可以使用proc()作为表达式,但这可能是一个错误。。。
Python在表达式和语句之间的区别上比C要严格一些。在C语言中,任何表达都是合法的。你可以有func(x=2);这是表达式还是语句?(Answer: Expression used as a Statement with a side-effect.)形式func(x=2)在Python中是非法的(或者至少它有不同的含义func(a=3)将命名参数a设置为3)