Python · Variables and types
What's a variable ?
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — What's a Variable?
When you code, you usually need to store information that you want to reuse later. For example, the name of your Codémon or its level. To do that, you are going to use variables.
A variable is a space in your computer's memory that stores information. More precisely, 3 informations:
- A Name → how we refer to it
- A Type → what kind of data it holds
- A Value → the data it contains
Name or Address in Memory?
In programming, we often use the term "name" to refer to a variable. However, in reality, a variable is more like an address in memory where data is stored. The name is just a convenient way for us to refer to that address.
It's like using an address to find a house instead of remembering the house's latitude and longitude.
Exercise 2 — How does it work ?
With Python, to create a variable, you just need to write the name of the variable, an equal sign, and the value we want to store.
name = value
As you can see, we do not precise the type of the variable. Python is smart enough to understand it by itself BUT it doesn't mean you shouldn't know the type of your variable!
Once created, to use the variable, you just need to write its name!
Typing variables
In other programming languages, you would have to precise the type of the variable when creating it. No need in Python tho! This is one of its force (and weaknesses at the same time). It makes it easier to write code, but also easier to make mistakes.
Exercise 3 — Your first variable!
Let's go back to our Codémon and create a variable to store its name.
Here is your mission: Change the
???
value in the code editor to the name of the Codémon you want then run the code.
Reminder: The names are Aquachu, Florachu, and Pyrochu. Choose wisely!
codemon_name = "???"
print(codemon_name)
Exercise 4 — Creating a name variable
Create a variable called codemon_name and assign it the value "Pyrochu".
____ ____ ____
Options: codemon_name = "Pyrochu"
Hint : We use ```=``` to assign values to variables. String values must be enclosed in quotes (either single ' or double ").
Exercise 5 — Assignment and re-assignment
Variables are called variables because their value can change!
To change the value of a variable, we use the assignment operator
=
, it's basically the same syntax as creating a variable!
Can't get back the old value?
If you change the value of a variable, the new value will replace the old one. You won't be able to get the old one back so be careful!
codemon_name = "Aquachu"
print(codemon_name)
codemon_name = "Florachu"
print(codemon_name)
If you run the code, you will see that the first print displays "Aquachu" and the second one displays "Florachu". This means that the value of the variable has changed. And you can't get back "Aquachu".
Exercise 6 — Creating and changing values
Store the number 5 as the level of your codemon and then, change it to 6.
codemon_level = ____
____ ____ 6
print(codemon_level)
Options: 5 5 codemon_level Codémon_level = ==
Hint : We use ```=``` to assign and re-assign values to variables. Pay also attention to the name of the variable, it should be the same!
Exercise 7 — Re-assignment of a variable
What happens to the old value when you assign a new value to a variable?
Options
• It is replaced and lost
• It is kept in memory
• It becomes the "second" value of the variable
• It is kept in a list
Hint : Remember : Variables can only store one value at a time. If a new value is assigned to a variable, the old value is replaced and lost.
Exercise 8 — Small test!
Here is a small non-guided exercise to practice what you learned about variables.
- Create 1 variable to store the name of your Codémon.
- Print the name of your Codémon using
print()
.
- Change the name of your Codémon to something else and print it again.
Back to the course