Learn Numpy - 10 Code Examples & CST Typing Practice Test
NumPy (Numerical Python) is an open-source Python library that provides high-performance, multi-dimensional arrays and a wide range of mathematical functions to operate on these arrays, forming the foundation of scientific computing in Python.
View all 10 Numpy code examples →
Learn NUMPY with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
NumPy Array Operations Example
import numpy as np
# Create arrays
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
# Array arithmetic
sum_arr = arr1 + arr2
print('Sum:', sum_arr)
# Statistical operations
print('Mean of arr1:', np.mean(arr1))
print('Standard deviation of arr2:', np.std(arr2))
# Multi-dimensional arrays
matrix = np.array([[1,2],[3,4]])
print('Matrix:
', matrix)
print('Transpose:
', matrix.T)
A minimal NumPy example demonstrating array creation, arithmetic, and basic statistics.
NumPy Array Indexing and Slicing
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print('Original array:', arr)
# Indexing
print('Element at index 2:', arr[2])
# Slicing
print('Elements from index 1 to 3:', arr[1:4])
print('Every second element:', arr[::2])
Demonstrates indexing and slicing of NumPy arrays.
NumPy Random Number Generation
import numpy as np
# Random integers
rand_ints = np.random.randint(0, 10, size=5)
print('Random integers:', rand_ints)
# Random floats
rand_floats = np.random.rand(3,3)
print('Random floats 3x3:\n', rand_floats)
Generating random numbers and arrays using NumPy.
NumPy Array Reshaping
import numpy as np
arr = np.arange(12)
print('Original array:', arr)
matrix = arr.reshape((3,4))
print('Reshaped 3x4 matrix:\n', matrix)
flattened = matrix.flatten()
print('Flattened array:', flattened)
Changing the shape of arrays.
NumPy Mathematical Functions
import numpy as np
arr = np.array([1,4,9,16])
print('Square root:', np.sqrt(arr))
print('Exponential:', np.exp(arr))
print('Natural log:', np.log(arr))
Applying mathematical functions on arrays.
NumPy Boolean Indexing
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
print('Original array:', arr)
mask = arr > 20
print('Boolean mask:', mask)
print('Filtered array:', arr[mask])
Filtering arrays using boolean conditions.
NumPy Stacking Arrays
import numpy as np
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
vstacked = np.vstack((arr1, arr2))
hstacked = np.hstack((arr1, arr2))
print('Vertical stack:\n', vstacked)
print('Horizontal stack:', hstacked)
Combining arrays using vertical and horizontal stacking.
NumPy Linear Algebra Operations
import numpy as np
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
# Matrix multiplication
C = np.dot(A, B)
print('Matrix multiplication:\n', C)
# Determinant
print('Determinant of A:', np.linalg.det(A))
# Inverse
print('Inverse of A:\n', np.linalg.inv(A))
Matrix multiplication, determinant, and inverse using NumPy.
NumPy Statistical Analysis
import numpy as np
arr = np.array([1,2,3,4,5])
arr2 = np.array([5,4,3,2,1])
print('Mean:', np.mean(arr))
print('Median:', np.median(arr))
print('Std deviation:', np.std(arr))
print('Correlation:', np.corrcoef(arr, arr2))
Compute mean, median, standard deviation, and correlation.
NumPy Broadcasting Example
import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
scalar = 10
# Add scalar to array
print('Add scalar:\n', arr + scalar)
# Subtract row vector
row_vec = np.array([1,0,1])
print('Subtract row vector:\n', arr - row_vec)
Demonstrates broadcasting operations between arrays of different shapes.
Frequently Asked Questions about Numpy
What is Numpy?
NumPy (Numerical Python) is an open-source Python library that provides high-performance, multi-dimensional arrays and a wide range of mathematical functions to operate on these arrays, forming the foundation of scientific computing in Python.
What are the primary use cases for Numpy?
Numerical computations with large datasets. Matrix operations, linear algebra, and array manipulations. Data preprocessing for machine learning and AI. Scientific simulations and mathematical modeling. Integration with other Python libraries for analytics and visualization
What are the strengths of Numpy?
Highly optimized and fast for numerical computations. Foundation for most Python scientific libraries. Extensive community support and documentation. Supports large datasets efficiently. Flexible array operations with broadcasting and vectorization
What are the limitations of Numpy?
Not a machine learning library by itself. Limited built-in plotting and visualization. Pure Python loops over arrays are slow; vectorization is required. Single-core by default (needs libraries like NumExpr for multi-core). No native support for GPU acceleration
How can I practice Numpy typing speed?
CodeSpeedTest offers 10+ real Numpy code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.