What is the difference between an expression and a statement in Python?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Magic Ocean Looping
--
Chapters
00:00 What Is The Difference Between An Expression And A Statement In Python?
00:14 Accepted Answer Score 324
00:49 Answer 2 Score 139
01:08 Answer 3 Score 138
03:46 Answer 4 Score 38
04:12 Answer 5 Score 27
04:31 Thank you
--
Full question
https://stackoverflow.com/questions/4728...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #expression
#avk47
ACCEPTED ANSWER
Score 327
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
ANSWER 2
Score 141
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
ANSWER 3
Score 138
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 
ifis usually a statement, such asif x<0: x=0but you can also have a conditional expression likex=0 if x<0 else 1that are expressions. In other languages, like C, this form is called an operator like thisx=x<0?0:1; - You can write you own Expressions by writing a function. 
def func(a): return a*ais an expression when used but made up of statements when defined. - An expression that returns 
Noneis a procedure in Python:def proc(): passSyntactically, you can useproc()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 ofx=2inside of the function call offunc(x=2)in Python sets the named argumentato 2 only in the call tofuncand is more limited than the C example. 
ANSWER 4
Score 38
An expression is something that can be reduced to a value.
Let's take the following examples and figure out what is what:
"1+3" and "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. Try it out!
Another statement could be:
class Foo(Bar): pass
as it cannot be reduced to a value.