NumPy (Numerical Python) is a powerful Python library used for numerical and scientific computing. It provides efficient n-dimensional arrays, mathematical functions, and tools for working with large datasets, making it much faster and more convenient than plain Python lists for numeric operations.
mean(), sum(), max(), min() that summarize data.To use NumPy, you first install the library and then import it in your Python code. The common convention is to import NumPy as np. Arrays can be created from Python lists, and once you have an array, you can perform mathematical operations directly on it.
NumPy arrays:
# Install NumPy using pip
pip install numpy
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr) # Output: [1 2 3 4]
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2) # Output: [5 7 9]
print(arr1 * 2) # Output: [2 4 6]
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr)) # Output: 3.0
print(np.sum(arr)) # Output: 15
print(np.max(arr)) # Output: 5
print(np.min(arr)) # Output: 1
np.array([1, 2, 3, 4]) creates a 1D NumPy array.arr1 + arr2 adds corresponding elements: [1, 2, 3] + [4, 5, 6] → [5, 7, 9].arr1 * 2 multiplies every element by 2, giving [2, 4, 6].np.mean(arr) computes the average value (here, 3.0).np.sum(arr) adds all elements (here, 15).np.max(arr) and np.min(arr) return the largest and smallest values.for loops whenever possible.mean(), sum(), max(), and min() for quick summaries.np.array() with a specified dtype if you need strict control over data types.np.array() and perform addition and multiplication on it.mean, sum, max, and min for a 2D array.