How to package Python code with uv, in 4 images. 🐍
Name a command, run uv build, share the wheel.
#Python#Coding
ALT Slide reading 'Package your Python code with uv'. The next slides show the four commands, the function greet calls with its output, and the takeaway 'A package is a folder + a toml'.
A pytest test is just an assert, in 4 images. 🐍
#Python#Coding
ALT Slide reading 'A test is just an assert'. The next slides show the test code, its failure output assert 5 == 6, and the naming rules, and end with: A test is an assert.
Fetch JSON from the web with urlopen and json.load, in 4 images. 🐍
#Python#Coding
ALT Slide reading 'Fetch data from the web, no install'. The next slides show the urlopen code, its output delectus aut autem, and end with: urlopen + json.load.
How to call a web API in Python, in 4 images. 🐍
urlopen the URL, json.load the response: you get a dictionary.
#Python#Coding
ALT Slide reading 'Call a web API in Python'. The next slides show code that opens a URL and loads the JSON, a terminal printing 'delectus aut autem' and False, and the takeaway 'URL in, dictionary out'.
Walk a tree left, node, right and get sorted values, in 4 images. 🐍
#Python#Coding
ALT Slide reading 'A tree that hands you sorted values'. The next slides show the inorder function, its output [1, 2, 3], and end with: Left, node, right = sorted.
How an in-order walk visits a binary tree, in 4 images. 🐍
Walk left, print the node, walk right: the values come out sorted.
#Python#Coding
ALT Slide reading 'Walk a tree left, node, right'. The next slides show a recursive inorder function, a terminal printing 1, 2, 3, 4, 6, and the takeaway 'Left, node, right = sorted order'.
Top 3 numbers without sorting the whole list, in 4 images. 🐍
heapq.nlargest keeps a small heap of the best few.
#Python#Coding
ALT Slide reading 'Top 3 numbers without sorting it all'. The next slides show heapq.nlargest and nsmallest with their output, what to remember, and end with: Top k? Use heapq.
Python 3.15 frozendict, in 4 images. 🐍
A built-in dict that can't change, so it can be a dict key.
#Python#Coding
ALT Slide reading 'Python 3.15 has an immutable dict'. The next slides show a frozendict used as a dict key with its output, what to remember about hashing, and end with: Need a dict as a key? frozendict.
How a Python dict finds a key, in 4 images. 🐍
Hash the key, jump to the slot: lookup is O(1). Two keys in one slot is a collision.
#Python#Coding
ALT Slide reading 'How a dict finds a key instantly'. The next slides show a small slot function, three words with their slot numbers where cat and act both get slot 0, and the takeaway 'Hash the key. Jump to the slot.
Sorting by length in Python, in 4 images. 🐍
sorted(words, key=len) sorts by any rule you give it, and ties keep their order.
#Python#Coding
ALT Slide reading 'Sort words by length in one line'. The next slides show sorted with key equals len and its output, what key does, and end with: key= picks the order.
Sort words by length in Python. 🐍
sorted(["pear", "fig", "kiwi", "apple"], key=len)
→ ["fig", "pear", "kiwi", "apple"]
Ties keep their order.
#Python#Coding
Python bisect, in 4 images. 🐍
Find where a value belongs in a sorted list, and keep it sorted with insort.
#Python#Coding
ALT Slide reading 'Binary search is built in to Python'. The next slides show insort and bisect_left with their output, what to remember about speed and sorting, and end with: Sorted list? Use bisect.
Python has binary search built in. 🐍
bisect.insort(nums, 6) keeps a sorted list sorted.
bisect.bisect_left(nums, 8) gives 3: the position where 8 belongs.
#Python#Coding
Python binary search, in 4 images. 🐍
Check the middle of a sorted list and drop half. A million items take about 20 steps.
#Python#Coding
ALT Slide reading 'Binary search in Python in 3 minutes'. The next slides show a find function with its middle check, how low and high move past the middle, and the takeaway 'Sorted list. Halve. Repeat.'
A function with no base case, in 4 images. 🐍
Python stops it at about 1000 calls with a RecursionError.
#Python#Coding
ALT Slide reading 'What if a function calls itself and never stops?' The next slides show the code with its RecursionError message, why Python stops it, and end with: no base case, no stopping.
A function that calls itself forever?
def dive(n):
return dive(n + 1)
Python stops it at about 1000 calls:
RecursionError: maximum recursion depth exceeded
The fix: a base case. 🐍
#Python#Coding
Python recursion, in 4 images. 🐍
A function calls itself on a smaller problem. The base case stops it.
#Python#Coding
ALT Slide reading 'Recursion in Python in 3 minutes'. The next slides show a factorial function with its base case, how calls pile up and unwind to give 24, and the takeaway 'Smaller problem. Base case. Done.'
List to text, then back:
text = json.dumps(todos)
json.loads(text) == todos # True
Note that False becomes false in the text. And a tuple comes back as a list. 🐍
#Python#Coding
Getting the third item of a linked list, in 4 images. 🐍
No index: follow the links, one hop at a time.
#Python#Coding
ALT Slide reading 'No index. How do you get the third item?' The next slides show the code with its output c, what each hop costs, and end with: no index, walk the links.
A linked list has no index.
The third item is two hops:
head = Node("a", Node("b", Node("c")))
print(head.next.next.value) # c
The hundredth is 99 hops. O(n). 🐍
#Python#Coding