Page - 41 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 41 -
Text of the Page - 41 -
2.2 Variables,ObjectsandExpressions 41
Generally, if you press the TAB key “too early”, IPython might have to give you a
list ofoptions, fromwhichyoumaychoose the intendedword.
With longer words, or if you are a bit uncertain about the spelling, TAB
completion is a handytool.
2.2 Variables,ObjectsandExpressions
2.2.1 ChooseDescriptiveVariableNames
Namesof variables should be chosenso that they are descriptive. If youare coding
some formula that in mathematical writing containsx and y, x and y ought to be
the corresponding names for those variables in your code (unless there are special
considerationsforyourcase).
Sometimes it isdifficult,orevenimpossible, tohaveavariablenamein thecode
that is identical to the corresponding mathematical symbol. Some inventiveness
is then called for, as, for example, when we used the variable name v0 for the
mathematical symbol v0 in our first program ball.py. Similarly, if you need a
variable for the counting of sheep, one appropriate name could beno_of_sheep,
i.e., join well-chosenwords by use of an underscore.2 Such naming makes it much
easier for a human to understand the written code, which in turn makes it easier to
finderrorsormodify thecode.Variablenamesmayalsocontainanydigit from0to
9,orunderscores,butcannot start withadigit.Lettersmaybe loweroruppercase,
which to Python isdifferent.
2.2.2 ReservedWords
Note that certainnames inPythonare reserved, meaning that youcannot use these
as names for variables. Interactively, we may get a complete list of the reserved
words:
In [1]: import keyword
In [2]: keyword.kwlist
Out[2]:
[’False’, ’None’, ’True’, ’and’, ’as’, ’assert’, ’break’, ’class’,
’continue’, ’def’, ’del’, ’elif’, ’else’, ’except’, ’finally’, ’for’,
’from’, ’global’, ’if’, ’import’, ’in’, ’is’, ’lambda’, ’nonlocal’,
’not’, ’or’, ’pass’, ’raise’, ’return’, ’try’, ’while’, ’with’, ’yield’]
As you can see, we have met some of these already, e.g.,fromandimport. If you
accidentallyuse a reservedwordasavariablenameyougetan errormessage.
2 Another common way of joining words into variable names, is to start each new word with
a capital letter, e.g., like in noOfSheep. In this book, however, we stick to the convention with
underscores.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik