Please enable JavaScript to use CodeHS

Python Tutorial

Variables in Python

Variables are how programs remember things. Learn how to create them, name them, and use the four core types every Python programmer knows by heart.

python ~15 min Beginner friendly
The Big Idea

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:

main.py
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:

main.py
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.

Interactive

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.

main.py
# 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)
Console
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:

Variable playground
num_lives
3
>>> print(num_lives)  →  3

Conventions

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

Data Types

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:

int

Integer

Whole numbers, such as -5, 0, or 15.

num_lives = 3
float

Float

Numbers with a decimal, such as 43.67 or -0.11.

cost = 40.25
str

String

Text. Strings are always surrounded by quotes.

name = "Baby Yoda"
bool

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:

main.py
name = "Baby Yoda"
age = 140.352
is_jedi = True

print(name)
print(age)
print(is_jedi)
Console
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.)


Your Turn

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.

1

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.

solution.py
favorite_food = "pizza"
slices_eaten = 4
still_hungry = True

print(favorite_food)
print(slices_eaten)
print(still_hungry)
Console
pizza
4
True
2

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
solution.py
small_mugs = 124
large_mugs = 75
total_mugs = small_mugs + large_mugs

print(total_mugs)
Console
199

Code

A variables example

Variables let you store and reuse values. This short Python program uses several different types of variables.


Check Yourself

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.