问题:Python中的表达式和语句有什么区别?

在Python中,表达式和语句之间有什么区别?

In Python, what is the difference between expressions and statements?


回答 0

表达式仅包含标识符文字运算符,其中的运算符包括算术运算符和布尔运算符,函数调用运算符 ()包括预订运算符 []等,并且可以简化为某种“值”,可以是任何Python对象。例子:

3 + 5
map(lambda x: x*x, range(10))
[a.x for a in some_iterable]
yield 7

声明(请参阅 1另一方面,2)是构成一行(或几行)Python代码的所有内容。注意表达式也是语句。例子:

# all the above expressions
print 42
if x: do_y()
return
a = 7

Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator () the subscription operator [] and similar, and can be reduced to some kind of “value”, which can be any Python object. Examples:

3 + 5
map(lambda x: x*x, range(10))
[a.x for a in some_iterable]
yield 7

Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:

# all the above expressions
print 42
if x: do_y()
return
a = 7

回答 1

表达 -来自新牛津美国词典

expression(数学表达式):数学上共同表示一个数量的一组符号:圆的圆周的表达式为2πr。

总的来说:表达式产生至少一个值。

在Python中,表达式在Python语言参考中得到了广泛的介绍。通常,Python中的表达式由AtomsPrimaryOperators的语法合法组合组成。

维基百科的Python表达式

表达式示例:

操作符内置函数或用户编写的函数的调用之间的文字和语法正确组合:

>>> 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

维基百科的声明

在计算机编程中,可以将语句视为命令式编程语言中最小的独立元素。一个程序是由一个或多个语句的序列构成的。语句将具有内部组件(例如,表达式)。

维基百科的Python语句

总的来说:陈述做某事,通常由表达式(或其他陈述)组成

《 Python语言参考广泛涵盖了简单语句复合语句

但是,“语句做某事”和“表达式产生值”的区别可能变得模糊:

  • 列表推导被认为是“表达式”,但它们具有循环构造,因此也可以执行某些操作。
  • if通常是一个语句,如if x<0: x=0,但你也可以有一个条件表达式就像x=0 if x<0 else 1是表达式。在其他语言中,例如C,这种形式称为这样的运算符x=x<0?0:1;
  • 您可以通过编写函数来编写自己的表达式。def func(a): return a*a使用时为表达式,但定义时由语句组成。
  • 返回的表达式None是Python中的一个过程:从def proc(): pass语法上讲,您可以将其proc()用作表达式,但这可能是一个错误…
  • 对于表达式和语句之间的区别,Python比说C更严格。在C语言中,任何表达都是法律声明。您可以拥有func(x=2);“表达式”或“语句”吗?(答案:表达式用作带有副作用的语句)Python x=2中的函数调用内部的赋值语句仅在对C示例的调用func(x=2)中将命名参数设置a为2,func并且比C示例受到更多限制。

Expression — from the New Oxford American Dictionary:

expression: Mathematics a collection of symbols that jointly express a quantity : the expression for the circumference of a circle is 2πr.

In gross general terms: Expressions produce at least one value.

In Python, expressions are covered extensively in the Python Language Reference In general, expressions in Python are composed of a syntactically legal combination of Atoms, Primaries and Operators.

Python expressions from Wikipedia

Examples of expressions:

Literals and syntactically correct combinations with Operators and built-in functions or the call of a user-written functions:

>>> 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

Statement from Wikipedia:

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).

Python statements from Wikipedia

In gross general terms: Statements Do Something and are often composed of expressions (or other statements)

The Python Language Reference covers Simple Statements and Compound Statements extensively.

The distinction of “Statements do something” and “expressions produce a value” distinction can become blurry however:

  • List Comprehensions are considered “Expressions” but they have looping constructs and therfore also Do Something.
  • The if is usually a statement, such as if x<0: x=0 but you can also have a conditional expression like x=0 if x<0 else 1 that are expressions. In other languages, like C, this form is called an operator like this x=x<0?0:1;
  • You can write you own Expressions by writing a function. def func(a): return a*a is an expression when used but made up of statements when defined.
  • An expression that returns None is a procedure in Python: def proc(): pass Syntactically, you can use proc() as an expression, but that is probably a bug…
  • Python is a bit more strict than say C is on the differences between an Expression and Statement. In C, any expression is a legal statement. You can have func(x=2); Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The assignment statement of x=2 inside of the function call of func(x=2) in Python sets the named argument a to 2 only in the call to func and is more limited than the C example.

回答 2

尽管这与Python不相关:

一个expression值等于一个值。A statement做某事。

>>> x + 2         # an expression
>>> x = 1         # a statement 
>>> y = x + 1     # a statement
>>> print y       # a statement (in 2.x)
2

Though this isn’t related to Python:

An expression evaluates to a value. A statement does something.

>>> x + 2         # an expression
>>> x = 1         # a statement 
>>> y = x + 1     # a statement
>>> print y       # a statement (in 2.x)
2

回答 3

语句表示动作或命令,例如打印语句,赋值语句。

print 'hello', x = 1

表达式是变量,操作和值的组合,产生结果值。

5 * 5 # yields 25

最后,表达式语句

print 5*5

Statements represent an action or command e.g print statements, assignment statements.

print 'hello', x = 1

Expression is a combination of variables, operations and values that yields a result value.

5 * 5 # yields 25

Lastly, expression statements

print 5*5

回答 4

表达式是可以简化为值的内容,例如"1+3""foo = 1+3"

很容易检查:

print foo = 1+3

如果不起作用,则为语句;如果不起作用,则为表达式。

另一种说法可能是:

class Foo(Bar): pass

因为它不能减少为一个值。

An expression is something that can be reduced to a value, for example "1+3" or "foo = 1+3".

It’s easy to check:

print foo = 1+3

If it doesn’t work, it’s a statement, if it does, it’s an expression.

Another statement could be:

class Foo(Bar): pass

as it cannot be reduced to a value.


回答 5

  1. 表达式是返回值的语句。因此,如果它可以出现在赋值的右侧,或者作为方法调用的参数,则它是一个表达式。
  2. 根据上下文,某些代码可以是表达式也可以是语句。该语言可能有一种在模棱两可时区分它们的方法。
  1. An expression is a statement that returns a value. So if it can appear on the right side of an assignment, or as a parameter to a method call, it is an expression.
  2. Some code can be both an expression or a statement, depending on the context. The language may have a means to differentiate between the two when they are ambiguous.

回答 6

表达式是某种东西,而语句则是某种东西。
表达式也是一个语句,但是它必须有一个返回值。

>>> 2 * 2          #expression
>>> print(2 * 2)     #statement

PS:解释器始终打印出所有表达式的值。

An expression is something, while a statement does something.
An expression is a statement as well, but it must have a return.

>>> 2 * 2          #expression
>>> print(2 * 2)     #statement

PS:The interpreter always prints out the values of all expressions.


回答 7

声明:

语句是执行某项操作的动作或命令。例如:If-Else,Loops..etc

val a: Int = 5
If(a>5) print("Hey!") else print("Hi!")

表达:

表达式是值,运算符和文字的组合,产生某些结果。

val a: Int = 5 + 5 #yields 10

STATEMENT:

A Statement is a action or a command that does something. Ex: If-Else,Loops..etc

val a: Int = 5
If(a>5) print("Hey!") else print("Hi!")

EXPRESSION:

A Expression is a combination of values, operators and literals which yields something.

val a: Int = 5 + 5 #yields 10

回答 8

语句包含关键字。

表达式不包含关键字。

print "hello"是语句,因为print是关键字。

"hello" 是一个表达式,但是列表压缩与此相反。

以下是一个表达式语句,没有列表理解也是如此:

(x*2 for x in range(10))

A statement contains a keyword.

An expression does not contain a keyword.

print "hello" is statement, because print is a keyword.

"hello" is an expression, but list compression is against this.

The following is an expression statement, and it is true without list comprehension:

(x*2 for x in range(10))

回答 9

表达式:

  • 通过组合objects和来形成表达式operators
  • 表达式具有一个值,该值具有一个类型。
  • 简单表达式的语法:<object><operator><object>

2.0 + 3是一个表达式,其结果为,5.0并且具有float与之关联的类型。

陈述

语句由表达式组成。它可以跨越多行。

Expressions:

  • Expressions are formed by combining objects and operators.
  • An expression has a value, which has a type.
  • Syntax for a simple expression:<object><operator><object>

2.0 + 3 is an expression which evaluates to 5.0 and has a type float associated with it.

Statements

Statements are composed of expression(s). It can span multiple lines.


回答 10

有些语句可能会改变我们Python程序的状态:创建或更新变量,定义函数等。

表达式仅返回一些值,无法更改函数中的全局状态或局部状态。

但是现在我们:=知道了,它是外星人!

There are some statements which could change the state of our Python program: create or update variables, define function, etc.

And expressions just return some value can’t change the global state or local state in a function.

But now we got :=, it’s an alien!


回答 11

Python将表达式称为“表达式语句”,因此问题可能尚未完全形成。

语句几乎包含您可以在Python中执行的所有操作:计算值,分配值,删除变量,打印值,从函数返回,引发异常等。完整列表在此处:http:// docs.python.org/reference/simple_stmts.html#

表达式语句仅限于调用函数(例如math.cos(theta)“),运算符(例如“ 2 + 3”)等以产生值。

Python calls expressions “expression statements”, so the question is perhaps not fully formed.

A statement consists of pretty much anything you can do in Python: calculating a value, assigning a value, deleting a variable, printing a value, returning from a function, raising an exception, etc. The full list is here: http://docs.python.org/reference/simple_stmts.html#

An expression statement is limited to calling functions (e.g., math.cos(theta)”), operators ( e.g., “2+3”), etc. to produce a value.


回答 12

我认为表达式包含运算符+操作数和保存运算结果的对象…例如

var sum = a + b;

但是一条语句只是一行代码(可能是一个表达式)或代码块…例如

fun printHello(name: String?): Unit {
if (name != null)
    println("Hello ${name}")
else
    println("Hi there!")
// `return Unit` or `return` is optional

}

I think an expression contains operators + operands and the object that holds the result of the operation… e.g.

var sum = a + b;

but a statement is simply a line of a code (it may be an expression) or block of code… e.g.

fun printHello(name: String?): Unit {
if (name != null)
    println("Hello ${name}")
else
    println("Hi there!")
// `return Unit` or `return` is optional

}


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。