Lesson 5-3 covered what this loop prints.
total = 0 for n in range(1, 4): total = total + n print(total)
6. The accumulator pattern takes total through 0, 1, 3, 6, and the single print after the loop shows the final value.
If you expected 1 2 3, check the indentation. The print is outside the loop body, so it runs once rather than once per iteration.
Two symbols are enough
You can write software without knowing what is underneath, but engineers who do know debug faster and design better. Binary explains why file sizes come in strange numbers, why colors max out at 255, and why computers sometimes get decimal math slightly wrong. This unit builds that mental model.
You have been typing text and numbers, but the machine underneath stores none of that directly. Deep down, a computer's memory is billions of tiny switches, each either off or on. We write those two states as 0 and 1, and one such switch's value is called a bit, short for binary digit.
How can 0s and 1s represent the number 214, or the letter A, or a photo? The same way ordinary decimal numbers work. In decimal, each position is worth 10 times the one to its right, giving ones, tens, and hundreds. Binary does the identical thing with 2 instead of 10, so positions are worth 1, 2, 4, 8, 16, 32, 64, 128, and so on.
So binary 1011 means, reading right to left, 1×1 + 1×2 + 0×4 + 1×8 = 11.
A group of 8 bits is a byte, enough for numbers 0 to 255. Letters are just numbers by agreement, where the letter A is 65, colors are three numbers for red, green, and blue, and images are grids of colors. Everything is bits.
Reading binary in Python
Python understands binary directly when you prefix the digits with 0b.
print(0b101) print(0b1101)
Output
5 13
Check them by adding place values. 0b101 has place values 4, 2, 1 with 1s at 4 and 1, so 4 + 1 = 5. 0b1101 has place values 8, 4, 2, 1 with 1s at 8, 4, and 1, so 8 + 4 + 1 = 13.
The 0b prefix is a notation for you, not a different kind of number. print(0b101) outputs 5 because Python stores the value and forgets how it was written, which is why the output has no prefix.
Converting binary 1010
Binary 1010 is 10 in decimal.
Writing the place values under the bits gives 8, 4, 2, 1. The 1 bits sit over 8 and 2, so the value is 8 + 2 = 10.
You can confirm it with print(0b1010).
This one is a useful trap to have seen, because 1010 looks like a thousand and ten and is worth ten. Binary and decimal share the same ten digits for 0 and 1, so a binary number can always be misread as a decimal one unless the notation says otherwise, which is exactly why the 0b prefix exists.
The largest value in one byte
All eight bits on is the maximum a byte can hold.
print(0b11111111)Output
255Reading the pieces
- The literal is
0bfollowed by eight 1s, one for each bit position in a byte. - Summing the place values gives 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, the biggest value one byte can hold.
- That is why so many computer limits involve 255 or 256. A byte holds 256 distinct values counting zero, so the largest is 255, and colour channels, small counters, and countless file formats stop exactly there.
- The pattern generalizes. Turning on all bits of any width gives one less than the next power of two, which is where 65535 and 4294967295 come from.