NumPy - Numerical Python

NumPy (Numerical Python) is the foundational package for numerical computing in Python. It provides support for multi-dimensional arrays, along with a large collection of mathematical functions to operate on these arrays efficiently.

Key Features

Basic NumPy Example

import numpy as np

# Create a 1D array
arr = np.array([1, 2, 3, 4])
print("1D Array:", arr)

# Create a 2D array
mat = np.array([[1, 2], [3, 4]])
print("2D Array:\n", mat)

# Element-wise operations
print("Squared:", arr ** 2)

10 Examples

import numpy as np

# Example 1: Array creation
arr = np.array([1, 2, 3])
print(arr)

# Example 2: Zeros and ones
print(np.zeros((2, 3)))
print(np.ones((2, 3)))

# Example 3: Array range and reshape
print(np.arange(10).reshape(2, 5))

# Example 4: Slicing and indexing
print(arr[1])
print(arr[1:])

# Example 5: Broadcasting
print(arr + 5)

# Example 6: Element-wise multiplication
print(arr * arr)

# Example 7: Matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[2, 0], [1, 2]])
print(np.dot(A, B))

# Example 8: Statistical operations
print(np.mean(arr), np.std(arr))

# Example 9: Boolean indexing
print(arr[arr > 1])

# Example 10: Random numbers
print(np.random.rand(2, 2))