import numpy as np
Introduction to NumPy ArraysΒΆ
NumPy arrays differ from Python lists in important ways. While Python lists can hold mixed types (strings, integers, booleans, other lists), NumPy arrays are homogeneous β every element shares the same data type. This constraint enables NumPy to store data in contiguous memory blocks and apply operations in bulk via optimized C routines. The result is dramatically faster computation: operations that take seconds with Python lists can complete in milliseconds with NumPy arrays. Below, we first review Python lists, then convert one into a NumPy array to see the difference.
my_list = [0,1,2,3,4,5,6,7,8,9]
print(my_list)
print(my_list[0])
my_list2 = ["John Elder", 41, my_list, True]
print(my_list2)
# Create a numpy array
np1 = np.array([0,1,2,3,4,5,6,7,8,9])
print(np1)