Variables and Data Types
You may have seen the terms variables and data types being thrown around here and then, but what exactly do they mean? Why do you keep hearing about them? How are they relevant to Scratch?
Let’s start by talking about variables. In the context of programming, a variable is sort of like a container for information: the point of a variable is to label and store information. We use them for a few reasons, but before I go into these, what better way to illustrate what they are than with an example?
1. Variables can make our code a lot more readable.
Let’s say that you have a Python program (don’t worry, you don’t need to know anything about Python to understand this example — I’ll explain everything you need to know!): you’re writing a program that produces a random number and prints it to the screen.
Here’s an example of the above piece of code without using variables:
Above is a program which generates a new random number each time. Click on the pencil to view the code, and the forward arrow to run the program.
To simplify things a bit, line 1 simply allows us to generate random numbers in Python. Without this line, Python won’t understand what you’re trying to do.
Line 2 does two things: randint(1, 100) generates a random number between 1 and 100. The print() part then prints the randomly generated number to the screen.
Here’s a piece of code that does the exact same thing, this time using variables:
Above is a program which generates a new random number each time. Click on the pencil to view the code, and the forward arrow to run the program.
2. Variables enable us to reference and manipulate the same data in multiple places in a program.
Variables also make it easy to change and reference the same piece of information elsewhere in the code. Let’s extend upon our earlier example, shall we? Let’s say that, after our program prints the randomly generated number to the screen, we want it to generate a new number and print that to the screen. We can do it like this:
Above is a program which generates two new random number each time. Click on the pencil to view the code, and the forward arrow to run the program.
(There is a more efficient way to write this program, but for the sake of simplicity, let’s go with this!)
If we run through the code from top to bottom, you’ll see that line 5 is the first place that the variable randomlyGeneratedNumber is assigned a value. This value is then printed to the screen, thanks to the code on line 7. You’ll then see on line 9 that randomlyGeneratedNumber is reassigned a new value. This new value is then printed to the screen thanks to the code on line 11.
Do you see how we can change and reuse variables? It’s an invaluable tool to have while you’re coding.
Fun fact: There are a couple of conventions that you should know variables:
- Variables need to be one word: they can’t have spaces, so that’s why you’ll see names like “randomly_generated_number” (using underscores) or “randomlyGeneratedNumber” (camel case) instead of “randomly generated number”.
- They need to be one word: that’s why instead of calling our variable “randomly generated number”, we call it “randomlyGeneratedNumber”
- Names of variables should be meaningful, so that you can be reminded of what information it contains.
More about variables
Let’s talk more about the features of variables. One thing that’s great about them is that they can store many different types of information. You saw in the above Python examples that variables can store numerical information like numbers, but they can also store other kinds of information, like alphanumeric information (e.g. letters and characters). There are many other types of data — or data types — that variables can embody, but let’s keep it simple for now.
Variables can also only hold one piece of information at a time. If you want to store multiple bits of information using in the same variable name, then you’ll have to use a list instead! We’ll talk about what lists are and how to use them in a separate post.
Variables in Scratch
We’ve already addressed that variables can be manipulated or changed at different points in a program. They have many uses, but in something like Scratch, one of the most common uses you’ll see for variables are as containers for pieces of information like score counts in games.
You can make a variable by going to the Data category in Scratch, and clicking “Make a Variable”, like so:
You’ll be able to enter a name for your variable:
You’ll also see two other options under the name field: For all sprites, and For this sprite only. We’ll be discussing what these two options mean in the next post.
Recap
So, let’s recap what we’ve learned, shall we?
- Variables are like containers: they store information, but they can only hold one piece of information at a time.
- They can store various types of data, such as numerical information or alphanumeric information.
- We use variables for a number of reasons, but two important ones are:
- They make code more readable
- They allow us to reference and manipulate the same piece of information in multiple places
- Variables in Scratch are commonly used for things like keeping count of scores.
- We can create variables in Scratch by clicking the “Make a Variable” button in the Data category.
Happy Scratching!
By Erika Chumpia
Check out our free teacher download. Learn to code with Scratch in 30 minutes.
If you’re interested to find out more