liveBook · Manning

Expression

As explained in Get Programming: Learn to code with Python

A combination of values, names, and operators that evaluates to a single result in a program.

An expression is code that evaluates to a value. It can use values directly, such as `3 + 2`, or names whose values are already known, such as `b - c`. To find its result, substitute each name’s value and carry out the operation. section 4.1 section 5.3

Give a result a name

If `a = 1` and `b = 2`, then `c = a + b` evaluates the expression `a + b` as `1 + 2`, then assigns the result, `3`, to `c`. Unlike a math equation, Python assignment needs a variable name on the left; `a + x = b` does not ask the computer to solve for `x`. section 4.1 section 4.3

An expression and a statement are not quite the same thing. `2.7 - 1` is both: it is a line of code and has a value. `a = 5 + 6` is a statement containing an expression, but the assignment itself is not an expression. Likewise, `print(21)` is a statement; printing is an action, not a value. section 5.3

Show an expression’s result

Try `print(13 - 1)`. Python evaluates `13 - 1` first, then prints its value. You can also print string expressions: `print(3 * "a")` shows `aaa`. Quick check: what would `print("abc" + "def")` show? section 11.1

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines evaluation through substitution and examples, then distinguishes expressions from statements without descending into interpreter internals.
Examples
Works through `c = a + b` and checks arithmetic, assignment, and printed expressions.
What it assumes
Starts with everyday familiarity with instructions and decisions, then introduces variables and expressions without requiring prior programming experience.
What you can do after
Practice/perform it: Evaluate expressions, use their results in assignments or `print()`, and distinguish them from statements that only perform actions.
How it connects
Connects expressions to variables, assignment, object values, operations, statements, and `print()`.

Also explained in

Compare how 11 books explain expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.