}
Next Previous Table of Contents

Variable Assignment

Naming things is one of the most common and fundamental operations in programming. In Python, we assign names to data using =:

>>> greeting = "Hello!"
>>> greeting
'Hello!'
>>> miles_home = 11
>>> walking_speed_in_mph = 2.2
>>> hours_home = miles_home / walking_speed_in_mph
>>> hours_home
5.0

Giving names to data is useful, both for you, the programmer, and the computer.

On the computer side, assigning a name tells Python to keep track of that information from now on. Python puts that data in the computer's short-term memory and keeps track of it as long as the Python process is running or until something changes to tell Python to get rid of it. If a piece of data loses its name, Python will generally toss it away. When we enter something like 10 into the REPL...

>>> 10
10

...Python winds up throwing away that data, because it doesn't get a name.

Naming things is also useful on the people side. Programming is not only about computation, or the process of doing math inside a computer. It's about making meaning. Without making meaning, we cannot hope to have computers do anything useful for us, because the computer won't understand us, and we won't understand what the computer is doing.

In the above example, we could calculate the number of hours it will take to get home without using variables: 11 / 2.2. However, that won't help us, or another person, understand the purpose of the program.

Programmers use a specific word when talking about meaning: "semantics." If a variable describes its purpose well, it's called semantic.

Communication is hard. Just as writing well is often difficult, communicating intent while coding is also a challenge.

Next Previous Table of Contents