Young programmers need to understand the purpose of Python variables. In a program, variables store values that can change. They are very powerful. They can be used to count, create lists, store user input, report information, personalize the user experience, trigger an action, calculate amounts, and more!
One of the best ways to learn about computer science concepts is hands-on learning. In other words, writing programs that use variables. Talking about variables will generate a limited understanding. Having said that, before jumping into Python programming, it is worthwhile to begin instruction about variables with an offline coding exercise.
Variables can seem a bit abstract to a child. It is worthwhile to make the concept tangible. A quick activity that connects variables to daily life will help elementary and middle school students to form an understanding of their use.
Have your students apply computational thinking to think about the world around them as a set of variables that store and direct events. This knowledge can then be transferred to programming activities. Refer to the connections below as a way to create an offline coding exercise about Python variables.
The Value of a Variable Changes
Just like the weather, a variable is a value that changes. It could be sunny in the morning and then rainy in the afternoon. What is the weather?
Complete the value of the variable weather. For example: weather=”sunny”
weather=” “
A Variable Stores Different Types of Information
A variable can be text, a number, or a list. In the above example, the value stored was a word. However, you can also store numbers as variables. Temperature is a numbered variable that changes. It could be 5°C (41°F) at night and then 15°C (59°F) in the afternoon.
Write the value of the variable temperature. For example: temperature=”15″
temperature=” “
The Value of a Variable Can Trigger an Event
The value of a variable can cause an action to occur. For example, if it is hot outside you might wear shorts. Or if it is rainy you might use an umbrella.
Complete the script using weather as the variable. What will you wear?
if weather==”snowy”:
what will you wear?
An Event Can Trigger the Value of a Variable to Change
When a program starts, a variable has a specific value. However, an event may occur that changes the value. This event could make a number higher or lower. Or, it could assign a new word to a text variable.
When making a decision you might change your mind depending on what is happening. Programs do the same thing! Pretend you are getting dressed for school. What piece of clothing do you want to wear?
Complete the value of the variable clothing
clothing=” “
The item is in the laundry. What piece of clothing will you wear now?
clothing=” “
A Variable Is Stored in a Numbered Location
The value of a variable is saved in a spot that is like a numbered storage bin. When the program needs the value in the variable it takes it from the bin. The computer knows where to find the value because it has a unique location.
Labeling a location to store things is done everyday at school. For example, your school locker may have a number. Or the library may have a numbered bin of books.
Look around your classroom. What bins are used to store things? Are they labelled?
Write your discovery as a variable. For example: mylocker=”1215″ or period1bin=”homework”
bin=”item”
A Variable Is an Efficient Way to Refer to Information
A variable is like a container that stores information. The label on the outside is the name. The item you put inside is the value. When giving an instruction, the program uses the variable name, not the data stored in it. This is done to save time and keep things simple.
Imagine it is recess time. The teacher needs to tell the students what to do. The teacher gives the instruction, “Please go and get your snack”. The teacher does not say, “Please go and get your apple, crackers, cookies, carrots, chips, or banana.”
The word “snack” is used to refer to many values. It also allows the values to be unknown. The teacher cannot know what you or your classmates brought for a snack. Using one word that is meaningful is an efficient way to refer to information.
People often use one word to quickly refer to many things. Turn the word lunch into a variable. What is in your lunch today? Make a list of the values in your lunch. For example:
lunch=(“orange juice”, “ham sandwich”, “apple”, “blueberry muffin”)
lunch=
A Variable Can Store User Information
Sometimes the programmer assigns the value of a variable. Other times, the user inputs a value. This can be done by prompting the user to enter data by displaying a text box or question on the screen. The program can then use this information to sign into an account, customize the settings of an app, or pick a selection.
Think about a game you play on a device. What information does it ask you to input? Each of the pieces of information is a variable.
List two pieces of information you must enter into a game before you could play it. For example:
playername=”technokid”
playermode=”single player”
Python Variables and Fun Programming Activities for Kids
If you are looking for some fun programming activities for kids that use variables, take a look at TechnoTurtle. This STEM project introduces beginners to Python variables in a way that is easy to understand. Students develop programs that have players play Mad Libs, a Carnival Game, and a Guess the Number Game. Each uses variables to trigger action. These coding activities are a great way to make Python variables meaningful to kids. The lessons provide a foundation for further learning.