Python NumPy Tutorial for Beginners
4 min readMay 14, 2024
NumPy is a fundamental package for scientific computing in Python. It provides support for arrays, matrices, and mathematical functions to operate on these arrays efficiently.
Here’s an overview to get you started:
1. Installation
If you haven’t installed NumPy yet, you can do so via pip:
pip install numpy
2. Importing
After installation, you can import NumPy in your Python script or interpreter:
import numpy as np
3. NumPy Arrays
NumPy’s primary object is the homogeneous multidimensional array, numpy.ndarray
. You can create NumPy arrays using lists or other array-like objects.
Creating Arrays:
# From a list
arr1 = np.array([1, 2, 3, 4, 5])
# Multidimensional array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
# Zeros array
zeros_array = np.zeros((3, 4)) # Creates a 3x4 array of zeros
# Ones array
ones_array = np.ones((2, 3)) # Creates a 2x3 array of ones
# Array with a range of values
range_array = np.arange(0, 10, 2) # Creates an array from 0 to 10 (exclusive) with a step of 2
# Random array
random_array = np.random.rand(3, 3) # Creates a 3x3 array of random values between 0 and 1