Introduction to Programming I

How code runs — the notional machine

Paolo Rota

September 16, 2026

Welcome

This is a course about telling a computer what to do, precisely.

Over 14 lectures we go from “what even is a program?” to reading text off the web and processing it.

No prior experience assumed. The only requirement is willingness to be precise and to be wrong often — errors are how we learn here.

Today you will see some code, but the goal is not yet to write your own. We use it for one thing: to watch a program run in sequence — one instruction after another, in the order you wrote them, and nothing happening in between that you did not ask for.

That sequential nature is the whole of today. Get that picture right and everything later is easier.

Roadmap of the course

Block Lectures
Thinking like a machine 1–2 (today + next)
Data & control flow 3–4
Structuring code 6, 7, 9, 10
Text & the web 12, 13
Hands-on with the tutor 5, 8, 11, 14

Four sessions (5, 8, 11, 14) are practicals: you write code, the tutor helps.

How the course runs

  • 6 CFU, 42 hours — 14 lectures of 3 hours, 16 September to 30 October 2026.
  • Ten taught lectures plus four practicals (5, 8, 11, 14), run by the tutor. You can work on the classroom computers or bring your own laptop.
  • Taught lectures are live coding and exercises in class — you will write code yourself during the lecture.
  • Every taught lecture ends with exercises. Worked solutions follow afterwards.
Where Polo di Rovereto — Palazzo Piomarta, corso Bettini 84
Attendance Not compulsory — but the practicals are hands-on, so come
Office hours By appointment

How you are assessed

One individual practical exam, in class: you solve a series of programming exercises on the topics of the course. No written theory paper, no syntax to recite from memory.

What that means for how you should work:

  • Reading code is not enough. You have to write it, regularly.
  • The end-of-lecture exercises are the training for exactly that task.
  • Getting stuck and getting yourself out is part of what is being examined.

Exam dates are published from around December.

Course materials

Everything lives on Moodle: slides, worked solutions, announcements. Enrol today — didatticaonline.unitn.it, course 44040

There is no compulsory textbook. The slides and the worked exercise solutions are the course material.

Optional further reading, both available through the UniTrento libraries:

  • Sweigart, A. (2015). Automate the Boring Stuff with Python. No Starch Press.
  • Hammond, M. (2020). Python for Linguists. Cambridge University Press.

Neither is required. Take Sweigart if you want a second voice explaining the basics; take Hammond for the linguistic angle this course picks up later.

What is a program?

A program is a recipe

A recipe is a list of instructions, carried out in order, that transforms ingredients into a result.

A program is a list of instructions that a computer carries out in order, transforming data into a result.

The computer is fast and obedient, but utterly literal. It does exactly what you write, even when you meant something else.

The world’s most literal assistant

Imagine an assistant who:

  • follows instructions one at a time, top to bottom;
  • never guesses what you meant;
  • has a perfect memory but no common sense;
  • stops and complains the moment an instruction makes no sense.

Learning to program is learning to write instructions for this assistant.

Our first program

print("Hello, World!")
Hello, World!
  • print(...) is an instruction: “display this on the screen”.
  • The text in quotes, "Hello, World!", is the data it acts on.

print is built in: Python already knows this word. It is one of a few dozen the machine starts with, so you never have to explain it. Ask for a word it does not know and it stops — exactly like our literal assistant.

The notional machine

A model you can run in your head

You will never watch electrons move through the CPU. Instead you keep a simplified, predictable model of the machine in your head and use it to predict what the code will do.

The notional machine is the simplified mental model of the computer that executes your code — just detailed enough to predict a program’s behaviour, and no more.

Expert programmers are not people who memorised syntax. They are people whose notional machine is accurate.

What our notional machine contains

For now, three parts are enough:

  1. The instruction pointer — a finger pointing at the current line.
  2. Memory — a set of labelled boxes that hold values.
  3. The screen — where print writes output we can see.

Execution = move the finger down the lines, doing what each says, updating memory and the screen.

Rule 1 — one line at a time, top to bottom

The finger starts at line 1 and moves down:

print("first")
print("second")
print("third")
first
second
third

Order is not decoration. Swap the lines and the output changes.

Tracing by hand

“Tracing” = pretending to be the notional machine and writing down what happens at each step. This is the single most useful skill in this course.

print("A")
print("B")
print("C")
A
B
C
Step Finger at Action Screen so far
1 line 1 display A A
2 line 2 display B A B
3 line 3 display C A B C

When unsure what code does — trace it.

Rule 2 — the machine is literal

The machine does not read your intentions. This line is missing a closing quote, so the machine cannot make sense of it and stops:

print("Hello, World!)
  Cell In[4], line 1
    print("Hello, World!)
          ^
SyntaxError: unterminated string literal (detected at line 1)

An error is not the machine being mean. It is the machine telling you exactly where it stopped understanding. Read the message — it is a gift.

Two kinds of “not working”

  • Syntax error — the machine can’t even read your instruction (a missing quote, a typo). It refuses to start.
  • Logic error — the machine runs happily but does the wrong thing, because you told it the wrong thing.

The second kind is sneakier: the machine will not warn you, because as far as it is concerned nothing is wrong. Syntax errors it finds for you. Logic errors only tracing will.

What’s inside the boxes?

Only two kinds of thing

Our notional machine says memory is a set of labelled boxes holding values. But a computer is electronics: there are no letters in there, and no numbers either — only circuits that are carrying current, or not.

So how does "Hello, World!" fit inside a machine that can only tell on from off?

We open the lid once, look inside, and close it again.

You already know how this works

Take 123. The same digit means something different in each position:

Position 100 10 1
Digit 1 2 3

100 × 1 + 10 × 2 + 1 × 3 = 123

We have ten digits, 0–9, so each position is worth ten times the one to its right. Nothing in nature forces that ten — it is just how many fingers we happen to have.

A machine has two digits

A circuit either stores electricity or it does not. That gives exactly two digits: 0 and 1.

A binary digit is a bit — the smallest thing a computer can remember.

With ten digits, each position was worth ten times the previous one. With two digits, each position is worth twice the one to its right.

So how do we count past one?

Counting to seven

Three positions, worth 4, 2 and 1. Add up the positions holding a 1:

4 2 1 Value
0 0 0 0
0 0 1 1
0 1 0 2
0 1 1 3
1 0 0 4
1 0 1 5
1 1 0 6
1 1 1 7

That is all binary is.

And eight?

Three bits are full: 111 is seven, with nowhere left to carry.

So we do what decimal does when it runs out at nine — add a position:

8 4 2 1
1 0 0 0

n bits hold 2ⁿ different values. Eight bits — one byte — give 256 of them: 00000000 to 11111111, or 0 to 255.

From numbers to text

Bits give us numbers. Text is then just an agreement about which number means which character.

  • ASCII — 7 bits per character, so 128 slots: enough for English. The eighth bit of the byte was left spare, used to check the character had survived transmission intact.
  • Unicode — over a million characters: every alphabet, plus the emoji.

Once that check fell out of use, every country spent the spare bit on its own alphabet — in different ways. That is why text from elsewhere still arrives broken.

Python knows both agreements, so we can just ask:

print(ord("A"), bin(ord("A")))
print(chr(129504), ord("🧠"))
65 0b1000001
🧠 129504

The same trick, for everything else

  • Colour — three bytes per pixel: how much red, green and blue, each 0–255.
  • Sound — thousands of numbers a second, each the height of the wave.
  • Your program — also just numbers, once the machine has read it.

There is only one kind of stuff inside the machine: numbers, stored as bits. Everything else is an agreement about how to read them.

Pick the wrong agreement when reading a file and text comes out as garbage. We meet that again in lecture 10, reading real text off the web.

Closing the lid

You now know what is in the boxes. For the rest of this course you can forget it.

We work at a higher level: we write "Hello, World!" and Python deals with the bits. That is what a high-level language means — and why Python reads almost like English.

Back to the notional machine: instructions in, results out, labelled boxes in between.

Setting up

What you need installed

Two things, and the first one brings Python with it.

  • Miniconda — a small bundle containing Python plus conda, a tool that builds isolated environments. You do not install Python separately.
  • VS Code — the editor we will write code in.

We install all of this together, right now. There is nothing to work out on your own tonight.

Use the classroom computer today, even if you brought your own laptop. It is the very machine you will sit the exam on, so the sooner it feels familiar the better. Set up your own laptop as well if you like — but do today’s work here.

Already using Anaconda? Keep it — it contains everything Miniconda has.

Installing

Windows

  • Download the Miniconda installer for Windows and run it
  • Accept the defaults; do not tick “add to PATH” — it is not needed
  • Afterwards you get a new program in the Start menu: Anaconda Prompt — that is the terminal we will use

macOS

  • Download the Miniconda installer — the Apple Silicon one for M1/M2/M3, the Intel one otherwise
  • Run it and accept the defaults
  • Afterwards use the normal Terminal app

Then download VS Code from its website and install it normally.

If a step fails, stop and bring the laptop to class. Losing an evening to an installer is the worst possible first experience of programming.

What is a terminal?

A terminal is a window where you type a command as text and the computer answers as text. No buttons, no menus, no icons.

It looks primitive, and it is — but it is the most direct way to tell a computer to do something, and it is how we will start Python.

Windows

Start menu → Anaconda Prompt

macOS

Applications → Utilities → Terminal

You type one line, press Enter, the machine does it and waits for the next. That is our literal assistant again — one instruction at a time, in order.

Creating the course environment

From now on the commands are the same on both systems — that is the whole point of installing conda. Open your terminal and type:

conda create -n intro2python python=3.12 ipykernel
conda activate intro2python
python --version
  • The first line builds a private Python 3.12 called intro2python.
  • The second switches into it. Your prompt gains an (intro2python) prefix.
  • The third should answer Python 3.12.x.

Anything you install inside this environment cannot break anything else on your computer. That is why we bother.

Pointing VS Code at it

VS Code does not find the environment by itself. Two steps, once:

  1. Open the Extensions panel on the left and install the Python extension from Microsoft.
  2. Press Ctrl+Shift+P (Cmd+Shift+P on macOS) to open the Command Palette, type Python: Select Interpreter, and pick intro2python.

Quicker, once a .py file is open: the interpreter in use is shown in the status bar at the bottom right. Click it and the same list appears — that is also how you check, at a glance, which Python you are about to run.

Skip step 2 and your code will run against the wrong Python. This is the single most common reason a beginner’s setup “doesn’t work” — so we will do it together, on your screen, in class.

Running Python

Two ways to talk to Python

The interactive shell (REPL) — in your terminal, with the environment active, type python. You get a >>> prompt: one line in, result straight back. Great for experiments.

>>> 2 + 2
4
>>> print("hi")
hi

A script — a file (e.g. hello.py) with many lines, run all at once. This is how real programs live.

Same notional machine underneath; just different ways to feed it instructions.

The edit → run → read loop

Programming is a loop you go round again and again:

  1. Edit — write or change some instructions.
  2. Run — let the machine execute them.
  3. Read — look at the output and any error message.
  4. Go back to 1.

Beginners fear step 3. Good programmers seek it — the feedback is the point.

And a third: the notebook

A notebook is a file split into cells. You run one cell at a time and its output appears right underneath it.

It is a hybrid of the two we just saw:

  • like the REPL, the state stays alive from one cell to the next;
  • like a script, the file stays on disk, with the code and the results saved inside it.

Notebooks have the extension .ipynb. The worked solutions to every exercise in this course are notebooks — so you need to be able to open one.

Notebooks in VS Code

Two steps, once — we do them together now:

conda install -n intro2python ipykernel

Then in VS Code install the Jupyter extension, next to the Python one you already added.

To use it: create a file ending in .ipynb, click Select Kernel at the top right and pick intro2python. Write code in a cell, press Shift+Enter to run it.

A warning that matters today: you can run cells in any order you like. The machine still executes one instruction at a time — but your reading order and its execution order may no longer match. When a notebook stops making sense, run the cells again from the top.

What to take away

  1. A program is ordered instructions for a literal machine.
  2. Keep an accurate notional machine in your head.
  3. Trace code by hand to predict what it does.
  4. Errors tell you where understanding broke — read them.
  5. Underneath it is all bits; above it, agreements about how to read them.

Next lecture: which values those boxes hold and how they change (state), and the rules for which names are visible where (scope).

Exercises

Exercise 1 — Be the machine

Without running anything, trace this program on paper and write the exact screen output:

print("one")
print("two")
print("one")
print("done")
one
two
one
done

Then run it and compare. Did your notional machine agree with the real one?

Exercise 2 — Spot the literal mistake

Each snippet has one problem. Predict the error message the machine will give, then check.

# (a)
print("Good morning)
  Cell In[7], line 2
    print("Good morning)
          ^
SyntaxError: unterminated string literal (detected at line 2)
# (b)
Print("Good morning")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[8], line 2
      1 # (b)
----> 2 Print("Good morning")

NameError: name 'Print' is not defined
# (c)
print "Good morning"
  Cell In[9], line 2
    print "Good morning"
    ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

Explain in words why each one stops the literal machine.

Exercise 3 — Order matters

Write a program that prints exactly these four lines, in this order:

Ready Set Go Ready

Then, without adding or deleting lines, reorder your existing lines so the output becomes Go / Set / Ready / Ready. What does this tell you about instruction order?

Questions?