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.
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.
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 variables in different ways, depending on what we want to do with them.
Legal Names and Conventions
There are some rules we must follow when we create new variables. Some names are ‘legal’, and some aren’t.
Some variable names are perfectly legal to use. But not all are recommended. Naming Conventions make our code easier to read.
Up next, we’re going to start using Python files.