Solutions

Strings and Variables

It is vital to understand the distinction between the variable, name used above and the string, "name" used here. If you are in any doubt, ask.

In [2]:
print("Hello","name")
Hello name

What happens if we change mass_in_kg?

You should notice that changing the value of mass_in_kg does not automatically "update" the value in mass_in_lbs:

In [7]:
mass_in_kg = 75.0
print(mass_in_lbs)
154.0

Again, this is because the = operation takes the current values of the variables on the right, and places the value of the computation into the variable on the left. As such, mass_in_lbs is just a snapshot - to update it, we would have to recompute it after changing the value mass_in_kg:

In [8]:
mass_in_lbs = 2.2 * mass_in_kgs
print(mass_in_kgs,"kg is equivalent to",mass_in_lbs,"lbs")
70.0 kg is equivalent to 154.0 lbs

What variable names should I use?

Variable names should be informative enough to describe the value that they contain, so that when you come back to your code later (or someone else has to use it) they can get a gist of what it's doing by looking at the variable names. Of course, there's nothing stopping you from naming your variables in any way that takes your fancy, but future you might regret this choice after a couple of months!

Using a new variable?

If we try to use a variable in a calculation, or in this case try to print the value of a variable that we haven't given a value to yet, we'll get a variable definition error:

In [9]:
print(my_new_variable)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-43286c8c1366> in <module>
----> 1 print(my_new_variable)
NameError: name 'my_new_variable' is not defined

This error is Python telling saying "you want me to use the value of the variable my_new_variable, but you haven't given a value to my_new_variable for me to use"!