1

I'm reading through Learning Python (3rd Edition), by Mark Lutz, and I'm in the portion that is dealing with the nuts and bolts of Python syntax.

He defines the Python language-structure hierarchy as follows:

  1. Programs are composed of modules
  2. Modules contain statements
  3. Statements contain expressions
  4. Expressions create and process objects

I'm a little confused about the definition of Python statements.
I've heard Expressions described as anything that is a value, but also can contain things like addition, etc.

Is it safe to say that statements are structured operations on expressions that drive a module's logic?

kmonsoor
  • 7,600
  • 7
  • 41
  • 55
flybonzai
  • 3,763
  • 11
  • 38
  • 72

3 Answers3

3

Yes, you are almost there.

Expressions are something that evaluate to some value.

On the other hand, statements are something that cause some action.

That action can be on some object, based on result of an expression which may or may not involve some other object(s).

kmonsoor
  • 7,600
  • 7
  • 41
  • 55
  • This is a great and concise answer! – flybonzai Dec 31 '15 at 23:48
  • 1
    I also like the compactness of this, but since _all_ values in Python, and hence all expression results, are objects, I'd recommend rewording the last sentence - maybe "That action can be on some object (possible the result of an expression), or the state of the program structure (creation of a new class), or the control flow (jumping back and forth with e.g. `if` or `for`)". – Aasmund Eldhuset Dec 31 '15 at 23:59
  • @AasmundEldhuset I've edited a bit. Hope it's better now. – kmonsoor Jan 01 '16 at 00:09
  • It's worth being explicit, too, that any expression by itself may constitute a statement, although only expressions that have side effects are useful as a statement. – chepner Jan 01 '16 at 01:29
2

I found this with a quick Google search, is it what you where looking for?

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

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

Community
  • 1
  • 1
Canyonman133
  • 49
  • 11
1

I'm quite wary of classifications like this, and especially attempts to make them into a hierarchy. An expression can also be, for example, a function call; I guess that falls into your "anything that is a value" definition since a function always returns a value even if it is None.

A statement is really everything else; assignment, flow control (eg defining a for or while loop, try/except, break, continue...), the introduction of a function or a class definition (the def or class keywords), and so on.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895