Python is not a natural language
Download original file: 1_reading_python_code.ipynb
View original file in nbviewer: 1_reading_python_code.ipynb
Python is an unnatural language.
Even if many words used in Python resemble English words, they are interpreted in a very different way. It is best if one does not associate the English meaning with the Python meaning and instead relearns each word in turn.
Think in tokens, not words with punctuation.
The words used in programs are not words, they are tokens that are interpreted
as one unit of thought with a single meaning depending on the context. All other
symbols (i.e. =
, +
)
and sometimes combinations of symbols (i.e. ==
, +=
) are also tokens. All
tokens are interpreted
with the same rules.
What you write is not a text, it is code.
Instead of writing text, computer people write code because of its natural tendency to be incomprehensible. All effort should be made to make the code you write easy for humans to read (interpret).
Think in statements not sentences.
Programs are made up of a series of statements made up of tokens (i.e. e =
2.718281828
) which can roughly
be thought of as sentences. However, unlike natural language, one
does not read each statement in consecutive order to reach the end of your
code.
Instead, one must read Python statements in the order defined by the rules of
the language. This may be confusing to the novice because statements written in
the code are sometimes skipped and others are read many many many times even
though
they appear only once; Reading code is almost never top to bottom and usually
involves jumping around, however,
all effort should be made to make the code you write easy for humans to interpret.
Code is never ambiguous.
Python code is interpreted by the python interpreter. Given the same input, your computer program should always do the same thing. One way to learn how to read code is to ask the interpreter questions.
Execute each cell in this IPython notebook by pressing Ctrl
, holding it
down, and then pressing Enter
(Ctrl-Enter
)
5 + 2
The statement 5 + 2
made up of the three tokens 5
, +
, 2
causes Python to
add these two numbers and return the result. We did not specify what to do with
this result and in this case it was spit out onto the screen.
a = 5 + 2
The =
token tells our program to store the output of adding these numbers into
a variable named a
.
7 = a
We cannot tell the program to store the contents of a
into a number. Variables
cannot start with numbers.
7 == a
The ==
token means test if the thing on the left is the same as the thing on
the right.
IPython provides a help
function. Try:
help(1)
You will learn things you never knew about integers, and a whole lot about
operators like +
.
A shorter version of help is available but it only works on variables (like b
below), not expressions like 1
or 41**(3 /2.0)
:
b = 1.0
b?
Exercise: Convert English into Python.
Write three Python statements in the next cell to calculate the area swept out
by a planet orbiting the sun. The first will store the maximum distance the
planet moves away from the sun during its orbit, the second will store the
minimum distance from the sun, and the third will multiply these together with
planet_area
.
Hint: The value of math
module as
seen below.
import math
math.pi
3.141592653589793
Variable names
Variable names in Python can contain alphanumerical characters a-z
, A-Z
,
0-9
and some special characters such as _
. Variable names cannot start with
a number.
By convension, variable names start with a lower-case letter, and Class names start with a capital letter.
In addition, there are a number of Python keywords that cannot be used as variable names. These keywords are:
and, as, assert, break, class, continue, def, del, elif, else, except,
exec, finally, for, from, global, if, import, in, is, lambda, not, or,
pass, print, raise, return, try, while, with, yield.
Common python functions and modules should also not be used as variable names. If it is commonly imported, DO NOT use it as a variable name:
string, os, sys, ord, chr, int, float, bool, long, numpy, .... (longer list)
Acceptable variable names
These are all legal variable names.
x = 7
x1 = 3.4
x23982342 = 56.8
Use descriptive variable names
Use variable names that describe what they contain.
number_of_cats = 7
area_of_cat_house = 3.4 # in square meters
cat_fights_per_day = 56.8