Why loops first, and the length pitfall
Every operation here is a Python-level loop that pays a small cost for each element. On three numbers you'll never notice; on three million the cost is real, and the code grows tangled as operations pile up.
That ache is the whole point. It's exactly what motivates NumPy, which does the identical arithmetic in fast compiled code with a single expression. You'll appreciate that leap far more for having done it the long way first — which is why this part of the course is deliberately loop-by-hand before any library.
One trap to watch with zip: if the two lists have different lengths, zip silently stops at the shorter one. No error is raised, so a length mismatch can quietly drop data and hide a bug.
list(zip([1, 2, 3], [10, 20])) # [(1, 10), (2, 20)] -- the 3 is dropped
When you intend two vectors to be the same length, it's worth checking that they are, rather than trusting zip to complain — because it won't.
product” and leads into “Matrices: indexing, row and column sums”.*
Related cards
Tasks
Card Info
- Topic: Python for Data Science
- Difficulty: Beginner
- Completed: 0 users