7. Variables (labo)
7. Variables (labo)
In computer programming, variables are used to store, process, and manipulate data. So, let's
say you wanted to store 28 in the variable age, this is what you would do:
Entrée [ ]:
age = 28
So if you used print() with age as an argument, you would get the value that you assigned to
that variable.
Entrée [ ]:
print(age)
With age = 28 you are telling Python to create a variable named age and use it to store the
value 28. However, you can change the value assigned to any variable you previously created.
For instance, you can assign a value of 5 to age using the following code:
Entrée [ ]:
age = 5
So if you display age, you will get 5 instead of 28.
Entrée [ ]:
print(age)
Variables store numbers, strings and other type of data
Variables can be used to store floating point numbers, integers, strings, and other types of
data. For instance, you can create a variable name and assign it the string "Otto", or a variable
gnome_height and assign it the floating point number 12.7.
Entrée [ ]:
name = "Otto"
gnome_height = 12.7
You can use variables within f-strings to make your output more readable. For example, you
can print the value assigned to age along with the string "Age: " so that anyone reading the
display will understand what the value represents.
Entrée [ ]:
print(f"Age: {age}")
As another example, let's use f-strings to display the values assigned to name and
gnome_height.
Entrée [ ]:
print(f"Name: {name}")
print(f"Gnome height: {gnome_height}")
Important: Variables are case-sensitive, so Gnome_height and gnome_height are not the
same. To see this, you can run the next cell where you will get an error message.
Entrée [ ]:
Entrée [ ]:
Entrée [ ]:
score = score + 50 # now score is 0 + 50 which is 50
print(score)
In a similar way, when the score increases by 100 points, the variable score can be updated by
adding 100 to the previous value stored in score.
Entrée [ ]:
Entrée [ ]:
You can print the final score using an f-string as shown in the cell below:
Entrée [ ]:
Entrée [ ]:
my score = 450
Now, ask the chatbot why that code didn't work. You can use the prompt suggested here.
🤖 Use the Chatbot: Why doesn't this code work? my score = 450
Entrée [ ]:
print(49 / 7)
Using f-strings so that it is clear what you are displaying, you would use code similar to the
one in the cell below.
Entrée [ ]:
Entrée [ ]:
dog_age = 49 / 7
You can see how this would be an advantage if you consider the following scenario. Let's say
you want to display an f-string with Otto's dog age multiple times. Without using variables,
you would need to compute his dog age as many times as you refer to it.
Entrée [ ]:
Entrée [ ]:
print(f"""Otto's dog age is {dog_age}. So a dog that's about
{dog_age} would be the same age as Otto. Any dog born about {dog_age}
years ago would be in the same stage of life as Otto.""")
You will only need to update dog_age with Otto's new equivalent dog age:
Entrée [ ]:
dog_age = 50/7
And use the same f-string that you used before without editing. By defining a variable once,
you can use it in multiple places, which makes computer programs much more efficient.
Entrée [ ]:
Entrée [ ]:
Entrée [ ]:
Entrée [ ]:
# Create a variable called 'my_name' and assign it the value of your name as a string.
# Then print out a greeting using the variable, like "Hello, Andrew!"
my_name =
print()
Entrée [ ]:
# Enter your favorite number below and store it in a variable called 'fav_num'.
# Print out a message telling you what your favorite number plus 10 is.
fav_num =
print(f"Your favorite number plus 10 is {}")
Entrée [ ]:
# Create two variables, 'countries_visited' and 'countries_to_visit' and assign them the number
of
# countries you've been to and the number of countries you hope to visit. Then complete the
print statement.