Unit 1 · Digital Information · Lesson 1.10 · Guided lab

Bits in Python

Everything you did by hand this unit, Python does in one line. Today you find out how you feel about that.

Big Idea 2 — DataNot assessed — participation

Before this lesson — read on Runestone

Your first look at Python. Don't worry about understanding all of it — Unit 2 teaches it properly. Enrolled in our Runestone course? Open these from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Standing at the edge of Bits, looking into Programs. Nine lessons of hand work are behind you. This one is a bridge: no new concepts, just watching a machine do everything you learned, so that Unit 2 opens on something that already looks familiar.

2

The Hook

bin(37) Two weeks ago that would have taken you a minute: find the largest power of 2 that fits, subtract, repeat, check by adding back.

Python does it instantly, and so does every computer you will ever touch. So here is the honest question this lesson is actually about, and you should hold it the entire period: if the machine can do it in one line, why did you spend two weeks learning to do it by hand? There is a real answer. See if you arrive at it before the end of the page.

3

The Idea

You have not learned to program yet. That starts in Unit 2. Today you only need two skills: run a line, and — before you run it — guess what it will print. The guessing is the actual assignment. Reading output you already predicted teaches you something; watching output scroll past teaches you nothing.

Here are the five functions that replace this entire unit:

PythonWhat it doesYour hand version
bin(37)decimal → binaryLesson 1.2, subtract-the-largest-power
int('1010', 2)binary → decimalLesson 1.2, add the column values
ord('A')character → numberLesson 1.4, looking it up in the table
chr(65)number → characterLesson 1.4, reading the table backwards
hex(255)decimal → hexadecimalLesson 1.5, chopping bits into fours

Two details will trip you up, so meet them now rather than in the notebook.

The labels. bin(10) does not print 1010. It prints 0b1010. The 0b is Python's label meaning "what follows is binary," the way a $ labels dollars. hex() does the same with 0x. To see only the digits, cut off the first two characters with [2:].

print(bin(10))       → 0b1010
print(bin(10)[2:])  → 1010     # just the digits

The base argument. int() takes the digits and the base they're written in, and forgetting the base is the classic mistake:

print(int('100', 2))   → 4      # read as binary
print(int('100'))      → 100    # read as ordinary decimal

Identical digits, different answers, because the base is not written in the number — exactly the point the joke in 1.2 was making.

One thing that will look wrong. Python's hex(255) gives '0xff' in lowercase, but in 1.5 you wrote web colours as #FF8000 in uppercase. Both are correct and both mean the same value — case carries no meaning in hexadecimal. Python picked one convention, CSS authors mostly picked the other. Notice how often "which is right?" turns out to be "which did somebody agree on."
4

Try It

The notebook is the lesson — this page is the warm-up

Everything below gets you ready to move quickly through the real thing. The notebook has forty cells, runs in your browser, and asks you to predict before every single one.

  1. Open the notebook and work through it top to bottom.
  2. Type your guess as a comment before running each cell.
  3. Download it before you close the tab — File → Download. Your work is not saved to any server.
⬇ Download the notebook (.ipynb)

Predict before you run

Type what you think each line prints, then check. Work them out the hand way — that's the whole exercise. Quotes are optional; 1010 and '1010' both count.

0 of 10 checked

Try any value you like

Pick a function, type an input, and see exactly what Python would print — labels and all.

This checker knows only the five functions from this lesson. For anything else, run the real Python in the notebook.

Three things to try in the notebook

1. Run bin(255), then bin(256). Count the digits in each. Explain the jump using the rule from 1.1.

2. Run print(0.1 + 0.2). Then run print(0.1 + 0.2 == 0.3). You predicted True. Explain the real answer using the words from 1.3.

3. Put your own initials in the ord() cell. Then change one to lowercase and predict the new number before running it. If you can do that in your head, 1.4 landed.

5

Vocabulary

No new terms today. Instead, here is every word from this unit that you just watched a machine act out. If you can point at the line of Python that does each one, the unit has landed.

bit · byte · binary number system 1.1
bin() shows you the bits; 2 ** 8 counts what a byte holds.
place value · base-2 · base-10 1.2
int('100', 2) versus int('100') — the base is the argument.
round-off error 1.3
0.1 + 0.2 == 0.3 returns False. There it is, on your screen.
character encoding · ASCII 1.4
ord() and chr() are the lookup table.
RGB · hexadecimal 1.5
A colour is a tuple of three numbers; hex() writes them the web's way.
6

Check

Five questions in AP format. Pick an answer to see feedback immediately.

0 of 5 answered
7

Impact Check

So was the hand work wasted?

You spent two weeks converting binary, looking up ASCII, and counting powers of 2. This morning you replaced all of it with five function calls. It is fair to ask whether the two weeks were a waste of your time.

Here is the case that they were: the machine is faster, never miscounts, and is available every time you will ever need it. Nobody converts binary by hand professionally. Time spent on a task a computer does perfectly is time not spent on something a computer can't do.

And here is the case that they weren't — which is really one observation. In 1.3 a view counter broke and the fix required knowing that a number has a fixed width. In 1.8 you refuted an impossible product claim by counting patterns. In 1.6 you could say why the audio sounded wrong instead of only that it did. None of those are conversions. They are judgements about what the machine is doing, and you cannot make them by calling a function you don't understand.

This is not a settled question and it is going to follow you. As tools get more capable, the line between "worth learning" and "let the machine do it" keeps moving, and every generation argues about where it should sit. You will meet the loudest version of that argument in Unit 8. Notice that you now have first-hand evidence to bring to it — which is more than most people arguing about it have.

8

Connections

Came from all of Unit 1 — every function on this page automates a specific lesson. That is why the table in section 3 has a "your hand version" column.
Next in 1.11–1.12 — Mini-Project 1. You design your own encoding scheme, and the writing is the assessment. Fast finishers can implement it in this notebook.
Opens Unit 2 — print(), function calls, arguments, and quotes around text will all be here waiting. You have already used them; Unit 2 just gives them names.
Returns in Unit 8 — "should a person still learn to do what the machine does?" is the question of the decade, and today you got a small honest look at it.
← 1.9 Compression II: Lossy