1.10. The building blocks of programs¶
In the next few chapters, we will learn more about the vocabulary, sentence structure, paragraph structure, and story structure of Python. We will learn about the powerful capabilities of Python and how to compose those capabilities together to create useful programs.
There are some low-level conceptual patterns that we use to construct programs. These constructs are not just for Python programs, they are part of every programming language from machine language up to the high-level languages.
Get data from the “outside world”. This might be reading data from a file, or even some kind of sensor like a microphone or GPS. In our initial programs, our input will come from the user typing data on the keyboard.
Display the results of the program on a screen or store them in a file or perhaps write them to a device like a speaker to play music or speak text.
- sequential execution
Perform statements one after another in the order they are encountered in the script.
- conditional execution
Check for certain conditions and then execute or skip a sequence of statements.
- repeated execution
Perform some set of statements repeatedly, usually with some variation.
Write a set of instructions once and give them a name and then reuse those instructions as needed throughout your program.
It sounds almost too simple to be true, and of course it is never so simple. It is like saying that walking is simply “putting one foot in front of the other”. The “art” of writing a program is composing and weaving these basic elements together many times over to produce something that is useful to its users.
The word counting program from the previous section directly uses all of these patterns except for one.