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.
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.
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.
It is just step-by-step instructions that the computer can understand and execute. Programs often implement algorithms, but note that algorithms are typically less precise than programs and do not have to be written in a programming language.
Programs often implement algorithms, but they are not the same thing. An algorithm is a step by step list of instructions, but those instructions are not necessarily precise enough for a computer to follow. A program must be written in a programming language that the computer knows how to interpret.