In Python, what is the difference between expressions and statements?
An expression is something that can be reduced to a value, for example
"1+3" is an expression, but
"foo = 1+3" is not.
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.
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
# all the above expressions print 42 if x: do_y() return a = 7
Though this isn’t related to Python:
expression evaluates to a value.
statement does something.
>>> x + 2 # an expression >>> x = 1 # a statement >>> y = x + 1 # a statement >>> print y # a statement (in 2.x) 2
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.
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.
Examples of expressions:
>>> 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.,
In gross general terms: Statements Do Something and are often composed of expressions (or other statements)
The distinction of “Statements do something” and “expressions produce a value” distinction can become blurry however:
ifis usually a statement, such as
if x<0: x=0but you can also have a conditional expression like
x=0 if x<0 else 1that are expressions. In other languages, like C, this form is called an operator like this
def func(a): return a*ais an expression when used but made up of statements when defined.
Noneis a procedure in Python:
def proc(): passSyntactically, you can use
proc()as an expression, but that is probably a bug…
func(x=2);Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The assignment statement of
x=2inside of the function call of
func(x=2)in Python sets the named argument
ato 2 only in the call to
funcand is more limited than the C example.
A statement contains a keyword.
An expression does not contain a keyword.
print "hello" is statement, because
"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))
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
2.0 + 3 is an expression which evaluates to
5.0 and has a type
float associated with it.
Statements are composed of expression(s). It can span multiple lines.
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.
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!")
A Expression is a combination of values, operators and literals which yields something.
val a: Int = 5 + 5 #yields 10
Statements before 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!
Expressions always evaluate to a value, statements don’t.
variable declaration and assignment are statements because they do not return a value
const list = [1,2,3];
Here we have two operands – a variable ‘sum’ on the left and an expression on the right.
The whole thing is a statement, but the bit on the right is an expression as that piece of code returns a value.
const sum = list.reduce((a, b)=> a+ b, 0);
Function calls, arithmetic and boolean operations are good examples of expressions.
Expressions are often part of a statement.
The distinction between the two is often required to indicate whether we require a pice of code to return a value.
An expression is a combination of values, variables, and operators. A value all by itself is
considered an expression, and so is a variable, so the following are all legal expressions:
>>> 42 42 >>> n 17 >>> n + 25 42
When you type an expression at the prompt, the interpreter evaluates it, which means that
it finds the value of the expression. In this example, n has the value 17 and n + 25 has the
A statement is a unit of code that has an effect, like creating a variable or displaying a
>>> n = 17 >>> print(n)
The first line is an assignment statement that gives a value to n. The second line is a print
statement that displays the value of n.
When you type a statement, the interpreter executes it, which means that it does whatever
the statement says. In general, statements don’t have values.
An expression translates to a value.
A statement consumes a value* to produce a result**.
*That includes an empty value, like:
**This result can be any action that changes something; e.g. changes the memory ( x = 1) or changes something on the screen ( print("x") ).
A few notes:
Think of statements as consecutive actions or instructions that your program executes. So, value assignments, if clauses, together with for and while loops, are all statements. Function and class definitions are statements, too.
Think of expressions as anything that can be put into an if clause. Typical examples of expressions are literals, values returned by operators (excluding in-place operators), and comprehensions, such as list, dictionary, and set comprehensions. Function calls and method calls are expressions, too.
Python 3.8 introduced the dedicated := operator, which assigns a value to the variable but acts as an expression instead of a statement. Due to its visual appearance, it was quickly nicknamed the