The Power of Variables: Storing and Using Information

The Power of Variables

Variables are one of the most important concepts in programming. Read on to learn how to think about them intuitively.

Reading Time 4 mins

One of the most important foundational building concepts in programming is variables. Variables are a way to make a computer remember values. A variable is a label to which a value can be assigned.

Variables allow programmers to give meaning to values and use those values throughout the program easily. For example, the number 238900 doesn’t mean anything but if we label it as “the distance between the Moon and the Earth” it’s easier to reference. We don’t have to remember the exact value as we can look it up using the phrase “distance between the Moon and the Earth.”

Here’s how we would use a variable in Python:

Python code declaring a variable called “distance_between_moon_and_earth” and printing it to the console.


Python code showing the integer value “238900.”

We can use “_” (underscore) to make big numbers easier to read in Python. Notice that we’re also using the “_” to separate words in the variable name.

Variable Naming Conventions

It is important to follow consistent guidelines when naming variables. The projects you work on will likely have their own guidelines. (Check out Google’s Python style guide as an example.)

The following are common Python variable naming conventions used in the industry:

  • Variable names can only contain letters, numbers, and underscores (_).
  • They can only start with a letter or an underscore (greeting1 and _greeting1 are valid but 1greeting is not).
  • Variables are case-sensitive (greeting and Greeting are separate variables).
  • Multiple words must be separated using _ (my_birthday, number_of_people). This convention is called snake_case.
  • Variable names must be meaningful (max_speed instead of m).

Visualizing Computer Memory

Whenever we ask the computer to represent a string, number, or boolean it creates the representation in its memory. You can think of computer memory as a giant container that can store information for the duration of a program’s execution.

At the start of program execution, the computer’s memory is empty. We can visualize it like this:

Blank rectangle representing empty memory

Now let’s run some code and visualize how the memory will change during execution:

Python code with 4 print statements where each line prints a value of different data type.

For each of the values, a representation will be created in the memory during execution.

Rectangle with various primitive values

Once the program completes execution, the memory will be empty again.

Note that if we had two print(1) statements, there would still be only a single representation of the integer 1. This is the case for strings and booleans too.

Variable Declaration and Assignment

Let’s look at how to create a variable again:

Python code declaring a variable called “cat_name” and assigning the value “Fifi” to it.

There are three parts to this line:

  1. cat_name is the variable name.
  2. = is an assignment operator.
  3. 'Fifi' is an expression that produces the value Fifi of type str.

So this is the general structure:

General expression for declaring a variable name and assigning an expression to it.
  • The right side of the = operator is evaluated first.
  • The value produced by the expression evaluation is then assigned to the variable name or label.

The line cat_name = 'Fifi' is usually verbalized as “declare a variable called cat_name and assign the value ‘Fifi’ to it.

Visualizing declaration and assignment

We are going to visualize the following line:

Python code declaring a variable called “cat_name” and assigning the value “Fifi” to it.

First, the computer evaluates the expression which in this case produces the string 'Fifi' and stores it in the memory.

Rectangle with the value”Fifi”

The computer would then declare the variable with the given name. Notice that the string is in '' while the variable name is not.

Rectangle with the variable label “cat_name” and the value “Fifi”

Finally, the computer makes the variable name point to the value (i.e., it assigns the value to the variable label).

Rectangle showing an arrow going from the variable label “cat_name” to the value “Fifi.”

Variable Access

We’ve seen how a computer declares variables and assigns values to them. Now we will look at what happens when the computer accesses a variable.

Here’s the code we will be working with:

Python code declaring a variable called “cat_name”, assigning the value of “Fifi” to it, and printing it to the console.

When a computer sees a variable label it checks its memory for the label. If the label exists, the value that the label points to is used by the computer.

We can visualize this by checking the memory diagram and following the arrow from the variable label (cat_name) to the value (’Fifi’).

Variable Reassignment

Variable values can be changed during the execution of the program. For example:

Python code declaring a variable called “current_year” and assigning the value of “2023” to it. The value is reassigned to “2024” and printed to console.

The first line will result in the following memory state:

Rectangle showing an arrow from the variable label “current_year” to the integer value 2023.

On the second line, the computer will look up the current_year label in its memory and display the value 2023.

With the third line, a few things happen:

  • The computer creates the representation of 2023 in its memory (remember the right-hand side of the = operator is evaluated first). 
Rectangle showing an arrow from the variable label “current_year” to the integer value 2023 and the integer value 2024 without anything pointing to it.
  • The current_year label arrow is pointed to the 2024 representation.
Rectangle showing an arrow from the variable label “current_year” to the integer value 2024 and the integer value 2023 without anything pointing to it.
  • Since no variables are pointing to the 2023 value anymore, it gets removed from memory.
Rectangle showing an arrow from the variable label “current_year” to the integer value 2024.

On the 4th and final line, the computer checks its memory for the current_label again but this time it’s pointing to a new value (2024).

As a reminder, this mental model works for strings and booleans too!

Hopefully you now have an understanding of how to visualize variable declaration and assignment. These visualizations can also be used for reference type values such as arrays and objects. Whenever you’re using variables, make sure to follow a consistent guideline and identify what value a variable is referencing. With these foundations, you will be able to effectively use variables in any programming language.

Make a Software Engineering Career Change at Flatiron

Our Software Engineering Bootcamp offers hands-on training, expert technical coaching, and real-world projects designed to launch your career in tech. Don’t miss this opportunity to join a vibrant community of developers and unlock limitless possibilities. Apply now and fast-track your path to success in the dynamic field of software engineering.

Disclaimer: The information in this blog is current as of April 12, 2024. Current policies, offerings, procedures, and programs may differ.

About Alvee Akand

Alvee Akand is the senior curriculum manager for software engineering at Flatiron School. He has a degree in electrical engineering and loves building communities.

More articles by Alvee Akand