Chapter 1 · Foundations
Inside the computer
Step 1 of 17 · Concept
A computer, simplified
By the end of this lesson you know what happens to your program when it runs.
Every computer has three main parts:
| part | what it does | keeps things when off? |
|---|---|---|
| CPU | carries out instructions, one at a time, very fast | no |
| memory | holds the programs that are running, and their values | no |
| storage | holds files, such as main.py | yes |
Step 2 of 17 · Quiz
Where is it kept?
You close the browser and come back tomorrow. main.py is still there. Where was it kept?
Step 3 of 17 · Concept
Memory is numbered boxes
- Memory is a long row of tiny boxes called bytes. A byte holds a number from 0 to 255.
- Each box has a number, its address. The CPU finds a box by its address.
- A value takes several boxes in a row.
Step 4 of 17 · Quiz
What fits in a byte?
A byte holds one number. Which of these numbers cannot fit in one byte?
Step 5 of 17 · Concept
From your file to a running program
Run types python3 main.py (lesson 1). Then:
python3readsmain.pyfrom storage.- It translates all of the file into bytecode: simple instructions, kept in memory.
- Its interpreter carries out the bytecode with the CPU, from the top.
Step 6 of 17 · Quiz
When is the whole file read?
When does python3 translate main.py into bytecode?
Step 7 of 17 · Concept
One line at a time
- The interpreter keeps a program counter: a marker on the next instruction.
- Again and again, it reads that instruction, carries it out, and moves the marker on.
- In this course we put the marker on a line of code. Really, one line becomes several bytecode instructions.
Step 8 of 17 · Trace
Watch the program counter
Press Next to run one line. Watch the marker and the output.
print("Ready")
print("Set")
print("Go")- Line 1 is next. Nothing is printed yet.
- Line 1 ran and printed
Ready. Line 2 is next. - Line 2 ran and printed
Set. Line 3 is next. - Line 3 ran and printed
Go. No line is left, so the program ends.
Step 9 of 17 · Try it
Try it: put it in order
The lines run from the top, one at a time. Move them so the program prints:
3
2
1
Go!
Cut a whole line and paste it where it belongs (on a desktop, Alt+↑ and Alt+↓ move the line the cursor is on). Keep each line at its first column.
Step 10 of 17 · Concept
Input and output
| goes from ... to ... | in Python | |
|---|---|---|
| output | the program to the screen | print, which you know |
| input | the keyboard to the program | input(), in lesson 4 |
| files | the program to storage, and back | open(), in lesson 20 |
Step 11 of 17 · Concept
Every value is an object
While a program runs, every value it uses is an object in memory: 15, 2.5, "Ada".
- Each object knows its type:
intfor a whole number,floatfor a number with a point,strfor text. - Objects live in a part of memory called the heap.
Step 12 of 17 · Concept
A name points at an object
age = 15
price = 2.5
name = "Ada"
- Each line gives a value a name. Lesson 3 teaches you to write them.
- The names live in the
<module>frame, a part of the stack: one box per name. - A box holds an arrow to its object: a reference. The name points at the object.
Step 13 of 17 · Quiz
Where does it live?
A program runs this line. Where are the name name and the text "Ada"?
name = "Ada"
Step 14 of 17 · Trace
Calls stack up
A function is a piece of work with a name, run when it is called (lesson 7 writes them).
Each running call gets a frame of its own, on top of the stack. Press Next and watch the stack.
def greet():
print("Hi")
print("Start")
greet()
print("End")- Lines 1–2 make the function
greetwithout running it. The 4 spaces put line 2 inside it (lesson 6). Line 4 is next. - Line 4 printed
Start. Line 5 callsgreet. - A frame for
greetgoes on top of the<module>frame. Line 2 is next. - Line 2 printed
Hi.greethas no line left, so it returns to line 5, where it was called. - The frame of
greetis gone. Back in the<module>frame, line 6 is next. - Line 6 printed
End. The program is finished, and the stack is empty.
Step 15 of 17 · Quiz
When a function ends
greet has finished. What happened to its frame?
def greet():
print("Hi")
print("Start")
greet()
print("End")Step 16 of 17 · Check yourself
Check yourself: the life of a program
This program prints, in the wrong order, six things that happen when a program calls a function greet. Move the
lines into the order they happen.
Storage comes first: nothing runs before python3 has read your file. A call's frame goes on top of the frame that called it.
Step 17 of 17 · Recap
Recap
- CPU, memory and storage: only storage keeps things when the power is off.
python3translates all ofmain.pyinto bytecode, then its interpreter runs it line by line.- Every value is an object on the heap, with its type.
- A name is a box in a frame, holding an arrow to its object.
- Each running call has a frame on the stack, removed when the call ends.
Next: lesson 3 gives values names with =, and computes with them.
Review
5 questions from this lesson, in a new order each time. Answer from memory: 3 of 5 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
print(1)
print("Go!")
print(3)
print(2)
print("A frame for greet goes on top")
print("python3 reads main.py from storage")
print("The program ends; the stack is empty")
print("The <module> frame goes on the stack")
print("It translates the whole file into bytecode")
print("greet returns; its frame is removed")