What is a program¶
~10 min ยท prerequisite: none
What you will be able to do¶
- Explain what a program is in plain words.
- Predict the result of a short sequence of steps by reading it from top to bottom.
- Recognize why a step that uses a name before it exists stops the program.
The idea¶
A program is a list of instructions that a computer follows. Each instruction is a small, exact step: store a number, add two numbers, show a message, or check whether something is true. The computer does not guess what you meant. It does exactly what the instructions say, in the order they are written.
Think of a recipe card. The card says "add two eggs, then stir, then bake for twenty minutes." If you read the steps in a different order, the cake may not work. A program behaves the same way. Order matters, and every step has to be written clearly enough that nothing is left to guess.
Programs are written in a programming language. A programming language is a set of rules for writing instructions that both you and the computer can read. This course uses Python, a language with a readable, plain style.
The text you write is called source code. It is stored in a file, usually ending in .py. The computer cannot run that text directly. A program called the interpreter reads the source code, line by line, and carries out each instruction. When you run a Python file, the interpreter starts at the first line and moves down.
Beginners often expect a program to fix small mistakes on its own. It does not. If you use a name that you never created, the interpreter stops and reports the problem. You will see examples of these messages in the last lesson of this part. For now, the key idea is simple: a program is a sequence of exact steps, and the computer follows them in order.
Each new word in this lesson is a term you can look up later in the Python glossary. Keep the three ideas in mind for the rest of the course: instructions run in order, names must exist before you use them, and the interpreter does exactly what the source code says.
Worked example¶
Here is a short program with three instructions.
Line by line, this is what Python does:
price = 4creates a variable namedpriceand stores the number 4 in it. A variable is a name that points to a value.quantity = 3creates a second variable,quantity, and stores 3 in it.total = price * quantityreads the current values ofpriceandquantity, multiplies them, and stores the result, 12, in a new variable calledtotal.
Nothing is printed here. The program only changes what the variables hold. You will learn how to show results in lesson 3.
Now look at the same instructions in a different order.
When the interpreter reaches the first line, price does not exist yet. Python stops with a NameError. The steps are the same, but the order is wrong.
Try it¶
Run this block. Every assert line checks a fact. If a fact is false, Python stops with an error. If all checks pass, nothing appears.
Now a block that shows order matters. The first two lines are the same as before, but the final value changes.
Common mistakes¶
-
Using a name before it exists. Writing
c = b + 1beforeb = 2gives: -
Forgetting a closing quote. Writing
print("Hello)gives: -
Forgetting a closing bracket. Writing
print("a"gives: -
Expecting the program to guess. If you write
total = price + quantitywhen you meant to multiply, Python gives no error. The program runs, and the result is not what you wanted. Read your instructions and check the result. -
Typing a name with a typo. Writing
totl = 5and thenprint(total)givesNameError: name 'total' is not defined.
Practice¶
- Write three lines that store 4 in
x, storex * 2iny, and then check withassert y == 8. Run it. - Write three lines that start with
score = 0, add 10 toscore, then subtract 3. Add anassertthat checks the final value is 7. -
Without running the code, predict whether
assert z == 6passes or fails:Then run it to check. 4. The following lines fail. Reorder them so the program runs without an error.
-
Stretch: write a program that stores a price of 5 and a quantity of 4 in variables, computes the total cost, then computes a discount of 2 from that total and stores the final amount. Add an
assertthat checks the final amount is 18.
Solution
-
python x = 4 y = x * 2 assert y == 8 -
python score = 0 score = score + 10 score = score - 3 assert score == 7 -
The assertion passes. The steps give
z = 2, thenz = 4, thenz = 8. Soassert z == 6fails. Check your prediction by running the block. The correct check isassert z == 8. -
python count = 5 result = count + 1 -
python price = 5 quantity = 4 total = price * quantity discount = 2 final_amount = total - discount assert final_amount == 18
Recap¶
- A program is an ordered list of exact instructions that the interpreter runs from top to bottom.
- A name must be created before you use it, or Python stops with a
NameError. - The computer does exactly what the source code says, so careful reading of your own code is part of programming.