A critical aspect of a programming language is the means it provides for using names to refer to computational objects. If a value has been given a name, we say that the name binds to the value.
In Python, we can establish new bindings using the assignment statement, which contains a name to the left of = and a value to the right:
>>> radius = 10
>>> radius
10
>>> 2 * radius
20
Names are also bound via import statements.
>>> from math import pi
>>> pi * 71 / 223
1.0002380197528042
The = symbol is called the assignment operator in Python (and many other languages). Assignment is our simplest means of abstraction, for it allows us to use simple names to refer to the results of compound operations, such as the area computed above. In this way, complex programs are constructed by building, step by step, computational objects of increasing complexity.
The possibility of binding names to values and later retrieving those values by name means that the interpreter must maintain some sort of memory that keeps track of the names, values, and bindings. This memory is called an environment.
Names can also be bound to functions. For instance, the name max is bound to the max function we have been using. Functions, unlike numbers, are tricky to render as text, so Python prints an identifying description instead, when asked to describe a function:
>>> max
<built-in function max>
We can use assignment statements to give new names to existing functions.
>>> f = max
>>> f
<built-in function max>
>>> f(2, 3, 4)
4
And successive assignment statements can rebind a name to a new value.
>>> f = 2
>>> f
2
In Python, names are often called variable names or variables because they can be bound to different values in the course of executing a program. When a name is bound to a new value through assignment, it is no longer bound to any previous value. One can even bind built-in names to new values.
>>> max = 5
>>> max
5
After assigning max to 5, the name max is no longer bound to a function, and so attempting to call max(2, 3, 4) will cause an error.
When executing an assignment statement, Python evaluates the expression to the right of = before changing the binding to the name on the left. Therefore, one can refer to a name in right-side expression, even if it is the name to be bound by the assignment statement.
>>> x = 2
>>> x = x + 1
>>> x
3
We can also assign multiple values to multiple names in a single statement, where names on the left of = and expressions on the right of = are separated by commas.
>>> area, circumference = pi * radius * radius, 2 * pi * radius
>>> area
314.1592653589793
>>> circumference
62.83185307179586
Changing the value of one name does not affect other names. Below, even though the name area was bound to a value defined originally in terms of radius, the value of area has not changed. Updating the value of area requires another assignment statement.
>>> radius = 11
>>> area
314.1592653589793
>>> area = pi * radius * radius
380.132711084365
With multiple assignment, all expressions to the right of = are evaluated before any names to the left are bound to those values. As a result of this rule, swapping the values bound to two names can be performed in a single statement.
>>> x, y = 3, 4.5
>>> y, x = x, y
>>> x
4.5
>>> y
3