What Are Variables?

All programming languages use variables. They’re very important to most programs.

Think of variables like a jar. You could store many different types of things in the jar. Things like candy, marbles, flour, sand, anything that can fit really.

You can put things in a jar to store them. You can take a few things out, you can put more in, or you can empty the jar completely.

You can have many jars, all with different things in them. You can put labels on them, so you know what each one is for.

A jar of candy is like a variable

Just like our jars, we can use variables to store information. We give each variable a name, like a label on a jar. We can then store information, like numbers and strings, in the variable.

A variable is a ‘container’ that stores some information

Using Variables

Let’s see how variables are used in Python. Open a shell, and type this command:

marbles = 10

We have now created our first variable! The name of the variable is marbles. This is like the label that goes on the jar.

We’ve also put the value 10 in this variable. This is like saying “There are 10 marbles in the jar”

The equals symbol is known as an assignment operator. This is a fancy way of saying that it puts some value into the variable. In our example, we have assigned the value of 10 to the variable marbles.

Working with Variables

We can use many variables in our programs

We can use variables in different ways, depending on what we want to do with them.

Legal Names and Conventions

Not all variable names are legal!

Legal Names

There are some rules we must follow when we create new variables. Some names are ‘legal’, and some aren’t.

There are some variable names that will work fine, but should be avoided.

Naming Conventions

Some variable names are perfectly legal to use. But not all are recommended. Naming Conventions make our code easier to read.

Next…

Up next, we’re going to start using Python files.