Why Data Libraries: Lists to Arrays
Plain Python lists are flexible, but for numbers they are slow and verbose. Doubling every value in a list means writing a loop:
prices = [10, 20, 30]
doubled = []
for p in prices:
doubled.append(p * 2)
print(doubled) # [20, 40, 60]numpy (Numerical Python) gives you the ndarray: a grid of numbers stored in one tight block of memory. Math on a whole array happens in fast compiled C, with no Python loop. This is called vectorization.
import numpy as np
prices = np.array([10, 20, 30])
print(prices * 2) # [20 40 60] no loop needed
print(prices.sum()) # 60
print(prices.mean()) # 20.0The whole data and machine-learning stack sits on top of numpy, so this is the natural first step. We import it as np by convention.
Build an array with np.array(a_list), then call methods like .sum() and .mean() straight on it. They return single numbers (a Python/numpy scalar), not lists.
Import numpy as np. Turn the list [4, 8, 12, 16] into a numpy array called arr. Then compute total as its sum and avg as its mean. Print avg.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.