Code Bytes · "How Computers Work" · Episode 4 · Hardware
RAM vs storage
RAM (random access memory) is fast working memory that holds what the computer is using right now and forgets it when the power goes off. Storage is bigger and slower, but it keeps your files with the power off. A Python variable lives in RAM; to keep it, save it to a file in storage.
Two kinds of memory
Every computer, phone and games console has both:
- RAM (random access memory) is the computer's main working memory. It holds the programs and data in use right now. It's much faster than storage, but it is volatile: everything in it is lost when the power goes off.
- Storage is the drive inside your computer (a solid-state drive or a hard drive). It is usually much bigger and slower than RAM, but it is non-volatile: it keeps your files with the power off.
Think of RAM as your desk, where the papers you're working on sit within easy reach, and storage as your filing cabinet, where everything is kept safe until you need it.
Running a program: storage → RAM
Your programs live in storage as files. When you run one, the operating system copies it from storage into RAM, and the CPU works with it there. While a Python program runs, its variables are held in RAM:
``python score = 10 print(score) ``
`` 10 ``
score = 10 makes a variable in RAM. When the program ends, that memory is handed back, and the variable is gone.
Keeping it: save to a file in storage
To keep a value after the program ends, save it to a file, which lives in storage:
``python with open("score.txt", "w") as f: f.write("10") ``
Run as a program, this prints nothing. It creates a file called score.txt holding 10. (The "w" means write. In the interactive Python prompt you would also see 2, the number of characters written.)
Later, a brand-new program can open that file, and the 10 is still there:
``python print(open("score.txt").read()) ``
`` 10 ``
Try it yourself
Open any free online Python editor (search "online Python") and run:
``python notes = "buy milk" with open("notes.txt", "w") as f: f.write(notes) print(open("notes.txt").read()) ``
You should see:
`` buy milk ``
The variable notes lived in RAM only while the program ran, but notes.txt stays in storage. Run just the last line in a new program and you'll get buy milk back.
Good to know
- Every snippet on this page was run with Python 3 (3.13) and printed exactly what is shown. The files they made were deleted afterwards.
- Random access means the computer can reach any spot in RAM directly, in about the same time, instead of going through it in order.
- This is a simplified picture. The CPU also has small, even faster memory of its own (registers and cache), and when RAM fills up, a computer can move parts of it out to storage for a while.
- Gigabytes (GB) are a unit of size here. Bytes were covered in Code Bytes EP26.
Answer to the comment question
16 GB of RAM, 512 GB of storage. How many times bigger is the storage?
32 times bigger.
512 ÷ 16 = 32. You can check it in Python 3:
``python print(512 // 16) ``
`` 32 ``
Test your knowledge
Quick quiz: RAM vs storage
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Britannica, "RAM": random-access memory is a computer's main working memory; any location can be reached directly, and its contents are lost when the power is turned off
- Britannica, "computer memory": main memory holds the programs and data in use, while slower, larger secondary storage such as disks keeps them when the power is off
- Python Software Foundation, "Reading and Writing Files": open() with mode "w" writes a file, and read() reads it back
- Python Software Foundation, "Built-in Functions: open()"
Video transcript
Why does your computer need both RAM and storage?
RAM is your desk. Storage is your filing cabinet.
RAM, random access memory, holds what the computer is using right now. It's fast, but forgets everything when the power goes off.
Storage, the drive inside, is bigger and slower, but it keeps your files with the power off.
Running a program copies it from storage into RAM. In Python, score = 10 makes a variable in RAM.
When the program ends, it's gone. To keep it, we save it to a file in storage.
Later, a new program opens that file, and the 10 is still there.
16 gigabytes of RAM, 512 gigabytes of storage. How many times bigger is the storage? Comment your answer below!
That's your byte.
More from "How Computers Work"
- "How Computers Work" EP1Bits and bytes
- "How Computers Work" EP2How text is stored
- "How Computers Work" EP6What an operating system does
- "How Computers Work" EP7Machine code vs high-level code