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 a 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 terms appear in just about every language.

input

Get data from the user via keyboard or mouse input, or from a file, or some other device.

output

Display data on the screen for an end user, in the console for the programmer, or send data to a file or other device.

math and logic

Perform basic mathematical operations like addition, multiplication and logical operations like and, or, and not (more on these later).

conditional execution

Check for certain conditions and execute the appropriate sequence of statements.

repetition

Perform some action repeatedly, usually with some variations.

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 for loop. This is a type of repetition instruction. Run the program below to see what the output to the console is:

We can also iterate without a definite stopping point with while loops (another type of repetition instruction). You might use this if you want to receive input from the user in your program but you don’t know how long it’ll take for them to be done entering new input. Run the program below and add some items to a grocery list:

Note that in the example above, you could run this program repeatedly and give it a different number of items each time you run it. Also note the cancel button does not do anything at this time. That is because we haven’t programmed it to do anything!

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

You have attempted of activities on this page