6
1 T h e F i r s t F e w S t e p s
t = 0.6
# Time
y = v0*t - 0.5*g*t**2
# Vertical position
print(y)
Let us now explain this program in full detail.
Typesetting of Code Computer programs, and parts of programs, are typeset with
a blue background in this book. When a complete program is shown, the blue
background has a slightly darker top and bottom bar (as for ball.py here). Without
the bars, the code is just a snippet and will normally need additional lines to run
properly.
We also use the blue background, without bars, for interactive sessions
(Sect. 2.1).
1.2.2 Dissecting the Program
A computer program like ball.py contains instructions to the computer written as
plain text. Humans can read the code and understand what the program is capable
of doing, but the program itself does not trigger any actions on a computer before
another program, the Python interpreter, reads the program text and translates this
text into specific actions.
You must learn to play the role of a computer
Although Python is responsible for reading and understanding your program,
it is of fundamental importance that you fully understand the program
yourself. You have to know the implication of every instruction in the program
and be able to figure out the consequences of the instructions. In other words,
you must be able to play the role of a computer.
One important reason for this strong demand is that errors unavoidably, and
quite often, will be committed in the program text, and to track down these
errors, you have to simulate what the computer does with the program. Also,
you will often need to understand code written by other people. If you are able
to understand their code properly, you may modify and use it as it suits you.
When you run your program in Python, it will interpret the text in your file line
by line, from the top, reading each line from left to right. The first line it reads is
# Program for computing the height of a ball in vertical motion.
This line is what we call a comment. That is, the line is not meant for Python to read
and execute, but rather for a human that reads the code and tries to understand what
is going on. Therefore, one rule in Python says that whenever Python encounters the
sign # it takes the rest of the line as a comment. Python then simply skips reading
the rest of the line and jumps to the next line. In the code, you see several such
comments and probably realize that they make it easier for you to understand (or
1 T h e F i r s t F e w S t e p s
t = 0.6
# Time
y = v0*t - 0.5*g*t**2
# Vertical position
print(y)
Let us now explain this program in full detail.
Typesetting of Code Computer programs, and parts of programs, are typeset with
a blue background in this book. When a complete program is shown, the blue
background has a slightly darker top and bottom bar (as for ball.py here). Without
the bars, the code is just a snippet and will normally need additional lines to run
properly.
We also use the blue background, without bars, for interactive sessions
(Sect. 2.1).
1.2.2 Dissecting the Program
A computer program like ball.py contains instructions to the computer written as
plain text. Humans can read the code and understand what the program is capable
of doing, but the program itself does not trigger any actions on a computer before
another program, the Python interpreter, reads the program text and translates this
text into specific actions.
You must learn to play the role of a computer
Although Python is responsible for reading and understanding your program,
it is of fundamental importance that you fully understand the program
yourself. You have to know the implication of every instruction in the program
and be able to figure out the consequences of the instructions. In other words,
you must be able to play the role of a computer.
One important reason for this strong demand is that errors unavoidably, and
quite often, will be committed in the program text, and to track down these
errors, you have to simulate what the computer does with the program. Also,
you will often need to understand code written by other people. If you are able
to understand their code properly, you may modify and use it as it suits you.
When you run your program in Python, it will interpret the text in your file line
by line, from the top, reading each line from left to right. The first line it reads is
# Program for computing the height of a ball in vertical motion.
This line is what we call a comment. That is, the line is not meant for Python to read
and execute, but rather for a human that reads the code and tries to understand what
is going on. Therefore, one rule in Python says that whenever Python encounters the
sign # it takes the rest of the line as a comment. Python then simply skips reading
the rest of the line and jumps to the next line. In the code, you see several such
comments and probably realize that they make it easier for you to understand (or
