Everyone Can Learn Program
中文 Start learning
Course 0 · Before codeLesson 2 of 8Free to read

Inside the machine · Computer Basics

What's inside a computer, and why does unsaved work disappear when the power goes off?

Course contents

Why you need it

Three main parts, the processor, memory and storage, explain a lot of everyday things: why a phone slows down with too many apps open, why you need to press Save, and what "8 GB" and "256 GB" on a price tag mean.

Later, when your own programs remember things, you'll know where those things live.

The intuition

Picture someone doing homework at a desk.

  • The processor (CPU) is the person. It does the actual work, one tiny step at a time, but billions of steps every second.
  • Memory (RAM) is the desk. Whatever you're working on right now lies here, close at hand. It's fast but small, and it is wiped clean when the power goes off.
  • Storage (an SSD or hard drive, or your phone's "256 GB") is the bookshelf. It's big and it keeps everything even with the power off, but it takes longer to reach.

When you open an app, it's copied from the shelf to the desk so the processor can work with it. When you type a document, your words lie on the desk. Save puts a copy on the shelf. That's why unsaved work disappears in a power cut: the desk was wiped, and nothing was on the shelf yet.

Too many apps open and the desk gets crowded. The computer then keeps moving things back and forth to the shelf, and everything feels slow.

Programs keep their values on the desk too. The machine on the right puts a score in memory and changes it step by step.

Definition

Processor (CPU)

The CPU (central processing unit) is the part of a computer that carries out the instructions of a program.

Memory (RAM)

RAM is fast, temporary memory that holds the programs and data in use right now; its contents are lost when the power is off.

Storage

Storage, such as an SSD or a hard drive, keeps files permanently, even when the power is off.

Examples

A score on the desk

Try it yourself

The name score is a spot on the desk (in memory). Each step changes what's there. Change 10 to another number and run it again.

# A spot in memory called score
score = 0
print("Start:", score)
score = score + 10
print("After level 1:", score)
score = score + 10
print("After level 2:", score)
Step by step
  1. L2Put 0 in memory, under the name score.score = 0
  2. L3Show what's in memory now.Start: 0
  3. L4The CPU reads score (0), adds 10, and puts 10 back in the same spot.score = 10
  4. L6Again: 10 + 10. The old value is replaced.score = 20
  5. L7Show the new value.After level 2: 20

Fast desk, slow shelf

Try it yourself

Real times are far too small to imagine, so this machine scales them up: if reaching memory took 1 second, how long would storage take? The numbers are rough, but the gap is real.

# If reaching memory (the desk) took 1 second...
memory = 1
ssd = memory * 1000          # roughly 1000 times slower
hard_drive = memory * 100000  # roughly 100000 times slower
print("Memory:", memory, "second")
print("SSD:", ssd // 60, "minutes")
print("Hard drive:", hard_drive // 3600, "hours")
Step by step
  1. L2Pretend one trip to memory takes 1 second.memory = 1
  2. L3An SSD is roughly a thousand times slower.ssd = 1000
  3. L4An old spinning hard drive is roughly a hundred thousand times slower.hard_drive = 100000
  4. L61000 seconds is about 16 minutes.SSD: 16 minutes
  5. L7100000 seconds is about 27 hours.Hard drive: 27 hours

A message is typed into memory and saved to storage halfway through. Then the power goes off. Run it and see which words survive.

memory = "See you at"
storage = ""
# Press Save: copy memory to storage
storage = memory
# Keep typing (only in memory)
memory = memory + " 6 pm!"
# Power cut: memory is wiped
memory = ""
print("Memory after restart: [" + memory + "]")
print("Storage after restart: [" + storage + "]")
Step by step
  1. L1The words you typed are in memory.memory = "See you at"
  2. L4Save copies them to storage.storage = "See you at"
  3. L6You keep typing. The new words are only in memory.memory = "See you at 6 pm!"
  4. L8The power goes off and memory is wiped.memory = ""
  5. L10Storage still has what was saved. " 6 pm!" is lost.Storage after restart: [See you at]

Common mistakes

Memory and storage on a price tag

A phone sold as "8 GB + 256 GB" has 8 GB of memory (the desk) and 256 GB of storage (the shelf). More storage holds more photos; more memory lets more apps stay open at once.

"Closing an app deletes it"

Closing an app clears it off the desk (memory). It's still on the shelf (storage), so it opens again next time. Deleting it removes it from storage.

Not everything saves itself

Many apps save for you as you go, but not all of them. When something matters, press Save (or check that it says saved) before you close it.

Quick quiz

1. Which part actually carries out a program's instructions?

  1. AStorage
  2. BThe CPU
  3. CThe screen
Show the answer

Answer: B

The CPU (processor) does the work. Memory and storage hold things; the screen shows the output.

2. You were typing and the power went out before you pressed Save. Where were your words?

  1. AOnly in memory, which is wiped when the power goes off
  2. BSafe in storage
  3. CInside the screen
Show the answer

Answer: A

Until you save, your work lives only in memory (RAM). Saving copies it to storage, which survives a power cut.

3. A phone has 6 GB of RAM and 128 GB of storage. You want room for more photos. Which number matters?

  1. AThe 6 GB of RAM
  2. BThe 128 GB of storage
  3. CNeither
Show the answer

Answer: B

Photos are files that are kept, so they go on the shelf: storage.

Key points

  • The CPU does the work, memory (RAM) holds what's in use now, storage keeps files for good.
  • Memory is fast but wiped when the power goes off; storage is slower but permanent.
  • Save copies your work from memory to storage.

Practice

3 short exercises: think it through, change something and see what happens, then do one yourself. Answers are checked the moment you submit.

  1. ThinkShelf to desk

    You tap a game to open it. Before the CPU can run it, the game is copied from ___ to ___.

  2. ChangeSave in time

    In this machine the power goes off before anything is saved, so it prints an empty Saved: line.

    Add one line, storage = memory, in the right place so that after the power cut the machine prints:

    Saved: Meet at the park at 6

    Don't change the message itself.

  3. Your turnDesk or shelf?

    For each thing below, write M if it's in memory (gone when the power goes off) or S if it's in storage (kept):

    1. A photo in your phone's gallery
    2. The score of the game round you're playing right now
    3. An app you installed last week
    4. Text you typed but never saved

    Write four letters in order, for example MMSS.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
CPU (processor)处理器The part of a computer that carries out a program's instructions.
Memory (RAM)内存Fast, temporary space for programs and data in use; wiped when the power is off.
Storage存储Permanent space for files, such as an SSD or hard drive; kept when the power is off.
Save保存Copy your work from memory to storage so that it is kept.