Intro to Variables // Grandma’s Memory

mary
3 min readMar 15, 2021

In this quick read, I introduce you to the concept of variables in computer programming.

Grandma Knows All the Drama

I think it’s safe to say that every family has at least one person in it that remembers everything: birthdays, anniversaries, the maiden name of your third cousin’s aunt-in-law… the list goes on.

For me, that person was my grandma. Nothing could slip by her; she knew and remembered everything. And if that third cousin’s aunt-in-law fell asleep at a wedding years ago? Yep, as soon as you mentioned her, she remembered.

In programming, there’s drama involved. There are a lot of things to keep track of — some that stay constant, and some that are continuously changing. It would be impossible to keep all that data straight without grandma’s secret to a sharp, organized mind: variables.

What is a Variable?

Shout out to Wikipedia for this formal definition:

In computer programming, a variable or scalar is a storage location (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.

Let’s tie it back to grandma. You asked her for your third cousin’s aunt-in-law’s maiden name, and she recalled, “Hoxha” (my American folks, feel free to substitute with “Smith” or “Jackson” instead 🤓).

In Python, that variable definition might look like this:

aunt_in_law_maiden = “Hoxha”

That line of code consists of three components: the variable* name, the assignment operator, and the value.

Note: in the Wikipedia definition above, they called this the symbolic name.

Components of a Variable Definition

First, I like to think of defining a variable as filling an empty box with something, and then adding a label with a Sharpie before dooming it to the attic. You’re taking a piece of data — whether it’s some text, a number, or some other value* — and storing it at a memory address in our computer’s physical hardware. We then give that address a name we can easily reference. That name is the variable name, and the piece of data is its value!

Variable Name:

This is what you will call the piece of information later in the program.

aunt_in_law_maiden

Value:

This is the literal value, or information, you want to “store” in your variable name. The value will be one of many data types, explained in the next section.

"Hoxha"

Assignment Operator:

This is the single equal sign in between the variable name and value.

aunt_in_law_maiden = “Hoxha”

*There are other kinds of operators, but we’ll address them in another post.

Data Types

Data types are exactly what they sound like: a classification of what kind, or type of data the value in your variable is.

Some examples:

Integers — whole positive and negative numbers

1, 3, 283, -13

Doubles — decimal positive and negative numbers

1.0, 3.5, 283.0, -13.41

Characters — single alphanumeric characters and symbols, enclosed in single quotes

‘2’, ‘e’, ‘E’, ‘?’, ‘ ‘

Strings — a collection of characters, enclosed in double quotes

"2", "e", "Hello, world!", " "

Booleans — a binary value of true or false, sometimes stored as 0 or 1

True, False, 0, 1

An Example

That’s it! Let’s try another example. Grandma remembered that Ms. Hoxha fell asleep at the wedding (lame). How might we declare that information as a variable?

  1. Name your variable
  2. Decide your datatype
  3. Assign your variable to an appropriate value

Name your variable

It’s extremely important to name your variable well. It should be concise and intuitive.*

hoxha_sleep_status

Decide your datatype

Since being asleep is something you either are or aren’t, this one is a boolean.

Assign your variable to an appropriate value

In this case, it was True; she was asleep!

hoxha_sleep_status = True

*One of the most important things in naming variables is to name them WELL. You may be coding alone at the moment, but your code will always end up in someone else’s hands at some point to build on; after all, most code is built upon existing code. Name your variable after what is stored in it!

Future Preview

And that’s all folks, your variable has now been defined. If you wanted to reference that status later in your program, it could look something like this:

if (hoxha_sleep_status == True) {     offer_coffee()}

This is a conditional statement that checks to see if the value in the variable is True. If it is, it runs the function inside of the curly brackets. We’ll cover both topics in future posts!

--

--