1. Programs and Python

Lesson 1 of 54 · Python Foundations

A train timetable can tell a person when a train is expected. A computer can work with a timetable too, but first it needs instructions that describe what to do with the information: find a departure, compare times, or display a result.

A program is a set of instructions expressed in a form a computer can execute. The text a programmer writes is called source code, often shortened to code. A programming language supplies rules for expressing those instructions.

Where Python fits

Python is a general-purpose programming language. People use it for tasks such as preparing data, automating repetitive work, building parts of web applications, and developing machine-learning tools. Those uses share a language, even though the programs themselves do different jobs.

Python is a useful place to begin because a small example can express an operation without requiring us to learn every part of a larger application first. Learning Python also gives us a way to study ideas that appear in other languages, while remembering that each language has its own rules.

Here is a complete, small Python program:

print("Next departure: 09:30")

When the program runs, it displays:

Next departure: 09:30

We do not need to memorize the punctuation yet. For now, notice the difference between the instruction in the first block and the result in the second. The program contains print(...); the displayed result does not.

From code to a result

A Python implementation reads the source and carries out the program. One common implementation is CPython. You may hear the program that runs Python code called the Python interpreter.

Python source code  →  Python interpreter  →  Program behavior
                                              ↓
                                      text shown as output

The diagram describes this example, not every possible Python program: programs can also read data, save files, or communicate over a network.

The place where text appears depends on how a program is run. A terminal is an application for interacting with a computer using text. An editor or learning website may provide a separate output panel instead. A console is a text-based interface used to interact with a program or see its output; the exact labels vary between tools.

Running an example on your computer

You can read this course without installing anything. To run every example locally, install Python 3.10 or newer using the instructions for your operating system at python.org/downloads. On Windows, the installer may offer to add Python to your PATH; the Python launcher is another way to run it.

Open a terminal and check that Python is available: on Windows, try py --version; on macOS or Linux, try python3 --version. The version number is output, not a command to type. If your system cannot find the command, consult the installation instructions for your platform before continuing.

In a plain-text editor, save the example above in a file named departure.py. In the terminal, navigate to the folder containing the file. Run py departure.py on Windows or python3 departure.py on macOS or Linux. The message shown earlier should appear. These commands are terminal commands, not Python code; exact setup and navigation depend on the operating system.

Reading the examples in this course

Blocks labeled python contain code. Blocks labeled text or output can show what an example displays; they are not extra instructions to add to the program. Some text blocks instead contain diagrams or tracebacks, as their surrounding explanations indicate. When an example contains multiple lines, their order matters.

The next lesson examines the pieces of the example and how Python handles text and numbers.