Loading content...
Loading content...
Master high-performance numerical data structures in Python: creating ndarrays, understanding Python List vs NumPy Array behavior, 0-based indexing, array attributes (ndim, shape, size, dtype), 1D vs 2D arrays, and vectorized operations.
In modern data science, standard Python lists are general-purpose containers. They can hold mixed types (integers, strings, objects), but they are not designed for mathematical and numerical operations on large datasets.
A flexible sequence of pointers to arbitrary Python objects. Great for general programming, but clumsy for math across thousands of records.
A contiguous block of memory storing homogeneous numbers. Enables instant element-wise math, multidimensional slicing, and powers Pandas.
ndarray (N-dimensional array), which represents a grid of values of the same type.To use NumPy, import it using the conventional alias np, then pass an array-like sequence (such as a list) into np.array():
[10 20 30 40], without the commas found in standard Python lists [10, 20, 30, 40].What happens when you multiply a Python list by 2 versus a NumPy array?
py_list = [10, 20, 30] print(py_list * 2) # Output: [10, 20, 30, 10, 20, 30] # Duplicates the sequence!
np_arr = np.array([10, 20, 30]) print(np_arr * 2) # Output: [20 40 60] # Multiplies every single number!
Import NumPy and initialize a 1D numerical array:
import numpy as np, create arr = np.array([10, 20, 30, 40, 50]), and print it with print(arr).NumPy arrays use zero-based indexing, exactly like Python lists:
Every NumPy array has built-in attributes that describe its geometry and storage:
| Attribute | Meaning | Example for 1D [10, 20, 30, 40] | Example for 2D [[10, 20, 30], [40, 50, 60]] |
|---|---|---|---|
| arr.ndim | Number of dimensions (axes) | 1 | 2 |
| arr.shape | Tuple of lengths along each axis | (4,) | (2, 3) (2 rows, 3 columns) |
| arr.size | Total count of elements | 4 | 6 |
| arr.dtype | Data type of elements | int64 | int64 |
NumPy represents both 1D sequences and 2D tables using the same unified class:
np.array([10, 20, 30, 40]) # 1 axis: shape is (4,) # Represents 1 feature or series
np.array([
[10, 20, 30],
[40, 50, 60]
])
# 2 axes: shape is (2, 3) -> 2 rows, 3 colsLook at the 2D array below. Before running the code, predict its properties:
Unlike Python lists, which can mix numbers, strings, and objects, a NumPy array has a single, uniform data type:
In data analytics, you often transform entire columns (such as applying tax, converting currencies, or testing performance thresholds) without writing manual loops:
Perform fundamental analytical queries and transformations on a numerical sales dataset:
| Feature | Python List | NumPy ndarray |
|---|---|---|
| Primary Design | General-purpose container | Numerical and scientific computing |
| Data Types | Heterogeneous (can mix types) | Homogeneous (single dtype) |
| Math Operators (*, +) | Sequence repetition & concatenation | Vectorized element-wise mathematics |
| Dimensionality | 1D (lists of lists for 2D) | Native N-dimensional (1D, 2D, 3D, ND) |
| In Data Analytics | Config options, mixed row objects | Numeric matrices, feature columns, Pandas backend |
| Mistake | Incorrect Code | Correct Code | Why It Fails |
|---|---|---|---|
Forgetting import numpy as np | arr = np.array([1, 2]) | import numpy as np first | Python raises NameError: name 'np' is not defined. |
| Passing separate numbers instead of a list | np.array(1, 2, 3) | np.array([1, 2, 3]) | np.array() takes a single sequence as its first argument. |
Assuming arr * 2 duplicates the array | Expecting [1, 2, 1, 2] | Produces [2, 4] | NumPy arithmetic is element-wise, not structural repetition. |
Calling arr.shape() as a function | print(arr.shape()) | print(arr.shape) | shape is an attribute tuple, not a callable method. |
Test your conceptual understanding of NumPy arrays, dimensionality, vectorization, and attributes:
Test your understanding of ndarray creation, list vs array behavioral differences, ndim/shape/size/dtype properties, and vectorized operations.
1. What is the fundamental behavior difference between "py_list * 2" and "np_arr * 2"?