Everything you did by hand this unit, Python does in one line. Today you find out how you feel about that.
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.
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.
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.
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:
| Python | What it does | Your hand version |
|---|---|---|
| bin(37) | decimal → binary | Lesson 1.2, subtract-the-largest-power |
| int('1010', 2) | binary → decimal | Lesson 1.2, add the column values |
| ord('A') | character → number | Lesson 1.4, looking it up in the table |
| chr(65) | number → character | Lesson 1.4, reading the table backwards |
| hex(255) | decimal → hexadecimal | Lesson 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.
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."
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.
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.
Pick a function, type an input, and see exactly what Python would print — labels and all.
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.
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.
bin() shows you the bits; 2 ** 8 counts what a byte holds.int('100', 2) versus int('100') — the base is the argument.0.1 + 0.2 == 0.3 returns False. There it is, on your screen.ord() and chr() are the lookup table.hex() writes them the web's way.Five questions in AP format. Pick an answer to see feedback immediately.
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.
print(), function calls, arguments, and
quotes around text will all be here waiting. You have already used them; Unit 2 just gives
them names.