Abhishek Kumar

Getting Started with NumPy in Python

"Learn how to set up a virtual environment in VS Code and get started with NumPy arrays, operations, reshaping, statistics, and more."

Abhishek Kumar

Table of contents

    Getting Started with NumPy in Python: A Complete Guide

    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.


    1. Setting Up a Virtual Environment in VS Code

    Before working with NumPy, it’s best practice to create a virtual environment. This keeps your project dependencies isolated.

    Step 1: Open Terminal in VS Code

    • Open VS Code
    • Press Ctrl + ` (backtick) or go to View → Terminal

    Step 2: Create a Virtual Environment

    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.

    Step 3: Activate the 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.

    Step 4: Install NumPy

    With the virtual environment active, install NumPy:

    pip install numpy
    

    Confirm installation:

    pip show numpy
    

    2. First Steps with 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,)
    

    3. Creating Arrays

    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)
    

    4. Array Operations

    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))
    

    5. Indexing and Slicing

    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])
    

    6. Reshaping and Flattening

    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)
    

    7. Useful Functions

    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))
    

    8. Broadcasting

    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)
    

    9. Saving and Loading Arrays

    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"])
    

    10. Next Steps

    Once you’re comfortable with NumPy basics, you can explore:

    • Pandas for data analysis
    • Matplotlib / Seaborn for visualization
    • SciPy for advanced scientific computing

    Final Thoughts

    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.