Please enable JavaScript to use CodeHS

Variables in Python

Learn the basics of variables in Python.

By Rachel Devaney

What is a variable?

In programming, we use variables to store information. By storing values in variables, we are able to reference and/or manipulate information throughout a program. Let’s say we create a game where players have a specific number of lives. We could use a variable to store the number of lives.

In Python, you create a variable by choosing a name for the container and assigning an initial, or starting, value. A variable that holds the number of lives a player has might look like this:

num_lives = 5
Plain text

Let’s say the player loses a life and now has 2 lives. We change the value of a variable by assigning a different value to it:

num_lives = 2
Plain text

When assigning a new value to an existing variable, it’s important that the variable name is exactly the same. For example, num_Lives = 2 would create an entirely new variable due to the capital L.

The program below puts all of this together. Explore the example by running it and answering these guiding questions:

  • What happens when you assign different values to the variable num_lives?
  • Let’s say the player loses another life. Can you add to the program to assign a new value to num_lives?

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

Additionally, while not necessarily a rule, you’ll want to name your variable in a way that describes what information it’s holding. This makes your program easier to read and understand.


Types of Variables

In the examples so far, we’ve only used numerical data. However, variables can store other types of information as well. The variable type is based on the type of data the variable holds. Let’s take a look at the different types of variables:

  • Integer (int): whole numbers, such as -5, 0, 15. Example: num_lives = 3
  • Float: number with a decimal, such as 43.67, -0.11, 45.2543. Example: cost = 40.25
  • String: text - note that strings are always surrounded by quotes. Example: name = "Baby Yoda"
  • Boolean: either true or false. Example: is_jedi = True;

Take a look at the example to see the different variable types in action.

  • Can you identify the type for each variable?
  • Notice how the Boolean variable type does not include quotes around the value True. If you were to add quotes, what type of variable would it be?
  • Can you change the variable type of age by assigning a different value?

Practice #1

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.

Practice #2

You’re a barista at a coffee shop, and you’re taking inventory of the number of coffee cups you have. You have 124 small mugs and 75 large mugs. How many mugs do you have in total?

Write a program that you can use whenever you’re taking inventory. By using variables, you can easily reuse this program by updating the number of each mug! Your program should do the following:

  • 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 above variables!)
  • Then, print out the total number of mugs.

The Power of Variables

So far, we’ve seen how we can change the values of variables and how to print these values to the console. However, variables are much more powerful than that! Take a look at this final example program to see how we can use variables in slightly more complex programs.

This program determines the amount of profit earned based on the number of cups of coffee sold. It asks the user for three orders and updates the variable num_coffees based on each order. Then, it calculates the total profit based on the amount of profit per cup, which is initially set to $0.75.

Run the program and explore with these guiding questions:

  • Are num_coffees and profit_per_cup the same type of variable?
  • How are the variables profit and num_coffees used differently?
  • Change the value of the variable profit and run the program. What happens?
  • Could you write this program without variables?

The program above uses a variable to store the amount of profit per cup of coffee. Using a variable to store this value enables us to easily change the value if we wanted to see the effect of using a different price point. Instead of having to go into our code and update the value in potentially multiple places, we just have the change the value of the variable once. This is extremely helpful when your program gets to be hundreds and hundreds of lines of code!

Additionally, using a variable to store the number of cups of coffee sold enables us to update the variable as more orders come in. This makes our program flexible and able to respond to new inputs.

In conclusion, the power of variables comes in their ability to store and update information throughout our program - making it easy to update our programs as we work. Go forth and use the power of variables in your programs!