Why Use Numpy Arrays?

Why Numpy Arrays?

Numpy is designed to work with arrays.

Numpy array operations are fast.

Differences Between Lists and Numpy Arrays

>>> import numpy as np # ---- You can not create a np.array with individual values >>> arr = np.array(1,2,3,4,5) TypeError: array() takes from 1 to 2 positional arguments # ---- you can create a np.array with a list >>> narr = np.array([1,2,3,4,5]) >>> narr array([1, 2, 3, 4, 5]) # ---- If you multiply a np.array by 2 # ---- each element is multiplied by 2 # ---- (Note: You can also do this with map.) >>> narr * 2 array([ 2, 4, 6, 8, 10]) # ---- you can convert a np.array to a list >>> list(narr) [1, 2, 3, 4, 5] # ---- you can create a list >>> lst = [1,2,3,4,5] # ---- you can multiply a list by 2 # ---- (you get the list twice) >>> lst * 2 [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] # ---- you can create a 3D numpy array # ---- (Think of it as a list within a list within a list.) >>> x = np.array([[[1, 2, 3],[4, 5, 6],[10,11,12]]]) >>> x[0][1][2] 6 >>> x[0][0][0] 1 >>> x[0][0][2] 3 >>> x[0][1][0] 4 x[1][1][1] IndexError: index 1 is out of bounds for axis 0 with size 1