-2

I wanted to understand a basic programming concept = evaluation. What does that means when python evaluates an expression?

To understand this, I need to understand what's the difference between 'expressions' and 'statements'. 'Expression' = something that results in a single value. 'Statement' = a line of code (made of expressions) that needs to be executed.

So, what does "evaluate an expression" mean (because this is something the Python interpreter always do, right)?

Thanks.

A clear explanation of the concept (with some simple examples even if I've found a lot on internet).

khelwood
  • 55,782
  • 14
  • 81
  • 108
soufianta
  • 1
  • 2
  • 2
    Does this answer your question? [What is the difference between an expression and a statement in Python?](https://stackoverflow.com/questions/4728073/what-is-the-difference-between-an-expression-and-a-statement-in-python) – Bill the Lizard Mar 06 '23 at 13:35
  • 1
    “Evaluating" an expression is like "executing" a statement. You pull the values together and get its result. – iBug Mar 06 '23 at 13:36

1 Answers1

0

Both expressions and statements are syntactic constructs. As you observed, expressions have values. Evaluation is the process of determining the "canonical" value of the expression, the value whose canonical value is itself.

For example, the canonical value of the three expressions 3 + 5, 2 * 4, and 8 is 8. The canonical value of print("hello world") is None. The canonical value of ''.join(["foo", "bar"]) is "foobar".

The eval function can be used to find the canonical value of an expression.

>>> eval('8') == eval('3 + 5') == eval('2 * 4')
True

(The canonical value of the chained comparison is True, indicating that 8 is indeed the canonical value of 8, 3 + 5, and 2 * 4. Part of the job of the interactive interpreter is to print the canonical value of an expression if that value is not None.)

Statements don't have values. That doesn't mean they can't produce a value; it just means you can't access the value. Most statements really don't have a value. The exception is the expression statement, which is, as the name implies, a statement that consists of a single expression. Expression statements are what allow you to have a function call act as a statement by itself without needing to embed it in a meaningless assignment statement to capture its return value. That is, we can write

print("Hello, world!")

instead of

whocares = print("Hello, world!")  # assert whocares is None

Any expression can be used as an expression statement, but only expressions that include at least one side effect (mutating an object, writes to a file, etc) are useful as expression statements.

chepner
  • 497,756
  • 71
  • 530
  • 681