How computers run code
Why this matters
Every tool in this course, from a ten-line script to a large language model, runs on the same machine: a processor stepping through instructions and a long row of numbered memory slots. Once you can picture that machine, many puzzles go away. You'll see why one change shows up in two variables, why an API call is slow next to a loop, and why NumPy beats plain Python.
- Explain how text, numbers and code all end up as bytes in memory
- Predict when two variables share one object, and avoid the bugs that causes
- Describe how a processor runs instructions, and how Python's interpreter fits on top
- Build a small stack machine, the same design Python's own virtual machine uses
Everything is numbers
Think of a row of light switches, each one either off or on. A computer stores everything that way, as bits: tiny switches that are off (0) or on (1). Eight bits make a byte, which can hold a whole number from 0 to 255.
Everything else is built from bytes, the way a paint-by-numbers picture is built from numbered colors. A photo is a list of color numbers. A song is a list of loudness numbers. Text is a list of character codes.
Python shows you the numbers behind text. ord gives a character's code, and encode turns a string into the bytes a file or a network would carry:
print(ord("A"), ord("a"), ord("€")) print(bin(65)) # 65 written in base 2: the bits themselves data = "café".encode("utf-8") print(data, len(data)) # é needs two bytes in UTF-8 print(data.decode("utf-8")) # and back again
The rule that maps characters to numbers is called an encoding. Think of it as a codebook both sides agree on. UTF-8 is the one the web uses. It stores plain English letters in one byte each, and other characters in two to four bytes. When text arrives scrambled, like café, someone decoded UTF-8 bytes with the wrong codebook.
In plain words: a letter, a color and a sound are all numbers to a computer. An encoding is the agreement about which number means what.