40
8
1 Computing with formulas
the variable represents a mathematical symbol, we use the symbol or a
good approximation to it as variable name. For example,y in mathematics
becomesy in the program, andv
0
in mathematics becomesv_0 in the
program. A close resemblance between mathematical symbols in the
description of the problem and variables names is important for easy
reading of the code and for detecting errors. This principle is illustrated
by the code snippet above: even if the long variable names explain well
what they represent, checking the correctness of the formula for y is
harder than in the program that employs the variablesv0,g,t, andy0.
For all variables where there is no associated precise mathematical
description and symbol, one must use descriptive variable names which
explain the purpose of the variable. For example, if a problem description
introduces the symbolD for a force due to air resistance, one applies a
variableD also in the program. However, if the problem description does
not define any symbol for this force, one must apply a descriptive name,
such as air_resistance, resistance_force, or drag_force.
How to choose variable names
• Usethesamevariablenamesintheprogramasinthemathe-
matical description of the problem you want to solve.
• Forallvariableswithoutaprecisemathematicaldefinitionand
symbol, use a carefully chosen descriptive name.
1.1.9 Reserved words in Python
Certain words are reserved in Python because they are used to build up
the Python language. These reserved words cannot be used as variable
names:and,as,assert,break,class,continue,def,del,elif,else,
except, False, finally, for, from, global, if, import, in, is, lambda,
None, nonlocal, not, or, pass, raise, return, True, try, with, while,
andyield. If you wish to use a reserved word as a variable name, it
is common to an underscore at the end. For example, if you need a
mathematical quantityλ in the program, you may work withlambda_
as variable name. See Exercise 1.16 for examples on legal and illegal
variable names.
Program files can have a freely chosen name, but stay away from names
that coincide with keywords or module names in Python. For instance,
do not use math.py,time.py, , random.py, os.py, sys.py,while.py,
for.py, if.py, class.py, or def.py.