Reading Python
Read short Python: lists, dicts, functions, loops, comprehensions, f-strings, imports.
What the animation says
Blocks and lists
Later lessons show ideas as short Python snippets. If you can read them, you can run every claim yourself. Python has no braces. Indentation marks a block: lines pushed in under def, for or if belong to it. A list holds items in order, counted from zero. So a, index zero, is ten. Minus one counts from the end: a, index minus one, is thirty, the last item. A slice takes a range. Here is a, from zero to two. What comes out?
Dicts, = and ==
Ten and twenty. A slice stops before its end, and range of three runs zero, one, two, the same way. A dict looks up values by key. Here, the key cat gives three. Storing under a new key, like dog, adds it. Storing under cat again replaces its value. One equals sign stores a value under a name. Two equals signs only test, and give True or False. Plus-equals adds to the value already there, and double star means power.
Comprehensions
This loop builds a new list: for each x, if x is above zero, it appends x squared. A comprehension packs that whole loop into one line. Read it in this order: the for part, then the if test, then the expression at the front, which is what gets collected. Curly braces with a key, a colon and a value build a dict the same way. Now xs is three, minus one, four. What ends up in sq?
Trace it by hand
Nine and sixteen. Let's trace it by hand: x is three, which passes the test, so keep nine. x is minus one: the test fails, so skip it. x is four: keep sixteen. Now sq at minus one is the last item, sixteen. And its length is two. An f-string is text in which each pair of braces is replaced by a value. Here, sq at one divided by three is five point three three three. Colon, point one, f shows one decimal: five point three.
Counting words
Now a real snippet that counts words. Split cuts the text into words wherever there is whitespace. The get call returns a word's stored count, or zero if the word isn't there yet. This line adds one and stores the result back under that word. Watch the dict fill up: the, cat, saw, each with a count of one. Next comes the second the. Tap the entry it changes.
What it prints
The second the raised its count to two. Then dog arrives with one. Print shows the dict on screen, with single quotes around the text. Looping over a dict gives its keys, in the order they were added. This comprehension keeps the words seen once: three of them. Import math brings in the math module. The square root of the count for the, which is two, is one point four one four. To read any snippet, go top to bottom and keep track of each name's current value.
Containers, blocks and loops
Compact forms you'll see everywhere
Trace a snippet by hand
A real snippet: counting words
import math
def count_words(text):
counts = {}
for w in text.split():
counts[w] = counts.get(w, 0) + 1
return counts
c = count_words("the cat saw the dog")
print(c)
once = [w for w in c if c[w] == 1]
print(f"{len(once)} appear once; sqrt={math.sqrt(c['the']):.3f}")
What to remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses