Table of contents
1.
Introduction
2.
Calculate the Distance Between Two Points
3.
Implementation
3.1.
Method 1: Using the math Module
3.2.
Method 2: Using math.dist()
3.3.
Method 3: Using NumPy
3.4.
Method 4: Using scipy.spatial.distance
4.
Examples
4.1.
Example 1: Distance Between Two Points in a 3D Space
4.2.
Example 2: Distance Between Two Latitude-Longitude Points
5.
Frequently Asked Questions
5.1.
What is the best way to calculate the distance between two points in Python?
5.2.
Why use NumPy instead of the math module?
5.3.
How can I calculate distances in a dataset?
6.
Conclusion
Last Updated: Mar 11, 2025
Medium

Program to Calculate Distance Between Two Points in Python

Author Gaurav Gandhi
0 upvote

Introduction

Calculating the distance between two points is a common mathematical problem that can be solved using Python. The distance is typically measured using the Euclidean formula, which considers the horizontal and vertical differences between the points. Python provides various methods to compute this, including mathematical operations and built-in libraries. 

Program to Calculate Distance Between Two Points in Python

In this article, we will discuss different approaches to calculate the distance between two points in Python, along with examples..

Calculate the Distance Between Two Points

In a 2D plane, the distance between two points (x1,y1) and (x2,y2) is calculated using the Euclidean distance formula:

Formula

Python provides multiple ways to compute this distance:

  1. Using the math module
     
  2. Using NumPy
     
  3. Using the scipy.spatial.distance module

Implementation

Method 1: Using the math Module

Python's built-in math module provides the sqrt() function, which we can use to calculate the Euclidean distance.

import math
def distance(x1, y1, x2, y2):
    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
# Example Usage
x1, y1 = 3, 4
x2, y2 = 7, 1
dist = distance(x1, y1, x2, y2)
print("Distance:", dist)
You can also try this code with Online Python Compiler
Run Code


Output:

Distance: 5.0

Method 2: Using math.dist()

Python 3.8 introduced the math.dist() function, which simplifies the computation of Euclidean distance.

import math
point1 = (3, 4)
point2 = (7, 1)
dist = math.dist(point1, point2)
print("Distance:", dist)
You can also try this code with Online Python Compiler
Run Code


Output:

Distance: 5.0

Method 3: Using NumPy

NumPy is a powerful library for numerical computations. You can use numpy.linalg.norm() to compute the distance.

import numpy as np
point1 = np.array([3, 4])
point2 = np.array([7, 1])
dist = np.linalg.norm(point2 - point1)
print("Distance:", dist)
You can also try this code with Online Python Compiler
Run Code


Output:

Distance: 5.0

Method 4: Using scipy.spatial.distance

The scipy.spatial module provides a direct method to compute the Euclidean distance between two points.

from scipy.spatial import distance
point1 = (3, 4)
point2 = (7, 1)
dist = distance.euclidean(point1, point2)
print("Distance:", dist)
You can also try this code with Online Python Compiler
Run Code


Output:

Distance: 5.0

Examples

Example 1: Distance Between Two Points in a 3D Space

The formula for 3D Euclidean distance is:

Formula

Here’s a Python implementation:

import math
def distance_3d(x1, y1, z1, x2, y2, z2):
    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2)
# Example Usage
x1, y1, z1 = 1, 2, 3
x2, y2, z2 = 4, 6, 8
dist = distance_3d(x1, y1, z1, x2, y2, z2)
print("3D Distance:", dist)
You can also try this code with Online Python Compiler
Run Code


Output:

3D Distance: 7.0710678118654755

Example 2: Distance Between Two Latitude-Longitude Points

For geographic coordinates, the Haversine formula is used to calculate the distance between two points on Earth.

Formula:
 

Formula

Where:

  • R is Earth's radius (≈ 6371 km).
     
  • Δφ and Δλ are latitude and longitude differences in radians.

from math import radians, sin, cos, sqrt, atan2
def haversine(lat1, lon1, lat2, lon2):
    R = 6371  # Radius of the Earth in km
    dlat = radians(lat2 - lat1)
    dlon = radians(lon2 - lon1)
    a = sin(dlat/2)**2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon/2)**2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))
    return R * c
# Example Usage
lat1, lon1 = 52.2296756, 21.0122287
lat2, lon2 = 41.8919300, 12.5113300
dist = haversine(lat1, lon1, lat2, lon2)
print("Geographical Distance:", dist, "km")
You can also try this code with Online Python Compiler
Run Code

 

Output:

Geographical Distance: 1317.135 km

Frequently Asked Questions

What is the best way to calculate the distance between two points in Python?

The best way depends on the context. For 2D points we can use math.dist() (Python 3.8+) or numPy.linalg.norm(). For 3D points: Use math.sqrt() with the extended formula.

Why use NumPy instead of the math module?

numPy is optimized for large-scale numerical computations, making it faster and more efficient, especially when working with multiple distance calculations in arrays.

How can I calculate distances in a dataset?

For datasets with multiple points, consider using scipy’s distance functions or pandas with numPy to apply distance calculations efficiently.

Conclusion

In this article, we discussed on how to calculate the distance between two points in Python using a simple mathematical approach. We used the math module to perform the calculations efficiently. This program is useful in geometry, mapping applications, and real-world distance measurements.

Live masterclass