How a Computer Works: Components, Running a Program and Programming Languages
- Explain the role of the CPU, RAM, storage and registers, and how they work together
- Tell the difference between temporary memory (RAM) and permanent storage, and explain why both are needed
- Describe step by step what happens from clicking a program until it runs
- Explain the CPU’s Fetch → Decode → Execute cycle
- Explain why the processor does not understand the code we write, and why we need programming languages
- Tell the difference between low-level and high-level languages
In this lesson
This lesson is built from a few short chapters, in the same order as the slides:
- Chapter 1 — the computer’s components: CPU, RAM, storage and registers
- Chapter 2 — what happens from the moment you start a program until it runs
- Chapter 3 — kinds of programming languages: from machine code to Python
Chapter 1: The Computer’s Components: CPU, RAM, Storage and Registers
What will we learn?
We will open the “lid” of the computer and meet four components every programmer needs to understand:
| Component | In one line |
|---|---|
| CPU | The brain — carries out the instructions. |
| Registers | Tiny, extremely fast memory cells inside the CPU. |
| RAM | The work surface — fast, temporary memory for the programs running right now. |
| Storage | The archive — permanent storage for files, even when the computer is off. |
Why do we need this?
Every line of code you write in the course passes through these components. Once you understand them, you will also understand things that currently look like “magic”:
- Why does a variable disappear when the program ends, while a file stays?
- Why is a program that reads from the disk slower than a program that works only in memory?
- Why does the computer “freeze” when you open too many programs?
- In the backend and databases topics, why do we store information in a database (on storage) and not just in a variable?
Job interviews often ask “what is the difference between RAM and storage” — it is a question that checks whether you understand the basics.
Explanation from the basics
CPU — Central Processing Unit
The CPU (also called the “processor”) is the component that carries out instructions. It can only do very simple operations: add two numbers, compare, copy a value from place to place, jump to another instruction. Its power is speed: billions of instructions per second.
- Clock speed, measured in GHz: 3 GHz ≈ three billion “ticks” per second.
- Core: a modern processor has several cores, and each core is like a small separate processor. 8 cores = 8 things in parallel.
The link to running a program: every line of Python you write is eventually translated into many small instructions, and the CPU carries them out one after the other.
Registers
Registers are tiny memory cells (each holds one number) inside the CPU itself. They are the fastest memory in the computer. To add two numbers, the CPU first brings them from RAM into registers, calculates, and stores the result in a register.
A processor has very few registers (a few dozen), so they hold only what the CPU is working on at this very moment. In Python you never access registers directly — the interpreter and the operating system take care of that — but it is important to know they exist.
RAM — Random Access Memory
RAM is the memory that holds the programs running right now and their data. When you write name = "Dana", the value "Dana" is stored in RAM.
Two important properties:
- Fast — much faster than storage (thousands of times).
- Temporary (volatile) — when the program ends or the computer turns off, the content is erased.
Storage — permanent storage
Storage is the disk: an SSD (fast, no moving parts) or an HDD (a spinning disk, older and slower). This is where the operating system, the installed programs and all your files are kept, including hello.py.
- Permanent (non-volatile) — the information stays even when the computer is off.
- Large and cheap — hundreds of GB up to TB, compared to 8–32 GB of RAM.
- Slow compared to RAM.
- Storage = the pantry: holds a lot, everything is kept for a long time, but you have to walk over to fetch things.
- RAM = the kitchen counter: you put on it only what you are cooking right now. Convenient and fast, but at the end of the day you clear it.
- Registers = the cook’s hands: they hold only the thing being worked on at this moment.
- CPU = the cook: carries out the actions (chops, stirs, calculates).
Why not settle for one component?
| Registers | RAM | Storage | |
|---|---|---|---|
| Speed | Fastest | Fast | Relatively slow |
| Size | Tiny (bytes) | Medium (GB) | Huge (hundreds of GB–TB) |
| Kept after power-off? | No | No | Yes |
| Price per GB | Very expensive | Expensive | Cheap |
No single component is very fast, huge, cheap and permanent all at once. So the computer combines them all: what is needed now is kept close to the CPU, and what is needed over time — on the disk.
Reading a computer’s spec
Everything we learned appears in every computer’s spec — in the store, on the maker’s site, or in Task Manager. A typical laptop spec:
CPU 8 cores, 3.2 GHz
RAM 16 GB (temporary)
Storage 512 GB SSD (permanent)
OS Windows 11, 64-bit (x64)| Line | What it says |
|---|---|
8 cores | Eight cores — up to eight things the CPU really does in parallel. |
3.2 GHz | The clock rate — about 3.2 billion ticks per second. |
16 GB RAM | How much room there is for the programs running now and their data. |
512 GB SSD | How many files and programs can be kept over time. |
64-bit (x64) | The architecture — the “instruction language” the processor understands. |
You can see the same data on the computer itself: on Windows in Task Manager (Ctrl + Shift + Esc) → Performance, and on a Mac in Activity Monitor. There you can also see in real time how much RAM is in use, and how it grows as you open more and more programs.
RAM versus storage in practice
| RAM | Storage | |
|---|---|---|
| What is there | Programs running now and their data | Files, installed programs, the operating system |
| Speed | Very fast | Slower |
| Typical size | 8–32 GB | Hundreds of GB up to TB |
| When the program ends or the computer turns off | Erased | Kept |
The link to programming: every variable you write in a program lives in RAM. On every run the program gets new, empty memory, and when it ends — the memory is released and the values disappear. That is why a counter inside a program “starts from zero” on every new run.
What needs to stay — a list of students, orders, grades — must be written to storage: to a file, or to a database. We will learn both ways later in the course.
Common mistake
The mistake — “I added RAM, so the computer will store more files”, or the opposite: “I have a 1 TB disk, so I’m not short of memory”.
These are two different resources:
- Too little RAM → the computer is slow when many programs are open. The operating system starts moving parts of RAM to the disk (called swap / page file), and the disk is slow.
- Too little storage → no room to save files and install programs.
Second mistake — “I saved”, but only in RAM. A program shows the message “Saved!”, but in fact the information is only in a variable, which means in RAM. As long as the program runs — everything looks fine. When it closes or the computer turns off — the data disappears. Real saving always means writing to storage.
Summary
- The CPU carries out instructions. Cores allow parallel execution, and GHz measures the tick rate.
- Registers are the tiniest, fastest memory, inside the CPU, holding what is being calculated right now.
- RAM is fast and temporary: variables live in it and disappear at the end of the program.
- Storage (SSD/HDD) is slower but permanent: files stay even after power-off.
- For saving to survive, you need to write to storage (a file / a database).
Official documentation
Chapter 2: What Happens from the Moment You Start a Program Until It Runs
What will we learn?
In the previous chapter we met the components separately. Now we will connect them into one story: what happens from the moment you start a program until it runs. We will go over the general path of every program, and then the special path of a Python file.
Why do we need this?
- When a program “won’t start”, you will know to ask at which step it failed: the file was not found? No permission? Not enough memory?
- When the Python topic talks about a runtime error versus a syntax error, the difference comes exactly from the steps we will learn here.
- In the backend and databases topics we will run a server — a program that does not end but keeps running and waiting for requests. To understand that, you first need to understand what a running program is.
Explanation from the basics
A saved program versus a running program
A program that is not running is just a file on the disk — a sequence of bytes on storage. For it to “do” something, it has to be loaded into RAM and the CPU has to carry out its instructions. A program while it runs is called a process. This distinction is very important, and we will go deeper into it in the next lesson.
The general path — 6 steps
- Request — the user double-clicks an icon, or types a command in the terminal.
- Locating — the operating system looks for the program’s file on storage.
- Permission check — the operating system checks that this user is allowed to run the file.
- Creating a process — the operating system registers a new process, gives it an identification number (PID — process ID) and allocates it its own memory area in RAM.
- Loading into RAM — the operating system copies the program’s instructions from the disk into the process’s memory area, and puts it in the CPU’s queue.
- Execution — the CPU starts carrying out the instructions, one after the other, in a cycle called Fetch → Decode → Execute.
The Fetch → Decode → Execute cycle
This is the “heartbeat” of every computer. The CPU repeats it billions of times per second:
| Step | What happens | Which component |
|---|---|---|
| Fetch | The CPU brings the next instruction from RAM. A special register (the program counter) remembers where it is. | RAM → registers |
| Decode | The CPU works out which instruction it is: add? compare? copy? | CPU |
| Execute | The CPU carries it out. The result is stored in a register, and if needed — goes back to RAM. | CPU, registers, RAM |
A cook with a recipe: Fetch — reads the next line of the recipe. Decode — understands that it says “chop an onion”. Execute — chops. Then moves on to the next line. The recipe (the program) lies on the counter (RAM), and the cook (CPU) holds in their hands (registers) only what they are working on right now.
And what happens with Python?
The CPU does not understand Python. It understands only the machine code of its own architecture. So what happens when you type python hello.py?
- The operating system finds the program
python(the filepython.exeon Windows), creates a process and loads the program into it. What runs on the CPU is the interpreter, which is itself a program in machine code. - The interpreter reads
hello.pyfrom the disk. - It checks the syntax and translates (compiles) the whole file into a simple intermediate language called bytecode. If there is a syntax error, the process stops here, before a single line runs.
- The PVM (Python Virtual Machine), the part of the interpreter that executes bytecode, goes through the instructions one after the other and carries them out. If a certain line fails (for example a division by zero), an error happens at runtime, and the lines that already ran — have run.
- When the code ends, the process ends and the operating system frees its RAM.
hello.py (Storage)
│ python reads the file
▼
Syntax check + compile ──► Bytecode (in RAM)
│
▼
PVM executes bytecode instruction by instruction
│ each instruction = many machine instructions
▼
CPU: Fetch → Decode → Execute (billions per second)Every run is a new process
When you run the same file three times, the operating system creates three different processes — each with its own PID (process ID) and a new memory area:
python hello.py -> new Process, PID 14872
python hello.py -> new Process, PID 9316
python hello.py -> new Process, PID 20480The program (the file hello.py) did not change. Each process is a separate run of it, which is born, runs and disappears. You can see this with your own eyes in Task Manager: a process called python appears while the program runs, and disappears when it ends.
Common mistake
The mistake: thinking all errors happen at the same moment. There are two completely different moments:
print("Line 1 runs?")
print("Line 3"
→ SyntaxError: '(' was never closed
(nothing runs at all)
print("Line 1 runs")
print(10 / 0)
→ Line 1 runs
→ ZeroDivisionError: division by zero- A SyntaxError is caught at the syntax check step, before bytecode is created — so no line runs, not even the first one.
- A ZeroDivisionError is a runtime error — the PVM has already carried out the lines before it, and stops only when it reaches the problematic line.
This is how you identify at which step of the path the problem is: if nothing was printed — it is probably syntax. If part was printed — the problem is in the line where the program stopped.
Summary
- A program on the disk is just a file. When it runs, it is a process with its own PID and an area in RAM.
- The operating system finds the file, checks permissions, creates a process and loads the program into its memory in RAM.
- The CPU carries out instructions in the Fetch → Decode → Execute cycle, with the help of registers.
- In Python: the code is checked and translated into bytecode, and then the PVM carries it out.
- A syntax error stops before the first line. A runtime error stops in the middle, after earlier lines have already run.
Official documentation
Chapter 3: Kinds of Programming Languages: from Machine Code to Python
What will we learn?
In the previous chapter we saw that the CPU carries out instructions in the Fetch → Decode → Execute cycle. Now we ask: where do these instructions come from? We will learn:
- What machine code is, and why nobody writes in it.
- The difference between low-level and high-level languages.
- The two ways to translate code into instructions: a compiler and an interpreter.
- Where Python stands, and why we chose it for the course.
Why do we need this?
When you start programming, you constantly hear sentences like “C is fast”, “Python is slow”, “JavaScript runs in the browser” or “you need to compile it”. Without understanding how code becomes instructions the CPU carries out, these sentences sound like magic.
This understanding helps in practice too:
- Understanding why Python finds a syntax error even before the code runs, but other errors only in the middle of the run.
- Choosing the right tool for the job — and understanding why companies use several languages side by side.
- Answering a classic job-interview question: “What is the difference between compiled and interpreted?”
Explanation from the basics
What the CPU really understands
The processor understands only binary instructions — sequences of zeros and ones. Each instruction is a very small action: “copy a number into this cell”, “add two numbers”, “jump to another address”. This is called machine code.
Writing a whole program in zeros and ones is almost impossible for humans: it is hard to read, hard to find mistakes, and every kind of processor has a different machine code.
A programming language is a bridge: we write in a language that is convenient for us, and a special program translates it into instructions the CPU understands.
Low-level versus high-level
The same action — storing the letter a — at several levels:
Machine Code 10110000 01100001
Assembly MOV AL, 61h
C char c = 'a';
Python c = "a"- Low-level — close to the machine. You control every detail: memory, registers, every instruction. Very fast, but hard to write and easy to get wrong. Examples: assembly, and to a large extent C as well.
- High-level — close to human language. The language takes care of the details for us: memory management, data types, ready-made lists and dictionaries. Easy to write and read, and usually slower at runtime. Examples: Python, JavaScript, Java.
Compiled — translating everything in advance
In a compiled language, a program called a compiler reads the whole code before running it, checks it, and translates it into an executable file in machine code. Then you run that file — as many times as you like, without translating again.
Compiled (C, Go, Rust):
source code -> compiler -> program.exe -> run
translate once, run many times- Advantage: very fast execution — the translation is already done.
- Disadvantage: after every change you need to compile again, and the executable fits a specific operating system and processor.
Interpreted — translating while running
In an interpreted language, a program called an interpreter reads the code while it runs, and carries it out as it translates.
Interpreted (Python, JavaScript):
source code -> interpreter -> run, line by line
translate every time you run- Advantage: very convenient for development — change a line and run immediately. The same code runs on any computer with the interpreter installed.
- Disadvantage: slower at runtime, because the translation happens again every time.
A compiler is like a translator who translates a whole book before it reaches the stores. It takes time, but afterwards every reader simply reads — fast.
An interpreter is like a simultaneous interpreter at a lecture: translating sentence after sentence, in real time. No waiting in advance, but every lecture needs an interpreter all over again.
Well-known languages and what they are used for
| Language | Level | Mainly used for… |
|---|---|---|
| Python | High-level, interpreted | Data, artificial intelligence, servers, automation |
| JavaScript | High-level, interpreted | Websites — in the browser and on the server |
| Java / C# | High-level, compiled to an intermediate step | Large business systems, apps |
| C / C++ | Relatively low-level, compiled | Operating systems, games, hardware |
And where is Python?
Python is a high-level, interpreted language. But behind the scenes there is an intermediate step, as we saw in the previous chapter:
- Python reads the whole file and checks the syntax — that is why a SyntaxError stops everything before the first line.
- It translates the code into bytecode — simple intermediate instructions.
- The PVM (Python Virtual Machine) runs the bytecode instruction by instruction — and errors such as ZeroDivisionError are found only when that line is reached.
Another important property: Python is a dynamic language — you do not declare a variable’s type in advance. The same variable can hold a number and later text.
Common mistake
“Which language is the best?” — a question with no answer.
- There is no best language — there is a language that fits the job. A 3D game needs the speed of C++; data analysis needs Python’s libraries; a website needs JavaScript.
- A language that is fast at runtime is usually slower to write, and the other way round.
- The ideas carry over between languages: variables, conditions, loops, functions and objects exist in almost every language. Someone who learned one language well learns the next one much faster.
Another mistake: thinking that “interpreted” means “checks nothing in advance”. Python does check the syntax of the whole file before running — it just does not produce a separate executable file.
Summary
- The CPU understands only machine code — zeros and ones. A programming language is the bridge between us and it.
- Low-level — close to the machine, fast and hard. High-level — close to people, readable and convenient.
- A compiler translates everything in advance into an executable — fast execution. An interpreter translates while running — convenient development.
- Python is high-level, interpreted and dynamic, with a bytecode step that the PVM runs.
- There is no best language — there is a language that fits the job, and the ideas carry over between languages.
Official documentation
Practice
Comprehension questions on the lesson material — no code.
Exercise 1: Which component?
For each description — which component fits: CPU, registers, RAM or storage?
- Erased when the computer turns off, and holds the variables of a running program.
- Carries out billions of instructions per second.
- Keeps the files even when there is no power.
- Tiny, extremely fast memory cells, inside the processor.
Show solution
- RAM — fast, temporary (volatile) memory.
- CPU — the processor, which carries out the instructions.
- Storage (SSD/HDD) — permanent (non-volatile) storage.
- Registers — hold what the CPU is working on at this moment.
Exercise 2: Reading a spec
This is a laptop’s spec:
CPU 8 cores, 3.2 GHz
RAM 16 GB
Storage 512 GB SSD- What does
8 coresmean? - Danny says: “I have 512 GB, so I can open as many programs as I want without the computer slowing down”. What is the mistake?
Show solution
- Eight cores — up to eight things the CPU really does in parallel.
- The 512 GB are storage — room to save files. The programs running now are in RAM, and it has only 16 GB. When RAM fills up, the operating system moves parts of it to the disk (swap), and the computer slows down.
Exercise 3: From a file to a process
Dana double-clicks the icon of a program installed on the computer. Put the four steps in the order they happen — from the moment the program is saved on the disk until it runs. Write the order of the letters.
- (a) The operating system creates a new process, with a PID and its own memory area
- (b) The operating system finds the program’s file on the disk (storage)
- (c) The CPU carries out the program’s instructions, one after the other (fetch, decode, execute)
- (d) The operating system loads the program’s instructions from the disk into the process’s memory area in RAM
Show solution
The order: (b), (a), (d), (c)
- (b) The operating system finds the program’s file on storage.
- (a) It creates a new process — gives it a PID and allocates it its own memory area in RAM.
- (d) It copies the program’s instructions from the disk into the process’s memory area.
- (c) The CPU carries out the instructions one after the other — fetches, decodes and executes.
Why (a) before (d)? First you need a “home” in memory — the process with its memory area — and only then can the instructions be copied into it.
Exercise 4: An error — at which step?
In each of the following cases: is it a syntax error (SyntaxError) or a runtime error? And was the first line printed?
# Program A
print("Start")
print("End"
# Program B
print("Start")
print(10 / 0)Show solution
- Program A — SyntaxError (a parenthesis was not closed). Python checks the syntax of the whole file before it runs any line, so nothing was printed, not even
Start. - Program B — a runtime error (
ZeroDivisionError). The syntax is fine, so the program started running:Startwas printed, and the error happened only when it reached the second line.
Exercise 5: Compiled versus interpreted
- What is the difference between a compiler and an interpreter?
- Give one advantage of each approach.
- Where does Python fit in the picture, and what is the role of bytecode and of the PVM?
Show solution
- A compiler translates all the code in advance into an executable file in machine code. An interpreter translates and carries out the code while it runs.
- Advantages:
- Compiled — fast execution, because the translation is already done.
- Interpreted — convenient for development: change a line and run immediately, and the same code runs on any computer that has the interpreter.
- Python is a high-level, interpreted language. Behind the scenes it checks the syntax and translates the code into bytecode (intermediate instructions), and then the PVM (Python Virtual Machine) runs the bytecode instruction by instruction.