Course outline · 0% complete

0/27 lessons0%

Course overview →

Memory: where variables live

lesson 6-2 · ~12 min · 20/27

RAM and storage

When you assigned score = 15 in unit 3, where did the 15 actually go? Into RAM (random-access memory), the computer's fast working memory. RAM is a huge row of numbered byte-sized slots. Each slot's number is its address, like a house number on a very long street. A variable name is your friendly label for "the value at some address", so the labeled-box picture from lesson 3-1 was literal.

RAM has a catch: it is volatile, meaning it is wiped when the power goes off. That is why computers also have storage (an SSD or hard drive): slower, but it keeps data permanently as files.

The everyday consequences:

  • Running programs and their variables live in RAM
  • Saving a document copies data from RAM to storage
  • "Have you tried turning it off and on again?" works partly because a restart clears RAM, discarding whatever bad state a program had built up
score 72 0 15 200 4 99 1002 1003 1004 1005 1006 1007 ... addresses Volatile: cut the power and every slot in this row is blank.
RAM is a numbered row of byte slots, and a variable name is a friendly label for one of them. The address is how the machine finds the value, and the name is how your program does.

Copying a variable copies the value

b = a copies what is in a's box, not a link to the box itself.

a = 5
b = a
a = 99
print(b)

Output

5

b = a copied the value 5 into b's own box, so reassigning a afterwards refilled a's box only. b never heard about it.

This is the behaviour the labeled-box picture predicts, and it is worth confirming deliberately because the alternative is imaginable. If b = a had made both names refer to one box, the output would be 99.

For simple values like numbers and strings, the copy interpretation is always right in Python. Larger structures behave differently, and that difference is a real source of bugs later, so the distinction is worth planting now.

Why unsaved work disappears

The work is gone because the document lived in RAM, which loses its contents without power, and was never copied to storage.

The running editor kept your text in RAM, which is volatile. Saving is exactly the act of copying RAM contents to persistent storage, and until you do it there is only one copy of the work in the least durable place available.

That same split explains something you have felt without naming. Programs feel instant once open and take a moment to launch, because launching means copying them from slow storage into fast RAM.

Modern editors work around the problem by autosaving, which is not a different mechanism. It is the same copy to storage, performed on a timer so that a human forgetting is no longer the failure mode.

What identifies one slot in RAM

It is called an address, or a memory address.

Every byte of RAM has one, exactly like a house number on a very long street. The address is how the machine finds a value, and a variable name is the friendly label your program uses instead.

Python hides addresses from you completely, which is one of the reasons it is a comfortable first language. In the C course you will meet pointers, variables whose value is an address, and programmers there talk about a pointer holding the address of a value.

That hiding is a trade rather than a pure win. Working with addresses directly is how you write an operating system, and it is also how you write a program that overwrites memory it does not own.

Reading memory sizes

Engineers talk about memory and storage in units built from the byte, lesson 6-1's group of 8 bits, and you will see these numbers daily in laptop specs, error messages, and cloud bills:

  • a kilobyte (KB) is 1,024 bytes. It is 1,024 rather than 1,000 because memory hardware is organized around powers of 2, and 1,024 is 2 multiplied by itself 10 times
  • a megabyte (MB) is 1,024 KB, and a phone photo is a few MB
  • a gigabyte (GB) is 1,024 MB, and laptops today carry 8 to 32 GB of RAM

So "this laptop has 16 GB of RAM" means roughly 17 billion labeled byte slots for running programs to use.

The powers-of-2 detail is the source of a genuine everyday annoyance. Drive manufacturers count a gigabyte as 1,000,000,000 bytes while the operating system counts 1,073,741,824, which is why a drive sold as 500 GB shows up as about 465 GB and nobody has cheated you.

Bytes in 4 GB

Each factor of 1,024 steps down one unit.

print(4 * 1024 * 1024 * 1024)

Output

4294967296

Reading the pieces

  • The three 1,024s convert GB to MB, MB to KB, and KB to bytes, so the chain reads as a unit conversion rather than as an arbitrary calculation.
  • Letting Python multiply keeps the reasoning visible. print(4294967296) gives the same output and explains nothing.
  • That number is one you will meet again. 4,294,967,296 is 2 to the 32nd power, which is why 32-bit systems could not use more than 4 GB of memory, and why lesson 6-1's all-bits-on pattern gives 4294967295 as the largest 32-bit value.