1.2 A Python Program with Variables
9
1.2.3 Why Use Variables?
But why do we introduce variables at all? Would it not be just as fine, or even
simpler, to just use the numbers directly in the formula?
If we did, using the same numerical values, ball.py would become even shorter,
reading
# Program for computing the height of a ball in vertical motion
y = 5*0.6 - 0.5*9.81*0.6**2
# vertical position
print(y)
What is wrong with this? After all, we do get the correct result when running the
code!
Coding and Mathematical Formulation If you compare this coded formula with
the corresponding mathematical formulation
y = v 0 t − 0.5gt
2 ,
the equivalence between code and mathematics is not as clear now as in our original
program ball.py, where the formula was coded as
y = v0*t - 0.5*g*t**2
In our little example here, this may not seem dramatic. Generally, however, you better believe that when, e.g., trying to find errors in code that lacks clear equivalence
to the corresponding mathematical formulation, human code interpretation typically
gets much harder and it might take you a while to track down those bugs!
Changing Numerical Values In addition, if we would like to redo the computation
for another point in time, say t = 0.9 s, we would have to change the code in two
places to arrive at the new code line
y = 5*0.9 - 0.5*9.81*0.9**2
You may think that this is not a problem, but imagine some other formula (and
program) where the same number enters in a 100 places! Are you certain that you
can do the right edit in all those places without any mistakes? 7 You should realize
that by using a variable, you get away with changing in one place only! So, to
change the point in time from 0.6 to 0.9 in our original program ball.py, we could
simply change t = 0.6 into t = 0.9. That would be it! Quick and much safer
than editing in many places.
7 Using the editor to replace 0.6 in all places might seem like a quick fix, but you would have to
be sure you did not change 0.6 in the wrong places. For example, another number in the code,
e.g. 0.666, could easily be turned into 0.966, unless you were careful.
9
1.2.3 Why Use Variables?
But why do we introduce variables at all? Would it not be just as fine, or even
simpler, to just use the numbers directly in the formula?
If we did, using the same numerical values, ball.py would become even shorter,
reading
# Program for computing the height of a ball in vertical motion
y = 5*0.6 - 0.5*9.81*0.6**2
# vertical position
print(y)
What is wrong with this? After all, we do get the correct result when running the
code!
Coding and Mathematical Formulation If you compare this coded formula with
the corresponding mathematical formulation
y = v 0 t − 0.5gt
2 ,
the equivalence between code and mathematics is not as clear now as in our original
program ball.py, where the formula was coded as
y = v0*t - 0.5*g*t**2
In our little example here, this may not seem dramatic. Generally, however, you better believe that when, e.g., trying to find errors in code that lacks clear equivalence
to the corresponding mathematical formulation, human code interpretation typically
gets much harder and it might take you a while to track down those bugs!
Changing Numerical Values In addition, if we would like to redo the computation
for another point in time, say t = 0.9 s, we would have to change the code in two
places to arrive at the new code line
y = 5*0.9 - 0.5*9.81*0.9**2
You may think that this is not a problem, but imagine some other formula (and
program) where the same number enters in a 100 places! Are you certain that you
can do the right edit in all those places without any mistakes? 7 You should realize
that by using a variable, you get away with changing in one place only! So, to
change the point in time from 0.6 to 0.9 in our original program ball.py, we could
simply change t = 0.6 into t = 0.9. That would be it! Quick and much safer
than editing in many places.
7 Using the editor to replace 0.6 in all places might seem like a quick fix, but you would have to
be sure you did not change 0.6 in the wrong places. For example, another number in the code,
e.g. 0.666, could easily be turned into 0.966, unless you were careful.
