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.
- input
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.
- output
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.
- reuse
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.
-
Q-3: Match each pattern with what it means for a program.
- Input
- Getting data from outside the computer.
- Output
- Displaying or producing the results of the program.
- Sequential Execution
- Performing statements one after another in the order they are encountered.
- Conditional Execution
- Checking for certain conditions, then executing or skipping a sequence of statements.
- Repeated Execution
- Performing some set of statements multiple times, typically with some variation.
- Reuse
- writing a set of instructions once, giving them a name, and calling those instructions as needed in the program.