What’s a Variable (With Examples in Python)

What’s a Variable (With Examples in Python)

Comment Icon0 Comments
Reading Time Icon11 min read

Imagine we’re going to build an app!

Just a simple app.

It’s going to ask the user to enter two numbers. Then the app will show the sum of the two numbers. Like this; here’s the first screen:

Your friend enters 10 and clicks Next. Then the next screen opens:

Your friend types in 5 and clicks next; then the answer appears on the next screen:

Think about how you would do this if you’re a human pretending to be the app.

You ask the person their favorite number. They say 10.

What happens next? Well ideally you’ll move on to the next question. But what are you doing with the first number, 10?

You’re remembering it. You’re putting it into your memory. If you don’t, then you won’t be able to add the second number to it, because you already forgot it. Not good!

Computers need to do the same. After the app asks the user the first number, the computer needs to remember it. So let’s talk about computer memory briefly.

Some Brief Thoughts about Computer Memory

Most computers today have somewhere around 16 Gigabytes or even 32 Gigabytes of memory. That memory is laid out as a long sequence of little slots of memory, each one eight bits long. Eight bits makes up a byte. A Gigabyte has one billion bytes. So 16 Gigabytes of memory is 16 billion bytes of memory.

The bytes of memory are numbered with an “address” similar to the address on the front of a house.

  • The first byte gets address 0.
  • The next byte gets address 1.
  • The next gets address 2.

And on and on until you reach the end, such as 32 billion bytes, which we obviously won’t list all here. But since we started at 0 for the first byte, the last one won’t be 32,000,000,000. Instead it will be one less than that since the 1st got address 0. So the last one will have an address of 31,999,999,999,999.

 

Pro tip! In computer programming, when we’re listing things in order, we don’t number things starting with 1. We usually start with 0. So if we have a list of favorite animals, the first one in the list is numbered 0. The second is numbered 1, and so on. The memory addresses work the same way.

But remember: If we have 5 favorite animals, and we number starting with 0, then the numbers are:

0, 1, 2, 3, 4

The last number is 4, even though there are 5 animals.

 

So how do we store our two numbers in memory, the first favorite and second favorite? We let the computer pick a slot in memory, but we provide a name for that memory slot. And we store the number they typed into that slot.

For example, we might have three slots in memory, one called FirstFavorite the second called SecondFavorite, and the third called Sum.

(Where in memory are these, as in memory addresses? It doesn’t matter. Every time our app runs, the computer will pick a different place in memory with a different address. That’s why we name the slot. It gets a lot easier.)

These slots don’t have anything in them yet. (Technically when we create a slot they contain all 0s, but we’ll treat them as “empty.”)

Now after our friend types in a 10 for the first favorite, we’ll store that in the first slot like so:

So we’ve put a 10 in the slot called FirstFavorite.

Next our friend typed in a 5 for the second favorite. So let’s put that in the slot named SecondFavorite:

And now the fun part! How will we calculate the sum? We’ll take what’s in FirstFavorite, and add it to what’s in SecondFavorite. Think of it like this:

FirstFavorite + SecondFavorite

(We’re not actually adding the word FirstFavorite to the word SecondFavorite. We’re adding what’s in the two slots with those names.)

So we’ll see there’s a 10 in FirstFavorite, and we’ll add it to the 5 we see in SecondFavorite, and we’ll get 15. And we’ll put that in the third slot, the one named Sum:

And then we’ll display what’s in Sum, 15, on the screen.

How to do this in code

Next let’s see how we would do this in code.

For starters, let’s skip the part about asking our friend. Instead, let’s just make the assumption that our user’s first favorite number is 10, and the user’s second favorite number is 5. (We’ll see later how to actually ask the user, don’t worry.)

Here’s a line of computer code that will put a 10 inside the slot, or variable, called FirstFavorite:

FirstFavorite = 10

Don’t read too much into this! This line simply means we’re taking what’s on the right of the equal sign, in this case 10, and putting it in the slot whose name is on the left side of the equal sign, in this case FirstFavorite.

Again: This line of code means we’re taking a 10 and placing it in the slot named FirstFavorite.

(Note for those of you who took algebra: That line of code does NOT– I repeat DOES NOT –have anything to do with solving an algebra problem. We’re not doing algebra here. It’s just the way we store something in a variable using computer code.)

Next, to put a 5 in the second slot, we would type something like this:

SecondFavorite = 5

In terms of computer programming, we would actually put those two lines one after the other, in order:

FirstFavorite = 10
SecondFavorite = 5

The computer will then run those two lines of code, starting with the first line, then the second line.

Now before we work on the sum part, let’s play with these variables for a bit.

What happens if we put another number in a variable?

What if we want to put a 7 in the variable called SecondFavorite? We do this:

SecondFavorite = 7

When we do that, what happens to the 5? It gets replaced.

That means the SecondFavorite no longer has a 5 in it. It now has a 7. So let’s say we have these three lines of code:

FirstFavorite = 10
SecondFavorite = 5
SecondFavorite = 7

When we “run” these three lines of code in order, the second line will put a 5 in the variable or slot called SecondFavorite, and the third line will put a 7 in that same variable, replacing the 5. So after these three lines of code run, SecondFavorite now has a 7 in it.

Regular variables like these can only hold one thing at a time. (Later we’ll learn about more advanced types of variables that can hold multiple things! But even then they’re really just a sequence of items that take up multiple memory slots, and the fact remains, a single slot can only hold one number at any time.)

Pro Tip: About that word “Variable”

Most of the terms we use in computer programming date literally decades back, like from the 1950s and 1960s. Many of the early programmers (most of whom are no longer with us) had degrees in math. When they needed words to describe what they were doing, they used words from the field of math. And many of the original definitions are really outdated.

The word “variable” is an example. It actually comes from the word “vary” as in something can vary. But that doesn’t really apply here, unless we stretch our thinking and force the definition to fit. (You can, but it’s not worth it.)

So just take the word at face value. Don’t worry about other definitions. Ignore any definitions outside of programming. A “variable” is just a slot in memory that has a name. As for what “variable” means outside of programming, it doesn’t matter.

PRO TIP: You’ll find that happens a lot in programming. You’ll find we’re using some word that might be an English word and have some other meaning outside of programming. Don’t worry about that other meaning. It usually has no relationship to programming. And you’re better off ignoring the regular English meaning of a word that’s used in programming.

Continuing our Exploration of Variables

What if we want to find out what’s in a variable? We just type the name of the slot. For example, if we want to see what’s in SecondFavorite, we just type “SecondFavorite.” By itself that’s not very useful. But how about we take what’s in SecondFavorite (currently a 7) and save it in the slot named FirstFavorite? Here’s what we would type:

FirstFavorite = SecondFavorite

This will NOT put the word “SecondFavorite” inside FirstFavorite. (There are ways to do that, though, which we’ll see shortly.)

Instead, this takes what’s inside SecondFavorite, currently a 7, and put that 7 inside the variable named FirstFavorite.

Let’s break this down.

Remember, the stuff on the right side of the equal sign is what we’re going to get, and we’re going to put what we found into the variable named on the left of the equal sign.

Again:

Whatever is on the right of the equal sign…

Gets put in the variable named on the left side of the equal sign.

What’s on the right? It’s the name of a variable, and that means we take what’s in that variable’s slot in memory, a 7.

And we’re going to store it in the variable named on the left, FirstFavorite.

So this code will take the 7 that’s stored in SecondFavorite and store it in FirstFavorite.

The SecondFavorite variable remains unchanged. We took what was in it, 7, and copied it over into FirstFavorite. The 10 that was in FirstFavorite gets replaced with a 7, so now both variables hold a 7.

Pro-tip: Regarding the language of variables, we programmers often get sloppy.

If we see a line like this:

FirstFavorite = 12

We don’t usually say “I’m going to put a 12 in the variable whose name is FirstFavorite.” We usually get sloppy and just say, “I’m putting a 12 in FirstFavorite.” It’s not technically correct; we’re not putting a 12 inside a word called FirstFavorite, but most people know what we mean. And in fact, pretty much every programmer on the planet (and beyond if you count the folks on the Space Station) says it like this. So you can too. It’s fine for you to say something like, “This code stores a 12 in FirstFavorite.”

Now let’s do Calculations!

Now how do we calculate the sum and store it in the variable named Sum? We do it like this:

Sum = FirstFavorite + SecondFavorite

This follows the same pattern:

Take what’s on the right

And store it in the variable named on the left.

What is on the right? The sum of whatever is in FirstFavorite and whatever is in SecondFavorite. You can probably see that by now, but let’s go through in extreme detail so we’re all clear:

  1. Grab what’s in the slot named FirstFavorite (which is a 10)
  2. Grab what’s in the slot named SecondFavorite (which is a 5)
  3. Add them up to get 15
  4. Take that result, 15, and store it in the slot named Sum

So here are the lines of code:

FirstFavorite = 10
SecondFavorite = 5
Sum = FirstFavorite = SecondFavorite

We put a 10 in FirstFavorite, then a 5 in SecondFavorite, and add those two together to get 15 and store it in Sum.

Tip: Look at these three lines and get comfortable with the concepts:

  • We have three lines that run in order from the first line through the third line.
  • Each line has an equal sign, which means the stuff on the right gets put in the variable whose name is on the left.
  • The first two lines just put a number in a variable.
  • The third line takes what’s inside those two variables, adds those values (510 and 5) together, and stores that answer in the variable named Sum.

And that’s it!

Tip: Oh yeah, one more thing: How do you ask the user for their numbers? Well, that requires some more code in the language we prefer (we’ll use Python). I’ll post that in another blog shortly!

Examples in Python

Pro-tip: Many, but not all, programming languages today end their statements with a semicolon. That’s just how the computer knows the line is done. So most of these examples have semicolons at the end. But Python is an exception. With Python, the computer knows the statement is done simply because it’s the end of the line.

So here’s a screenshot of the above code in Python, along with another line of code you can play with:

Share this article

About Author

jeffrey

My name is Jeffrey Cogswell and I've written and taught for 30 years and worked full time as a developer in languages such as Python, C#, Java, and C++.

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Relevent