This chapter is taken from the book A Primer on Scientific Programming with Python by H. P. Langtangen, 5th edition, Springer, 2016.
It is now time to pick up some important words that programmers use when they talk about programming: algorithm, application, assignment, blanks (whitespace), bug, code, code segment, code snippet, debug, debugging, execute, executable, implement, implementation, input, library, operating system, output, statement, syntax, user, verify, and verification. These words are frequently used in English in lots of contexts, yet they have a precise meaning in computer science.
Program and code are interchangeable terms. A code/program
segment is a collection of consecutive statements from a program.
Another term with similar meaning is code snippet. Many also use
the word application in the same meaning as program and code. A
related term is source code, which is the same as the text that
constitutes the program. You find the source code of a program in one
or more text files. (Note that text files normally have the extension
.txt
, while program files have an extension related to the
programming language, e.g., .py
for Python programs. The content of
a .py
file is, nevertheless, plain text as in a .txt
file.)
We talk about running a program, or equivalently executing a
program or executing a file. The file we execute is the file in
which the program text is stored. This file is often called an
executable or an application. The program text may appear in many
files, but the executable is just the single file that starts the
whole program when we run that file. Running a file can be done in
several ways, for instance, by double-clicking the file icon, by
writing the filename in a terminal window, or by giving the filename
to some program. This latter technique is what we have used so far in
this document: we feed the filename to the program python
. That is,
we execute a Python program by executing another program python
,
which interprets the text in our Python program file.
The term library is widely used for a collection of generally useful
program pieces that can be applied in many different contexts. Having
access to good libraries means that you do not need to program code
snippets that others have already programmed (most probable in a
better way!). There are huge numbers of Python libraries. In Python
terminology, the libraries are composed of modules and packages.
The section Evaluating standard mathematical functions gives a first glimpse of the math
module, which contains a set of standard mathematical functions for
\( \sin x \), \( \cos x \), \( \ln x \), \( e^x \), \( \sinh x \), \( \sin^{-1} x \),
etc. Later, you will meet many other useful modules. Packages are just
collections of modules. The standard Python distribution comes with a
large number of modules and packages, but you can download many more
from the Internet, see in particular www.python.org/pypi
. Very
often, when you encounter a programming task that is likely to occur
in many other contexts, you can find a Python module where the job is
already done. To mention just one example, say you need to compute how
many days there are between two dates. This is a non-trivial task that
lots of other programmers must have faced, so it is not a big surprise
that Python comes with a module datetime
to do calculations with
dates.
The recipe for what the computer is supposed to do in a program is called algorithm. In the examples in the first couple of chapters in this document, the algorithms are so simple that we can hardly distinguish them from the program text itself, but later in the document we will carefully set up an algorithm before attempting to implement it in a program. This is useful when the algorithm is much more compact than the resulting program code. The algorithm in the current example consists of three steps:
The implementation of an algorithm is the process of writing and testing a program. The testing phase is also known as verification: After the program text is written we need to verify that the program works correctly. This is a very important step that will receive substantial attention in the present document. Mathematical software produce numbers, and it is normally quite a challenging task to verify that the numbers are correct.
An error in a program is known as a bug, and the process of locating and removing bugs is called debugging. Many look at debugging as the most difficult and challenging part of computer programming. The origin of the strange terms bug and debugging can be found in Wikipedia.
Programs are built of statements. There are many types of statements:
v0 = 3
is an assignment statement, while
print y
is a print statement. It is common to have one statement on each line, but it is possible to write multiple statements on one line if the statements are separated by semi-colon. Here is an example:
v0 = 3; g = 9.81; t = 0.6
y = v0*t - 0.5*g*t**2
print y
Although most newcomers to computer programming will think they
understand the meaning of the lines in the above program, it is
important to be aware of some major differences between notation in a
computer program and notation in mathematics. When you see the
equality sign =
in mathematics, it has a certain interpretation as
an equation (\( x+2=5 \)) or a definition (\( f(x)=x^2+1 \)). In a computer
program, however, the equality sign has a quite different meaning, and
it is called an assignment. The right-hand side of an assignment
contains an expression, which is a combination of values, variables,
and operators. When the expression is evaluated, it results in a
value that the variable on the left-hand side will refer to. We often
say that the right-hand side value is assigned to the variable on
the left-hand side. In the current context it means that we in the
first line assign the number 3 to the variable v0
, 9.81 to g
, and
0.6 to t
. In the next line, the right-hand side expression v0*t -
0.5*g*t**2
is first evaluated, and the result is then assigned to the
y
variable.
Consider the assignment statement
y = y + 3
This statement is mathematically false, but in a program it just means
that we evaluate the right-hand side expression and assign its value
to the variable y
. That is, we first take the current value of y
and add 3. The value of this operation is assigned to y
. The old
value of y
is then lost.
You may think of the =
as an arrow, y <- y+3
, rather than an
equality sign, to illustrate that the value to the right of the arrow
is stored in the variable to the left of the arrow. In fact, the R
programming language for statistical computing actually applies an
arrow, many old languages (like Algol, Simula, and Pascal) used :=
to explicitly state that we are not dealing with a mathematical
equality.
An example will illustrate the principle of assignment to a variable:
y = 3
print y
y = y + 4
print y
y = y*y
print y
Running this program results in three numbers: 3
, 7
, 49
.
Go through the program and convince yourself that you understand what
the result of each statement becomes.
A computer program must have correct syntax, meaning that the text
in the program must follow the strict rules of the computer language
for constructing statements. For example, the syntax of the print
statement is the word print
, followed by one or more spaces,
followed by an expression of what we want to print (a Python variable,
text enclosed in quotes, a number, for instance). Computers are very
picky about syntax! For instance, a human having read all the previous
pages may easily understand what this program does,
myvar = 5.2
prinnt Myvar
but the computer will find two errors in the last line:
prinnt
is an unknown instruction and Myvar
is an undefined
variable. Only the first error is reported (a syntax error), because
Python stops the program once an error is found.
All errors that Python finds are easy to remove. The difficulty with
programming is to remove the rest of the errors, such as errors in
formulas or the sequence of operations.
Blanks may or may not be important in Python programs. In
the document Loops and lists [4] you
will see that blanks are in some occasions essential for a correct
program. Around =
or arithmetic operators, however, blanks do not
matter. We could hence write our program from the section Using variables as
v0=3;g=9.81;t=0.6;y=v0*t-0.5*g*t**2;print y
This is not a good idea because blanks are essential for easy reading
of a program code, and easy reading is essential for finding errors,
and finding errors is the difficult part of programming. The
recommended layout in Python programs specifies one blank around =
,
+
, and -
, and no blanks around *
, /
, and **
. Note that the
blank after print
is essential: print
is a command in Python and
printy
is not recognized as any valid command. (Python will
complain that printy
is an undefined variable.) Computer scientists
often use the term whitespace when referring to a blank. (To be more
precise, blank is the character produced by the space bar on the
keyboard, while whitespace denotes any character(s) that, if printed,
do not print ink on the paper: a blank, a tabulator character
(produced by backslash followed by t), or a newline character
(produced by backslash followed by n). (The newline character is
explained in the section Formatting text and numbers.)
When we interact with computer programs, we usually provide some information to the program and get some information out. It is common to use the term input data, or just input, for the information that must be known on beforehand. The result from a program is similarly referred to as output data, or just output. In our example, \( v_0 \), \( g \), and \( t \) constitute input, while \( y \) is output. All input data must be assigned values in the program before the output can be computed. Input data can be explicitly initialized in the program, as we do in the present example, or the data can be provided by user through keyboard typing while the program is running. Output data can be printed in the terminal window, as in the current example, displayed as graphics on the screen, or stored in a file for later access.
The word user usually has a special meaning in computer science: It means a human interacting with a program. You are a user of a text editor for writing Python programs, and you are a user of your own programs. When you write programs, it is difficult to imagine how other users will interact with the program. Maybe they provide wrong input or misinterpret the output. Making user-friendly programs is very challenging and depends heavily on the target audience of users. The author had the average reader of the book in mind as a typical user when developing programs for this document.
A central part of a computer is the operating system. This is actually a collection of programs that manages the hardware and software resources on the computer. There are three dominating operating systems today on computers: Windows, Mac OS X, and Linux. In addition, we have Android and iOS for handheld devices. Several versions of Windows have appeared since the 1990s: Windows 95, 98, 2000, ME, XP, Vista, Windows 7, and Windows 8. Unix was invented already in 1970 and comes in many different versions. Nowadays, two open source implementations of Unix, Linux and Free BSD Unix, are most common. The latter forms the core of the Mac OS X operating system on Macintosh machines, while Linux exists in slightly different flavors: Red Hat, Debian, Ubuntu, and OpenSuse to mention the most important distributions. We will use the term Unix in this document as a synonym for all the operating systems that inherit from classical Unix, such as Solaris, Free BSD, Mac OS X, and any Linux variant. As a computer user and reader of this document, you should know exactly what operating system you have.
The user's interaction with the operation system is through a set of
programs. The most widely used of these enable viewing the contents of
folders or starting other programs. To interact with the operating
system, as a user, you can either issue commands in a terminal window
or use graphical programs. For example, for viewing the file contents
of a folder you can run the command ls
in a Unix terminal window or
dir
in a DOS (Windows) terminal window. The graphical alternatives
are many, some of the most common are Windows Explorer on Windows,
Nautilus and Konqueror on Unix, and Finder on Mac. To start a
program, it is common to double-click on a file icon or write the
program's name in a terminal window.