What is NumPy, why is it 50x faster than Python lists, and how does contiguous C-memory work?
NumPy (Numerical Python) is the foundational open-source library for scientific computing and numerical calculations in Python. It provides the high-performance 'ndarray' data structure and compiled C-level vector routines.
Think of a standard Python list as a binder filled with loose slips of paper where each number is located on a different page. A NumPy array is a single engraved metal ruler with all numbers stored tightly in consecutive memory slots.
Python lists store references (pointers) to individual heap-allocated integer objects with heavy metadata. NumPy allocates one contiguous block of raw binary bytes in RAM.
arr = np.array([1, 2, 3, 4, 5])import numpy
arr = array([1, 2, 3])import numpy as np
arr = np.array([1, 2, 3])NumPy functions must be prefixed with 'np.' when imported as 'np'.
Solve the objective below using NumPy vectorized syntax
Create a NumPy array named `arr` containing `[100, 200, 300]` and print it.
[100 200 300]