Chapter 1 · pp. 23–30
The Way of the Program
This chapter introduces the most basic idea in computing: what a program is. It also explains how programmers think about big tasks, by breaking them into small steps a computer can perform. Learning to program is also a good way to practice problem solving, which is a key skill for computer scientists.
Thinking like a computer scientist
Computer science borrows habits from three other fields (p. 23):
- Mathematics: Mathematicians write ideas in formal languages, which have strict rules. Computer scientists do the same to describe computations.
- Engineering: Engineers design things by putting components together into systems. They also compare tradeoffs, such as speed versus cost, to pick the best option.
- Natural science: Scientists watch how complex systems behave, make hypotheses (educated guesses), and test their predictions. Programmers do this when they study how a program behaves and try to explain surprising results.
The most important skill is problem solving. It has three parts: formulating the problem (stating clearly what you need), thinking creatively about solutions, and expressing a solution clearly and accurately (p. 23). Writing programs gives you constant practice at all three. A program is useful in itself, but it is also a tool for reaching bigger goals (p. 23).
What is a program?
A program is a sequence of instructions that tells a computer how to carry out a computation (p. 23). The word sequence matters: the steps are given in a specific order, and the computer follows them exactly.
A computation can take several forms (p. 23):
- Mathematical: for example, solving a system of equations or finding the roots of a polynomial.
- Symbolic: for example, searching for a word in a document and replacing it with another.
- Graphical: for example, processing an image or playing a video.
What the task is does not change the basic idea. In each case, the program lists the steps that produce the result. Suppose you want to replace every "colour" with "color" in an essay. A program for that would say, in order: look at the text, find each match, swap it for the new word, and save the result.
The basic building blocks of every program
Programming languages look different on the surface, but nearly all of them offer the same few kinds of instructions (pp. 23–24):
- Input: get data from the keyboard, a file, the network, or another device.
- Output: show data on the screen, save it to a file, send it over the network, and so on.
- Math: do basic operations such as addition and multiplication.
- Conditional execution: check whether a condition is true and run the matching code. For example, "if the password is wrong, show an error message."
- Repetition: do something over and over, usually with some variation. For example, "for each of the 30 test scores, add it to a running total."
Even the most complicated program you have ever used is built from instructions like these (p. 24). This leads to a useful way to see programming: it is the process of breaking a large, complex task into smaller and smaller subtasks until each one is simple enough to be done with one of the basic instructions (p. 24).
Example: Imagine a program that finds the average of a class's test scores.
- Input: read the scores from a file.
- Repetition: go through the scores one at a time, adding each to a total.
- Math: divide the total by the number of scores.
- Conditional execution: if there are no scores, report that instead of dividing by zero.
- Output: display the average.
A big job ("find the class average") has become five small steps, each matching a basic instruction.
Running Python
Before writing code, you need a way to run it. Installing Python and related software can be hard for beginners, especially if they are also learning about their operating system. An easier start is to run Python in a web browser, using one of the websites that offer this (p. 24).
There are two versions of the language, Python 2 and Python 3. They are very similar, and a beginner meets only a few differences, so learning one makes it easy to switch to the other (p. 24). Python 3 is the version used here.
The Python interpreter is a program that reads and runs Python code. You can start it by clicking an icon or by typing python on a command line (p. 24). When it starts, it prints a few lines about itself, including a version number. If the number begins with 3, you are running Python 3. If it begins with 2, you are running Python 2 (p. 24).
The last line shown is the prompt, >>>. It means the interpreter is ready for you to type code. After you type a line and press Enter, the interpreter shows the result (p. 24):
>>> 1 + 1
2
Here the input is the math instruction 1 + 1, and the output is 2.
Check yourself
- In your own words, what is a program? Why does the order of the instructions matter?
- Give one example each of a mathematical, a symbolic, and a graphical computation.
- Name the five basic kinds of instructions found in almost every programming language, and describe what each one does.
- How does the idea of "breaking a large task into subtasks" describe what programming is?
- You start the Python interpreter and see a version number beginning with 2. Which version are you running, and what does the
>>> prompt tell you?