What is a variable?
In programming, we use variables to store information. By storing values in variables, we can reference and change that information anywhere in a program.
Say we create a game where players have a certain number of lives. We could use a variable to store the number of lives. In Python, you create a variable by choosing a name and assigning it a starting value:
num_lives = 5
Now say the player loses some lives and has 2 left. We change what a variable holds by assigning a different value to it:
num_lives = 2
Watch out: when assigning a new value to an existing variable, the name must match exactly. Writing num_Lives = 2 would create a brand new variable, because of the capital L. Python treats num_lives and num_Lives as two different boxes.
Try it: player lives
Here is that idea as a real program. Press Run to see what prints. Notice that print(num_lives) shows whatever value the variable holds at that moment.
# Creates a variable num_lives and assigns a value of 3 num_lives = 3 print(num_lives) # Assigns a new value to the variable num_lives num_lives = 2 print(num_lives)
3 2
Now drive the variable yourself. Each button below assigns a new value to num_lives, and everything that uses the variable updates with it:
Naming variables
When naming variables in Python, follow these rules:
- no spaces
- must start with a letter or underscore ( _ )
- all lowercase
- if there are multiple words, use an underscore ( _ ) between words - like high_score
One more tip that isn't strictly a rule: name your variable so it describes what it's holding. player_speed beats ps every time, because it makes your program easier to read and understand.
- ✓ No spaces
- ✓ Starts with a letter or underscore
- ✓ All lowercase
- ✓ Only letters, numbers, and underscores
Types of variables
So far we've only stored numbers. But variables can hold other kinds of information too. A variable's type is based on the type of data it holds. Here are the four you'll use constantly:
Integer
Whole numbers, such as -5, 0, or 15.
num_lives = 3
Float
Numbers with a decimal, such as 43.67 or -0.11.
cost = 40.25
String
Text. Strings are always surrounded by quotes.
name = "Baby Yoda"
Boolean
Either True or False. Note the capital first letter!
is_jedi = True
Take a look at the different variable types in action, then press Run:
name = "Baby Yoda" age = 140.352 is_jedi = True print(name) print(age) print(is_jedi)
Baby Yoda 140.352 True
Think about it: the boolean True has no quotes around it. If you wrote is_jedi = "True" instead, what type would the variable be? (Answer: a string! The quotes make it text.)
Practice problems
The best way to learn variables is to use them. Try these two challenges in any Python editor - or jump into the CodeHS Playground to code right in your browser.
Creating Variables
Write a program that creates 3 variables: one string, one integer, and one boolean. Make sure to use the correct naming conventions! Then print each variable on its own line using the print command.
favorite_food = "pizza" slices_eaten = 4 still_hungry = True print(favorite_food) print(slices_eaten) print(still_hungry)
pizza 4 True
Coffee Shop Inventory
You're a barista taking inventory of coffee mugs. You have 124 small mugs and 75 large mugs. How many mugs do you have in total?
Write a program you can reuse every time you take inventory. By using variables, you can update the count of each mug and the total updates too! Your program should:
- Create a variable to store the number of small mugs
- Create a variable to store the number of large mugs
- Create a variable to store the total number of mugs (hint: you can use a math statement with the variables above!)
- Print the total
small_mugs = 124 large_mugs = 75 total_mugs = small_mugs + large_mugs print(total_mugs)
199
A variables example
Variables let you store and reuse values. This short Python program uses several different types of variables.
Quick quiz
1. Which of these is a valid Python variable name?
2. What type of variable is price = 4.99?
3. What does this program print?
points = 10 points = 25 print(points)
Keep learning Python
You know how to store data. Next, learn how to get data from the user, and how to print it in interesting ways. Or go deeper with a free, full Python course on CodeHS.