Skip to content

Week 7: How the machine works, binary, and data representation

~9 min

This week's goal

This week you learn what happens between a line of Python and the hardware that runs it, and how numbers and text are stored as bits. You will see that the computer stores everything as patterns of 0 and 1, and that Python hides most of this until you look for it. By the end you can convert a number to binary by hand and with a short script, and explain why 0.1 cannot be stored exactly.

The main text is the how the machine works page. The Python parts use the official Python documentation.

Day-by-day (about 1.5-2 hours per day)

Day 1. Read the first half of the how the machine works page. Focus on the path from a program to the processor. Draw it on paper as a list of steps: source file, interpreter, instructions, processor. Keep the drawing. You will compare it with the page again on day 6.

Day 2. Read the second half of the page, on memory and storage. Write one sentence in your notes that explains the difference between memory and storage in your own words. Then look at the Python lesson on what a program is and check that your drawing from day 1 still matches it.

Day 3. Learn binary counting. Write the numbers 0 to 8 in binary on paper: 0, 1, 10, 11, 100, 101, 110, 111, 1000. Each position is a power of two, and the rightmost position is 1. Check your work in Python with bin(5), which returns '0b101'. The 0b prefix only marks the number as binary.

Day 4. Write to_binary.py. It reads a whole number with input, converts it to a string of bits with a loop that repeatedly takes n % 2 and n // 2, and prints the result. Test it against bin for ten values, including 0. Use assert to make the check automatic. This is the same idea that makes the computer store numbers as bits.

Day 5. Read the floating point section of the official tutorial. Open the prompt and type 0.1 + 0.2. Note the result. The tutorial explains why the value is not exact. Write two sentences in your notes about the reason, in your own words. Then read the Unicode introduction, which says that Unicode gives every character a unique number.

Day 6. Return to your drawing from day 1 and check it against the machine page again. Correct any step that you now see is wrong. Then write a short answer to this question: why does one text file in Python hold the same characters on two different computers? Use the Unicode page to check your answer.

Day 7 is a rest day.

Deliverable

to_binary.py that converts a whole number to binary with a loop and checks its result against bin for at least ten values. Next to it, a text file week-07-notes.txt with your drawing from day 1 (as text or a photo), two sentences on floating point, and your answer to the Unicode question.

Check yourself

  1. What does the binary number 1011 equal in decimal?
  2. Why does bin(5) start with 0b?
  3. Why can 0.1 not be stored exactly as a binary floating point number?
  4. What does Unicode give each character?
  5. Why is it useful for a program to check results against a built-in function such as bin?
Answers
  1. 1011 is 8 plus 0 plus 2 plus 1, which is 11.
  2. The 0b is a prefix that marks the value as binary. It is not part of the number.
  3. In binary, 0.1 is a repeating fraction, the same way that one third is a repeating decimal. The stored value is the nearest binary fraction that fits in the available bits, so the result is close to 0.1 but not exact.
  4. A unique number. The number does not depend on the platform, the device, the program, or the language.
  5. A built-in function is already tested, so a difference between the two results usually points to a bug in your code. The check also makes it clear when the code is right, which helps you change it safely.

Sources