1.5. More About Programs¶
A program is a sequence of instructions that specifies how to perform a computation. The computation might be something as complex as rendering an html page in a web browser or encoding a video and streaming it across the network. It can also be a symbolic computation, such as searching for and replacing text in a document or (strangely enough) compiling a program.
The details look different in different languages, but a few basic instructions appear in just about every language.
Get data from the keyboard, a file, or some other device.
Display data on the screen or send data to a file or other device.
- math and logic
Perform basic mathematical operations like addition and multiplication and logical operations like
- conditional execution
Check for certain conditions and execute the appropriate sequence of statements.
Perform some action repeatedly, usually with some variation.
Believe it or not, that’s pretty much all there is to it. Every program you’ve ever used, no matter how complicated, is made up of instructions that look more or less like these. Thus, we can describe programming as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with sequences of these basic instructions.
1.5.1. Preview of Control Structures¶
We won’t get too much into python control structures yet, but it is good to mention them early to give you a taste for what you can do with the language! If these make sense to you now, that’s great! However, we don’t expect you to understand these yet - understanding will come later.
First we have structures that allow us to iterate over something.
We can look at strings character-by-character or lists item-by-item until we’ve reached the end of them by using something called a
We can also iterate without a definite stopping point with
You might use this if you want to receive input from the user of your program but you don’t know how long it’ll take for them to be done with your code.
Other structures will allow us to only run parts of our programs or only do some task if a certain set of conditions are found. Conditionals, as they’re called, allow us to do that. Check out how adding conditionals to our code can change what we can write about regarding grocery shopping.
Check your understanding