Why NumPy?
NumPy is a fundamental library in Python for numerical computations. It introduces a powerful new data structure — the NumPy array— that is faster and leaner than a plain Python list. Let's see exactly why.
1Two ways to store numbers
A Python list is wonderfully flexible — it can hold different data types at once (an integer, a string, a float, a boolean). But that flexibility has a cost: doing maths on every element needs a loop, which gets slow on large datasets.
# A list can mix types...
mixed_list = [10, "hello", 3.14, True]
print(type(mixed_list[0])) # <class 'int'>
print(type(mixed_list[1])) # <class 'str'>
# ...but maths needs an explicit loop
numbers = [1, 2, 3, 4, 5]
squared = []
for n in numbers:
squared.append(n ** 2)
print(squared) # [1, 4, 9, 16, 25]A NumPy array is homogeneous — every element is the same type. That uniformity lets NumPy store data compactly and run vectorized operations: maths on the whole array at once, with no Python loop. When you print one, it still sits inside square brackets [ ] just like a list — but with a tell-tale difference: the values are separated by spaces, not commas (e.g. [1 2 3 4] instead of [1, 2, 3, 4]).
2See the difference
Press each button and watch how the work happens — one at a time for the list, all at once for the array.
Squares each item with a for loop — one at a time.
arr ** 2 — every item at once (vectorized).
3Recap
Why is a NumPy array faster than a list for maths?