Names, variables, and assignment

Published

2023-07-31

Variables and assignment

You have already written a “Hello, World!” program. As you can see, this isn’t very flexible—you provided the exact text you wanted to print. However, more often than not, we don’t know the values we want to use in our programs when we write them. Values may depend on user input, database records, results of calculations, and other sources that we cannot know in advance when we write our programs.

Imagine writing a program to calculate the sum of two numbers and print the result. We could write,

print(1 + 1)
print(2 + 2)
...

but that’s really awkward. For every sum we want to calculate, we’d have to write another statement.

So when we write computer programs we use variables. In Python, a variable is the combination of a name and an associated value which has a specific type.1

It’s important to note that variables in a computer program are not like variables you’ve learned about in mathematics. For example, in mathematics we might write a + b = 5 and, of course, there’s an infinite number of possible pairs of values which sum to five.

When writing computer programs, variables are rather different. While the same name can refer to different values at different times, a name can refer to only one value at a time.

Assignment statements

In Python, we use the = to assign a value to a variable, and we call = the assignment operator. The variable name is on the left-hand side of the assignment operator, and the expression (which yields a value) is on the right-hand side of the assignment operator.

a = 3        # the variable named `a` has the value 3
print(a)     # prints 3 to the console
a = 17       # now the variable named `a` has the value 17
print(a)     # prints 17 to the console

Assignment is a kind of statement in Python. Assignment statements associate a name with a value (or, in certain cases, can modify a value).

Beginners often get confused about the assignment operator. You may find it helpful to think of it as a left-pointing arrow.2 When reading your code, for example

a = 42

it may help to say, “Let a equal 42”, or “a gets 42”, rather than “a equals 42” (which sounds more like a claim or assertion about the value of a). This can reinforce the concept of assignment.3

Dynamic typing

In Python, all values have a type, and Python knows the type of each value at every instant. However, Python is a dynamically typed language. This means that any given name can refer to values of different types at different points in a program. So this is valid Python:

a = 42      # now `a` is of type int
print(a)    # prints 42 to the console
a = 'abc'   # now `a` is of type str
print(a)    # prints 'abc' to the console

Evaluation and assignment

Sometimes we can use a variable in some calculation and reassign the result. For example:

x = 0
print(x)    # prints 0 to the console
x = x + 1
print(x)    # prints 1 to the console
x = x + 1
print(x)    # prints 2 to the console

What’s going on here? Remember, = is the assignment operator. So in the code snippet above, we’re not making assertions about equivalence; instead, we’re assigning values to x. With:

x = 0

we’re assigning the literal value 0 to x. At this point we can say the value of x is 0.

Consider what happens here:

x = x + 1

So first, Python will evaluate the expression on the right, and then it will assign the result to x. At the start, the value of x is still zero, so we can think of Python substituting the value of x for the object x on the right hand side.

x = 0 + 1

and then evaluating the right-hand side:

x = 1

and assigning the result to x. Now the value of x is 1. If we do it again,

x = x + 1

now the x on the right has the value 1, and 1 + 1 is 2, so the variable x has the value 2.

Variables are names associated with values

What are variables in Python? Variables work differently in Python than they do in many other languages. Again, in Python, a variable is a name associated with a value.

Consider this code:

>>> x = 1001
>>> y = x

What we’ve done here is give two different names to the same value. This is A-OK in Python. What does x refer to? The value 1001. What does y refer to? The exact same 1001.4 It is not the case that there are two different locations in memory both holding the value 1001 (as might be the case in a different programming language).

Now what happens if we assign a new value to x? Does y “change”? What do you think?

>>> x = 2001
>>> x
2001
>>> y
1001

No. Even though x now has the new value of 2001, y is unchanged and still has the value of 1001.

When we assign a value to a variable,

>>> x = 1001

what’s really going on is that we’re associating a name with a value. In the above example, 1001 is the value, and x is a name we’ve given to it.

Values can have more than one name associated with them. In fact, we can give any number of names to the same value.

>>> x = 1001
>>> y = x
>>> z = y

Now what happens if we assign a new value to x?

>>> x = 500
>>> x 
500
>>> y
1001
>>> z
1001

y and z are still names for 1001, but now the name x is associated with a new value, 500.

While it’s true that values can have more than one name associated with them, it’s important to understand that each name can only refer to a single value (or object). x can’t have two different values at the same time.

>>> x = 3
>>> x
3
>>> x = 42  # What happened to 3? Gone forever.
>>> x
42

Comprehension check

Given the following snippets of Python code, determine the resulting value x:

x = 1
x = 1
x = x + 1
y = 200
x = y
x = 0
x = x * 200
x = 1
x = 'hello'
x = 5
y = 3
x = x + 2 * y - 1

Constants

A lot of the time in programming, we want to use a specific value or calculation multiple times. Instead of repeating that same value or calculation over and over again, we can just assign the value to a variable and reuse it throughout a program. We call this constant. A constant is a variable that has a value that will be left unchanged throughout a program. Using constants improves the readability of programs because they provide meaningful and recognizable names for fixed values. Let’s look at an example:

HOURS_IN_A_DAY = 24

Here we have assigned the variable HOURS_IN_A_DAY to 24. This variable is a constant because the number of hours in a day will always be 24 (at least for the foreseeable future). Now if we need to do some calculation using the number of hours in a day, we can just use this variable. Note that constants are uppercase. This isn’t enforced by Python, but it’s good common practice.

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).

Footnotes

  1. Python differs from most other programming languages in this regard. In many other programming languages, variables refer to memory locations which hold values. (Yes, deep down, this is what goes on “under the hood” but the paradigm from the perspective of those writing programs in Python is that variables are names attached to values.) Feel free to check the entry in the glossary for more.↩︎

  2. In fact, the left-facing arrow is commonly used to indicate assignment in pseudocode—descriptions of algorithms outside the context of any particular programming language.↩︎

  3. Later on, we’ll see the comparison operator ==. This is used to compare two values to see if they are identical. For example, a == b would be true if the values of a and b were the same. So it’s important to keep the assignment (=) and comparison (==) operators straight in your mind.↩︎

  4. We can verify this by inspecting the identity number of the object(s) in question using Python’s built-in id() function.↩︎