"Learn how to set up a virtual environment in VS Code and get started with NumPy arrays, operations, reshaping, statistics, and more."
Abhishek Kumar
NumPy (Numerical Python) is one of the most powerful Python libraries for numerical computing. It provides support for large, multi-dimensional arrays and matrices, along with a wide collection of mathematical functions to operate on them efficiently.
In this article, we’ll walk through everything you need to get started — from setting up a virtual environment in VS Code to writing NumPy code that covers the essentials.
Before working with NumPy, it’s best practice to create a virtual environment. This keeps your project dependencies isolated.
Run the following command inside your project folder:
# For Windows
python -m venv venv
# For macOS/Linux
python3 -m venv venv
This creates a folder named venv that stores your isolated environment.
# Windows (Command Prompt)
venv\Scripts\activate
# Windows (PowerShell)
venv\Scripts\Activate.ps1
# macOS/Linux
source venv/bin/activate
You should now see (venv) before your terminal prompt.
With the virtual environment active, install NumPy:
pip install numpy
Confirm installation:
pip show numpy
Let’s begin using NumPy in Python. Create a new file called numpy_basics.ipynb and add the following in the code section.
Note: To Run the code, use Shift + Enter and select the running virtual environment.
import numpy as np
# Create a simple array
arr = np.array([1, 2, 3, 4, 5])
print("Array:", arr)
print("Type:", type(arr))
print("Shape:", arr.shape)
Output:
Array: [1 2 3 4 5] Type: <class 'numpy.ndarray'> Shape: (5,)
NumPy provides several ways to create arrays:
# From a Python list
arr1 = np.array([10, 20, 30])
# Zeros array
arr2 = np.zeros((3, 3))
# Ones array
arr3 = np.ones((2, 4))
# Range of numbers
arr4 = np.arange(0, 10, 2)
# Evenly spaced numbers
arr5 = np.linspace(0, 1, 5)
# Random values
arr6 = np.random.rand(2, 3)
arr7 = np.random.randint(1, 100, (3, 3))
print("arr1:", arr1)
print("arr2:\n", arr2)
print("arr3:\n", arr3)
print("arr4:", arr4)
print("arr5:", arr5)
print("arr6:\n", arr6)
print("arr7:\n", arr7)
NumPy supports vectorized operations (faster than Python loops):
a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
print("a + b:", a + b)
print("a - b:", a - b)
print("a * b:", a * b)
print("a / b:", a / b)
print("Square root of a:", np.sqrt(a))
print("Exponential of a:", np.exp(a))
arr = np.array([10, 20, 30, 40, 50])
# Indexing
print("First element:", arr[0])
print("Last element:", arr[-1])
# Slicing
print("First three:", arr[:3])
print("From index 2 onward:", arr[2:])
For 2D arrays:
mat = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Element at row 1, col 2:", mat[0, 1])
print("First row:", mat[0, :])
print("Second column:", mat[:, 1])
arr = np.arange(1, 13)
print("Original:", arr)
reshaped = arr.reshape(3, 4)
print("Reshaped to 3x4:\n", reshaped)
flattened = reshaped.flatten()
print("Flattened:", flattened)
arr = np.array([1, 2, 3, 4, 5])
print("Sum:", np.sum(arr))
print("Mean:", np.mean(arr))
print("Median:", np.median(arr))
print("Standard Deviation:", np.std(arr))
print("Max:", np.max(arr))
print("Min:", np.min(arr))
NumPy allows operations between arrays of different shapes when compatible.
mat = np.array([[1, 2, 3],
[4, 5, 6]])
vec = np.array([10, 20, 30])
print("Matrix:\n", mat)
print("Vector:", vec)
print("Broadcasted addition:\n", mat + vec)
arr = np.arange(10)
# Save to file
np.save("my_array.npy", arr)
# Load from file
loaded = np.load("my_array.npy")
print("Loaded array:", loaded)
# Save multiple arrays
np.savez("data.npz", a=arr, b=arr*2)
# Load multiple arrays
data = np.load("data.npz")
print("a:", data["a"])
print("b:", data["b"])
Once you’re comfortable with NumPy basics, you can explore:
NumPy is the foundation of the Python data science ecosystem. By setting up your environment, learning how to create arrays, perform mathematical operations, and manipulate data, you’ve taken the first big step toward numerical computing in Python.