Chapter 6. Variables, Assignment and Input
As a multi-statement program makes progress from launch to
completion, it does so by undergoing changes of state. The state of our
program as a whole is the state of all of the program's variables. When
one variable changes, the overall state has changed.
Variables are the names your program assigns to the results of an
expression. Every variable is created with an initial value. Variables
will change to identify new objects and the objects identified by a
variable can change their internal state. These three kinds of state
changes (variable creation, object assignment, object change) happen as
inputs are accepted and our program evaluates expressions. Eventually the
state of the variables indicates that we are done, and our program can
A Python variable name must be at least one letter, and can have a
string of numbers, letters and
_'s to any length. Names that
__ have special significance.
Names that begin with
_ are typically private to a module or
class. We'll return to this notion of privacy in Chapter 21, Classes
and Chapter 28, Modules
that begin with
__ are part of the way the Python interpreter
Example variable names include
Python creates new objects as the result of evaluating an
expression. Python creates new variables with an
statement; it also assigns an object to the
variable. Python removes variables with a
Some Consequences. A Python variable is little more than a name which refers to an
object. The central issue is to recognize that the underlying object is
the essential part of our program; a variable name is just a meaningful
label. This has a number of important consequences.
One consequence of a variable being simple a label is that any
number of variables can refer to the same object. In other languages (C,
C++, Java) there are two kinds of values: primitive and objects, and there
are distinct rules for handling the two kinds of values. In Python, every
variable is a simple reference to an underlying object. When talking about
simple immutable objects, like the number 3, multiple variables referring
to a common object is functionally equivalent to having a distinct copy of
a primitive value. When talking about mutable objects, like lists,
mappings, or complex objects, distinct variable references can change the
state of the common object.
Another consequences is that the Python object fully defines it's
own type. The object's type defines the
representation, the range of values and the allowed operations on the
object. The type is established when the object is created. For example,
floating point addition and long integer objects have different
representations, operations of adding these kinds of numbers are
different, the objects created by addition are of distinct types. Python
uses the type information to choose which addition operation to perform on
two values. In the case of an expression with mixed types Python uses the
type information to coerce one or both values to a common type.
We've already worked with the four numeric types: plain integers,
long integers, floating point numbers and complex numbers. We've touched
on the string type, also. There are several other built-in types that we
will look at in detail in Part II, “Data Structures”. Plus, we can use class
definitions to define new types to Python, something we'll look at in
Part III, “Data + Processing = Objects”.
Note that a static language associates the type information with the
variable. Only values of a certain type can be assigned to a given
variable. In Python, a variable is just a label or tag attached to the
object. Any variable can be associated with a value of any type.
The final consequence of variables referring to objects is that a
variable's scope can be independent of the object itself. This means that
a variables which are in distinct namespaces can refer to the same object.
When a function completes execution and the namespace is deleted, the
variables are deleted, and the number of variables referring to an object
is reduced. Additional variables may still refer to an object, meaning
that the object will continue to exist. When only one variable refers to
an object, then removing the last variable removes the last reference to
the object, and the object can be removed from memory.
Also note that a expressions generally create new objects; if an
object is not saved in a variable, it silently vanishes. We can safely
ignore the results of a function.
Scope and Namespaces. A Python variable is a name which refers to an object. To be
useful, each variable must have a scope of
visibility. The scope is defined as the set of statements that can make
use of this variable. A variable with global
scope can be referenced anywhere. On the other hand,
variable with local scope can only be referenced
in a limited suite of statements.
This notion of scope is essential to being able to keep a
intellectual grip on a program. Programs of even moderate complexity need
to keep pools of variables with separate scopes. This allows you to reuse
variable names without risk of confusion from inadvertantly changing the
value of a variable used elsewhere in a program.
Python collects variables into pools called
namespaces. A new namespace is created as part of
evaluating the body of a function or module, or creating a new object.
Additionally, there is one global namespace. This means that each variable
(and the state that it implies) is isolated to the execution of a single
function or module. By separating all locally scoped variables into
separate namespaces, we don't have an endless clutter of global
In the rare case that you need a global variable, the
statement is available to assign a variable to
the global namespace.
When we introduce functions in Chapter 9, Functions
in Chapter 21, Classes
and modules in Part IV, “Components, Modules and Packages”, we'll revisit this namespace technique for managing
scope. In particular, see the section called “Functions and Namespaces” for a
digression on this.