Skip to content

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.

price = 4
quantity = 3
total = price * quantity

Line by line, this is what Python does:

  1. price = 4 creates a variable named price and stores the number 4 in it. A variable is a name that points to a value.
  2. quantity = 3 creates a second variable, quantity, and stores 3 in it.
  3. total = price * quantity reads the current values of price and quantity, multiplies them, and stores the result, 12, in a new variable called total.

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.

total = price * quantity
price = 4
quantity = 3

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.

a = 1
b = a + 1
assert b == 2

Now a block that shows order matters. The first two lines are the same as before, but the final value changes.

x = 10
x = x + 5
x = x * 2
assert x == 30

Common mistakes

  1. Using a name before it exists. Writing c = b + 1 before b = 2 gives:

    NameError: name 'b' is not defined
    
  2. Forgetting a closing quote. Writing print("Hello) gives:

    SyntaxError: unterminated string literal (detected at line 1)
    
  3. Forgetting a closing bracket. Writing print("a" gives:

    SyntaxError: '(' was never closed
    
  4. Expecting the program to guess. If you write total = price + quantity when 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.

  5. Typing a name with a typo. Writing totl = 5 and then print(total) gives NameError: name 'total' is not defined.

Practice

  1. Write three lines that store 4 in x, store x * 2 in y, and then check with assert y == 8. Run it.
  2. Write three lines that start with score = 0, add 10 to score, then subtract 3. Add an assert that checks the final value is 7.
  3. Without running the code, predict whether assert z == 6 passes or fails:

    z = 2
    z = z + 2
    z = z * 2
    

    Then run it to check. 4. The following lines fail. Reorder them so the program runs without an error.

    result = count + 1
    count = 5
    
  4. 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 assert that checks the final amount is 18.

Solution
  1. python x = 4 y = x * 2 assert y == 8

  2. python score = 0 score = score + 10 score = score - 3 assert score == 7

  3. The assertion passes. The steps give z = 2, then z = 4, then z = 8. So assert z == 6 fails. Check your prediction by running the block. The correct check is assert z == 8.

  4. python count = 5 result = count + 1

  5. 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.

Sources