Why loops first, and the length pitfall

Beginner Python for Data Science
Created by Best · 24.06.2026 at 14:03 UTC

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”.*

University approvals: 0
Related cards
Builds on Lists as vectors: scaling and the dot product · Python for Data Science
Next Matrices: indexing, row and column sums · Python for Data Science
Tasks
Question 1

Why do we teach vector math with plain Python loops BEFORE introducing NumPy?

Question 2

If you zip two lists of different lengths, what happens?

Card Info
  • Topic: Python for Data Science
  • Difficulty: Beginner
  • Completed: 0 users
Creator
Best
Best
BestBuddy