2.2 Variables, Objects and Expressions
41
Generally, if you press the TAB key “too early”, IPython might have to give you a
list of options, from which you may choose the intended word.
With longer words, or if you are a bit uncertain about the spelling, TAB
completion is a handy tool.
2.2 Variables, Objects and Expressions
2.2.1 Choose Descriptive Variable Names
Names of variables should be chosen so that they are descriptive. If you are coding
some formula that in mathematical writing contains x and y, x and y ought to be
the corresponding names for those variables in your code (unless there are special
considerations for your case).
Some times it is difficult, or even impossible, to have a variable name in the code
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 v 0 in our first program ball.py. Similarly, if you need a
variable for the counting of sheep, one appropriate name could be no_of_sheep,
i.e., join well-chosen words 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
find errors or modify the code. Variable names may also contain any digit from 0 to
9, or underscores, but can not start with a digit. Letters may be lower or upper case,
which to Python is different.
2.2.2 Reserved Words
Note that certain names in Python are reserved, meaning that you can not 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., from and import. If you
accidentally use a reserved word as a variable name you get an error message.
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.
41
Generally, if you press the TAB key “too early”, IPython might have to give you a
list of options, from which you may choose the intended word.
With longer words, or if you are a bit uncertain about the spelling, TAB
completion is a handy tool.
2.2 Variables, Objects and Expressions
2.2.1 Choose Descriptive Variable Names
Names of variables should be chosen so that they are descriptive. If you are coding
some formula that in mathematical writing contains x and y, x and y ought to be
the corresponding names for those variables in your code (unless there are special
considerations for your case).
Some times it is difficult, or even impossible, to have a variable name in the code
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 v 0 in our first program ball.py. Similarly, if you need a
variable for the counting of sheep, one appropriate name could be no_of_sheep,
i.e., join well-chosen words 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
find errors or modify the code. Variable names may also contain any digit from 0 to
9, or underscores, but can not start with a digit. Letters may be lower or upper case,
which to Python is different.
2.2.2 Reserved Words
Note that certain names in Python are reserved, meaning that you can not 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., from and import. If you
accidentally use a reserved word as a variable name you get an error message.
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.
