Learn the basics of variables in Python.
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:
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:
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:
num_lives
? num_lives
?When naming variables in Python, follow these rules:
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.
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:
num_lives = 3
cost = 40.25
name = "Baby Yoda"
true
or false
. Example: is_jedi = True;
Take a look at the example to see the different variable types in action.
True
. If you were to add quotes, what type of variable would it be?age
by assigning a different value?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.
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:
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:
num_coffees
and profit_per_cup
the same type of variable?profit
and num_coffees
used differently?profit
and run the program. What happens?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!