Introduction: what you do with Python and how to begin

Opening the same export every week, cleaning up columns, calculating a total and pasting the result into an email. Work like this takes no brainpower, but it does take hours. Python is the language you use to leave that kind of task to the computer. Most Python code in the world is small: a forty-line script that renames files, checks a list or converts an export. You write a script like that once and use it for years.

In this free course, you write code yourself and run it. You do not need any programming experience, only a computer on which you are allowed to install Python.

After this introduction
  • You know where Python comes from and what the language is often chosen for.
  • Do you have Python 3 on your computer, and do you know which command works for you?
  • You know the difference between the interactive prompt and a .py file.
  • You know the course schedule that returns in all chapters.

A Dutch language built for readability

Python began around 1989 as a project by the Dutch programmer Guido van Rossum. He wanted a language that was simpler than what was used at his workplace, yet serious enough for real systems. The name comes from the comedy group Monty Python. The underlying principle was that code is read far more often than it is written. That is why Python deliberately keeps the layout of code simple and the same for everyone, so that code written by different programmers looks very similar.

Python 3 was released in 2008, deliberately not fully interchangeable with Python 2. Since 2020, Python 2 has no longer been supported. If you come across an example online in which print "Hallo" zonder haakjes staat, dan kijk je naar oude Python 2-code. In deze cursus werk je met Python 3.

What Python is good at, and what it is not

Python is a generalist. The language is not the fastest or the most specialised, but it is a poor choice in almost no situation. You find it in scripts and automation, in data analysis (where Python has become the standard), on the server side of web applications and in test scripts. Three characteristics explain this. The learning curve is short: you write working code on your first day, without first having to learn what compiling is. There is an enormous amount of ready-made code, and much of it is already in the standard library that comes with Python. And the same code runs on Windows, macOS and Linux.

The downside: Python is relatively slow. For calculations where every microsecond counts, such as games or software in devices, developers choose another language. Python is also not a common choice for apps on your phone.

Installing Python and checking the version

On macOS and Linux, Python is usually already installed, often in an older version than you want. On Windows it is not installed by default. On Windows, you download the installer from python.org. One choice matters here: the option that adds Python to your PATH, so that you can start Python from the command line. On a Mac, you install your own version alongside the system version, and on Linux you install Python through the package manager.

Then check which version you have. Open a terminal (on Windows, Command Prompt or PowerShell) and type python --version en drukt op Enter. Krijg je de melding dat het commando niet bestaat, dan probeer je python3 --version. Werkt dat ook niet, dan staat Python niet in je PATH. Op de computer waarop deze cursus is gemaakt, gaf python3 --version dit antwoord:

In the terminal

Python 3.15.2

Any version number that starts with 3 is fine. Make a note of which command works for you: python of python3. Dat gebruik je de rest van de cursus. In de voorbeelden staat steeds python.

Tip

A development environment (an editor that colours your code and points out errors as you type) is useful, but not necessary. For this course, a simple text editor and a terminal are enough. That is why the chapters contain no menu paths or screenshots, only code and the output in the terminal.

The interactive prompt and a .py file

Python is an interpreted language. You write source code, and an intermediate program, the interpreter, reads and executes it. You can use that interpreter in two ways.

If you start python zonder bestandsnaam, dan kom je in de interactieve prompt. Je ziet drie groter-dan-tekens, >>>, en alles wat je typt wordt meteen uitgevoerd. In de prompt hoef je niet eens print() te gebruiken: typ je een berekening, dan toont Python het antwoord. Een waarde die je aan een naam geeft, zoals prijs = 395, levert geen uitvoer op; die bewaart Python voor de volgende regel.

In the interactive prompt

>>> print("Hallo Python")
Hallo Python
>>> 12 * 3
36
>>> prijs = 395
>>> prijs * 3
1185

You leave the prompt with exit(). Alles wat je typte is dan weg.

The second way is a file. You put your code in a text file with the extension .py, for example rooster.py, and start it in the terminal with python rooster.py. De interpreter leest het bestand van boven naar beneden. Je code blijft bewaard, je kunt hem aanpassen en opnieuw draaien. Let op: in een bestand toont Python alleen wat je met print() laat zien. Een losse regel 12 * 3 rekent wel, maar je ziet er niets van.

Two rows. Top: the prompt with 12 * 3 and the answer 36, via the interpreter that executes every line immediately, to an answer on the screen that then disappears. Bottom: a file rooster.py with three print lines, via the interpreter that reads the file from top to bottom, to output, while the file is kept.
The prompt for a quick try-out, a file for everything you want to keep.

Rule of thumb: the prompt is your scratchpad. Use it to try something quickly, check a calculation or provoke an error message to see what happens. Use a file as soon as you build something you will need again tomorrow. In the chapters you will almost always work in a file. A second window with the prompt next to it is useful: you can test a single line there while you continue reading.

The course schedule as a common thread

Separate examples per chapter give you the feeling that you are learning tricks. That is why one program runs through all the chapters. A training provider offers courses, each with a name, a number of days and a price. Someone on the phone wants to know which courses are available, which is the shortest and whether there are still places. In chapter 1 you put the schedule on the screen as text. In chapter 2 you calculate with prices and create neat lines. In chapter 3 each course gets its own collection of data, and in chapter 4 the program looks up a course that someone types in. The training provider and the prices in the examples are fictional.

Alongside the schedule, each chapter has examples from ordinary office work: lunch costs for a workshop, a mileage allowance, expense claims per colleague, a stock list. This shows that the same few lines of Python work for very different tasks.

Exercise

Before you start, make a note of your starting point. Write down which command works for you (python of python3) en welk versienummer je ziet. Daarnaast schrijf je in drie tot vijf regels een klus uit je eigen werk op die je nu met de hand doet en die zich steeds herhaalt: een bestand dat elke week dezelfde bewerking krijgt, een lijst die je telkens controleert. Die klus houd je bij de hand. Aan het eind van hoofdstuk 4 kijk je of je hem al in Python kunt uitdrukken.

How to work with the chapters

Each chapter begins with learning objectives and ends with a knowledge check of three questions. In between is the explanation with short code examples. Below each example, you see the output exactly as Python gave it. You learn most by typing the code yourself rather than copying it: making typing mistakes and fixing them is exactly how you learn to read Python. If you see something different from what is in the course, first look at the bottom line of the error message. How to read it is explained in chapter 1.

Share this chapter: