liveBook · Manning

Assignment

As explained in Get Programming: Learn to code with Python

Assignment is a programming-language operation that binds or stores an evaluated value in a variable, property, or other assignable location.

Assignment gives a value a name so you can use it later without calculating it again. In Python, `=` means “assign,” not “is mathematically equal to.” For a basic variable assignment, put the name on the left and a value or expression on the right. section 4.0 section 4.1

Calculate, then name the result

`a = 1` `b = 2` `c = a + b` The first two lines assign values to `a` and `b`. On the third line, Python uses those values to calculate `1 + 2`, then assigns the result, `3`, to `c`. An assignment is a statement; the expression `a + b` supplies its value. section 4.1 section 5.3

The first assignment to a variable initializes it: it binds the name to an object. You can later reassign the same name, as in `c = a - b`. The name then refers to the new object. Reassigning a name that refers to an immutable object does not change the old object. section 4.2 section 24.1

Assignment can also replace an element in a mutable list. If `colors = ["red", "blue", "yellow"]`, then `colors[0] = "orange"` changes the first element of that list. This differs from assigning a different object to the name `colors`. Take care when two names refer to the same mutable object: a change through one name is visible through the other. section 25.6 section 28.3

Quick check: after `a = 1`, `b = 2`, and `c = a + b`, what value does `c` have? What would it have after `c = a - b`? Assigning intermediate results like these helps turn a conversion formula into a small program. section 4.2 section 6.6

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains expression evaluation, initialization, reassignment, list-element changes, and aliasing; does not develop assignment syntax beyond these cases.
Examples
Works through `a = 1`, `b = 2`, and `c = a + b`, then uses indexed list replacement and conversion exercises.
What it assumes
Introduces variables through familiar names and arithmetic before showing Python assignments.
What you can do after
Build it from scratch: initialize and update variables, calculate conversion results, and replace indexed list elements.
How it connects
Connects assignment to variables, expressions, immutable objects, mutable lists, and aliasing.

Also explained in

Compare how 7 books explain assignment →

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.