0
0
PythonProgramBeginner · 2 min read

Python Program to Find Distance Between Two Points

You can find the distance between two points (x1, y1) and (x2, y2) in Python using the formula distance = ((x2 - x1)**2 + (y2 - y1)**2)**0.5.
📋

Examples

Input(0, 0) and (3, 4)
Output5.0
Input(1, 2) and (4, 6)
Output5.0
Input(2, 3) and (2, 3)
Output0.0
🧠

How to Think About It

To find the distance between two points, think of the points as corners of a right triangle. The distance is the length of the hypotenuse. Use the difference in x-coordinates and y-coordinates, square them, add them, and then take the square root.
📐

Algorithm

1
Get the coordinates of the first point (x1, y1).
2
Get the coordinates of the second point (x2, y2).
3
Calculate the difference in x-coordinates: dx = x2 - x1.
4
Calculate the difference in y-coordinates: dy = y2 - y1.
5
Calculate the distance using the formula: distance = square root of (dx squared + dy squared).
6
Return or print the distance.
💻

Code

python
def distance_between_points(x1, y1, x2, y2):
    distance = ((x2 - x1)**2 + (y2 - y1)**2)**0.5
    return distance

# Example usage
print(distance_between_points(0, 0, 3, 4))
Output
5.0
🔍

Dry Run

Let's trace the example (0, 0) and (3, 4) through the code

1

Input points

x1 = 0, y1 = 0, x2 = 3, y2 = 4

2

Calculate differences

dx = 3 - 0 = 3, dy = 4 - 0 = 4

3

Calculate distance

distance = sqrt(3**2 + 4**2) = sqrt(9 + 16) = sqrt(25) = 5.0

4

Return result

distance = 5.0

Stepdxdydistance
Calculate differences34
Calculate distance5.0
💡

Why This Works

Step 1: Calculate differences

Subtract the x and y coordinates to find the horizontal and vertical distances between points using dx = x2 - x1 and dy = y2 - y1.

Step 2: Apply Pythagorean theorem

Square the differences and add them: dx**2 + dy**2 gives the square of the distance.

Step 3: Find the distance

Take the square root of the sum using **0.5 to get the actual distance between the points.

🔄

Alternative Approaches

Using math.sqrt function
python
import math

def distance_between_points(x1, y1, x2, y2):
    return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

print(distance_between_points(0, 0, 3, 4))
This uses the built-in math.sqrt for clarity and readability; it is slightly more explicit.
Using numpy library
python
import numpy as np

def distance_between_points(p1, p2):
    return np.linalg.norm(np.array(p2) - np.array(p1))

print(distance_between_points((0, 0), (3, 4)))
This method is useful when working with arrays or many points but requires installing numpy.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation involves a fixed number of arithmetic operations, so it runs in constant time.

Space Complexity

Only a few variables are used to store intermediate results, so space usage is constant.

Which Approach is Fastest?

Using **0.5 or math.sqrt() both run in constant time; numpy adds overhead but is better for many points.

ApproachTimeSpaceBest For
Using **0.5O(1)O(1)Simple scripts without imports
Using math.sqrtO(1)O(1)Clearer code with standard library
Using numpy.linalg.normO(1)O(1)Working with arrays or many points
💡
Use **0.5 or math.sqrt() to find the square root in Python.
⚠️
Forgetting to square the differences before adding or taking the square root too early.