Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

What is the difference between an expression and a statement in Python?

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

Answer*

Draft saved
Draft discarded
Cancel
17
  • 33
    expressions are parts of statements Commented Nov 25, 2013 at 17:45
  • 69
    @bismigalis: Every valid Python expression can be used as a statement (called an "expression statement"). In this sense, expressions are statements. Commented Nov 25, 2013 at 18:05
  • 2
    Expressions can also include function calls (including calling classes for object instantiation). Technically these are "identifiers" exactly like names bound to values in an = statement ... even though the binding is through the 'def' or 'class' keywords. However, for this answer I would separately spell out function calls to make that clear. Commented Feb 8, 2015 at 23:26
  • 2
    @George Fair enough. :) Expression statements are quite useful even outside of the REPL – it's quite common to use function call expressions as expression statements, e.g. print("Hello world!") or my_list.append(42). Commented May 28, 2019 at 12:09
  • 8
    @WillTaylor Everything that yields a value is an expression, i.e. everything you could write on the write-hand side of an assignment. Since a = yield 7 is valid, yield 7 is an expression. A long time ago, yield was introduced as a statement, but it was generalized to an expression in PEP 342. Commented Aug 22, 2019 at 19:37

lang-py

AltStyle によって変換されたページ (->オリジナル) /