Free lesson preview · Python Basics

How Computer Programs Work

Explore this complete sample lesson before enrolling.

Learning objectives

By the end of this topic, you will be able to:

  • explain what a computer program is in precise, beginner-friendly language;
  • identify the input, processing, and output of a small program;
  • distinguish source code, the Python interpreter, and program output;
  • trace Python statements in execution order;
  • predict the output of a short program before running it;
  • describe a programming mistake using evidence instead of guessing.

Theory: from intention to instructions

A computer can repeat calculations, transform information, and follow rules very quickly. It cannot decide what you meant. A program therefore needs to express a task as a sequence of instructions that the computer can execute.

Programming is the work of turning an intention into explicit steps. For example, "prepare a welcome message" is an intention. A program needs a more precise description:

  1. obtain a person's name;
  2. build a message containing that name;
  3. display the message.

This way of thinking is more important than memorising Python syntax. Syntax gives you a vocabulary. Decomposition - breaking a goal into small steps - gives you a method for solving problems.

Mental model: a program is a recipe for data

Think of a program as a recipe that transforms data.

input  ->  processing  ->  output
  • Input is information available to the program. It may come from a user, a file, a sensor, a database, or values written directly in the code.
  • Processing is the work performed on that information: calculating, comparing, selecting, sorting, or formatting.
  • Output is the observable result: text on a screen, a saved file, a chart, a message sent to another service, or a changed value.

Consider a future temperature-conversion program:

input:       20 degrees Celsius
processing:  multiply by 9/5, then add 32
output:      68 degrees Fahrenheit

Not every program asks a user for input. In your earliest examples, the input will often be written directly in the source code. The input-process-output model still applies.

Source code, interpreter, and output

Three different things participate when you run Python code.

1. Source code

Source code is the human-readable text written by a programmer. Python source files normally use the .py extension.

print("Welcome to Python!")

2. Python interpreter

The Python interpreter is the program that reads Python instructions and executes them. It checks whether the code follows Python's grammar, evaluates expressions, and requests operations from the computer.

3. Program output

Output is what the running program produces. The previous source code produces:

Welcome to Python!

Do not copy prompts such as >>> into a Python file. A prompt belongs to an interactive Python session; it is not part of the instruction.

>>> print("Hello")   # interactive prompt plus source code
Hello                # output

In a .py file, write only:

print("Hello")

Your first instruction

The print() function asks Python to display a value.

print("Python follows explicit instructions.")

Break the instruction into parts:

  • print is the name of a built-in Python function;
  • ( and ) surround the value passed to the function;
  • the quotation marks delimit text;
  • the text inside the quotation marks is the value to display.

Python is case-sensitive. print, Print, and PRINT are different names. Only print refers to the built-in function used here.

Execution order

Python normally executes statements from top to bottom, one statement at a time.

print("Step 1: receive the order")
print("Step 2: prepare the order")
print("Step 3: deliver the order")

The output follows the same sequence:

Step 1: receive the order
Step 2: prepare the order
Step 3: deliver the order

This default is called sequential execution. Later chapters introduce conditions, which can choose a path, and loops, which can repeat a path. Those features change the flow, but the interpreter still executes one current instruction at a time.

Statements and expressions

Two words appear frequently in programming documentation:

  • An expression produces a value. For example, 4 + 3 produces 7.
  • A statement performs an action. For example, print(4 + 3) displays the value produced by the expression.
print(4 + 3)
print(10 - 2)

Output:

7
8

Python evaluates the expression inside the parentheses before print() displays the result.

Tracing a program by hand

Tracing means following a program one instruction at a time and recording what happens. It is one of the most useful habits a beginner can develop.

Trace this program without running it:

print("Order summary")
print(2 + 3)
print("items")
Step Instruction Observable result
1 print("Order summary") Displays Order summary
2 print(2 + 3) Calculates 5, then displays it
3 print("items") Displays items

Predicted output:

Order summary
5
items

Prediction makes practice active. Always predict first, run second, and explain any difference.

Programs are literal

Computers are consistent rather than intuitive. Python does not silently repair an instruction because its intention seems obvious.

print("System ready")

If the closing quotation mark is missing, Python cannot determine where the text ends:

print("System ready)

This produces a syntax error. The error is evidence about the instruction, not a judgement about the learner. A productive response is:

  1. read the final line of the error message;
  2. locate the reported line;
  3. compare punctuation and spelling with the intended syntax;
  4. make one small correction;
  5. run the program again.

Detailed traceback reading appears later in this chapter. For now, learn to treat every result - including an error - as feedback from the system.

Worked example: a delivery status program

Problem

Display a short sequence showing the lifecycle of a delivery.

Decomposition

input:       status messages written in the source code
processing:  execute each print instruction in order
output:      three lines describing the delivery

Implementation

print("Delivery tracker")
print("Package received")
print("Package ready for collection")

Expected output

Delivery tracker
Package received
Package ready for collection

Small change, predictable effect

Change only the final message:

print("Delivery tracker")
print("Package received")
print("Package collected")

Only the third output line changes. Making one change at a time helps you connect cause and effect.

Guided practice: event announcement

Create a three-line program that displays:

Python Study Session
Room 204
Starts at 18:30

Use one print() instruction per line.

Then complete these experiments:

  1. Predict the effect of swapping the second and third statements.
  2. Run the modified program and compare the result with your prediction.
  3. Change print to Print on one line and observe the error.
  4. Restore the lowercase name and confirm that the program runs again.

Common mistakes

Copying the interactive prompt

Incorrect in a .py file:

>>> print("Hello")

Correct:

print("Hello")

Using typographic quotation marks

Some word processors produce curved quotation marks. Python code should use straight quotation marks.

print("Valid Python text")

Changing the capitalisation

Print("Hello")

Python treats Print as a different name from print.

Reading code without predicting it

Running code immediately can hide gaps in understanding. Write down the expected output first, even for a three-line program.

Changing several things at once

When the result changes, you will not know which edit caused it. Prefer one controlled change followed by one execution.

Independent exercise: explain a tiny program

Study the following program without running it:

print("Daily report")
print(6 + 4)
print("tasks completed")

Complete all tasks:

  1. Write the exact predicted output.
  2. Identify the input, processing, and output.
  3. Explain why 6 + 4 is displayed as 10 rather than as the characters 6 + 4.
  4. Change the program so that it displays 12 tasks completed.
  5. Add a first line containing your name followed by the words status report.

Challenge: design before coding

Design a four-line program that presents a travel plan. Before writing Python, specify:

  • the intended output;
  • the ordered instructions;
  • which lines contain text;
  • which line, if any, contains a calculation.

Implement the program only after completing the design. Then compare the actual output with the intended output.

Knowledge check

  1. What is a computer program?
  2. What are the three stages in the input-process-output model?
  3. What is the difference between source code and program output?
  4. What role does the Python interpreter perform?
  5. In which order are ordinary Python statements executed?
  6. What is the difference between an expression and a statement?
  7. Why should you predict a program's output before running it?
  8. What should you do first when Python reports an error?

Key takeaways

  • A program is an explicit sequence of instructions for processing data.
  • Input is transformed by processing to produce output.
  • Source code is written by a programmer; the Python interpreter executes it.
  • Python normally runs statements sequentially from top to bottom.
  • Expressions produce values, while statements perform actions.
  • Tracing and prediction build a reliable mental model of execution.
  • Errors are observable feedback and an ordinary part of programming.