Data representation

Bits, binary integers, and why "10" is not 10.

Computers store bits. Python hides most of that, but "10" (text) is not 10 (an integer), and bin(10) shows the bits of ten.

Goal

Print binary for small integers, and show that string "10" plus 1 is a TypeError unless you convert.

Integers have bits

for n in range(0, 9):
    print(n, bin(n), n.bit_length())

bin(5) is '0b101'. bit_length() is how many bits you need for that integer (zero is a special case: 0.bit_length() is 0).

Text is not a number

text = "10"
number = 10
print(text, type(text))
print(number, type(number))
print(int(text) + 1)
print(str(number) + " kiosks")

Characters

print(ord("A"), chr(65))
print(ord("N"))
for ch in "Nairobi"[:3]:
    print(ch, ord(ch))

A string is a sequence of code points. The city name "Nairobi" is not stored as one magic atom — it is characters in order.

Pitfall

int("10.5") fails. Use float("10.5") for a decimal written as text. Mixing types is a representation bug, not a math bug.