How to Compute Distance in Python? [ Easy Step-By-Step Guide ]

Feature Img Distances Python

Hey there! Today we are going to learn how to compute distances in the python programming language. In this tutorial, we will be computing the following distances:

  1. Hamming Distance
  2. Euclidean Distance
  3. Manhattan Distance

We will be looking at the formulas for each distance calculation and then learn how to calculate the same with the help of python code.

Also read: Compute a^n in Python: Different Ways to Calculate Power in Python

Calculate Hamming Distance in Python

Hamming Distance is calculated between two numbers but in binary format. It basically implies the number of bits that differ between the two numbers in binary format.

For instance, if we choose the binary numbers 101 and 111 then the Hamming distance between them is 1 as they differ by only one binary digit.

Implement Hamming Distance in Python

Now to count the number of bits that differ we will make use of the XOR operation. XOR only results in 1 if the bits differ otherwise it results in 0. Finally, we will compute the number of set bits in the XOR of the two numbers.

a = int(input())
b = int(input())

x = a^b
final_ans = 0;

while (x > 0):
    final_ans += x & 1;
    x >>= 1;

print("First Number: ",a)
print("Second Number: ",b)
print("Hamming Distance: ",final_ans)

We entered 12 and 9 as the two inputs and the hamming distance came out to be 3 as shown in the output below.

First Number:  9
Second Number:  14
Hamming Distance:  3

Calculate Euclidean Distance in Python

Euclidean Distance is a distance between two points in space that can be measured with the help of the Pythagorean formula. The formula is shown below:

Consider the points as (x,y,z) and (a,b,c) then the distance is computed as:
square root of [ (x-a)^2 + (y-b)^2 + (z-c)^2 ].


To calculate the Euclidean Distance between two coordinate points we will be making use of the numpy module in python.

import numpy as np
p1 = np.array((1,2,3))
p2 = np.array((3,2,1))
sq = np.sum(np.square(p1 - p2))

The output of the code mentioned above comes out to be 2.8284271247461903. You can also compute the distance using the calculator manually it will come out approximately the same.

Also read: Calculating the Distance Between Nodes in an Unweighted Graph

Calculate Manhattan Distance in Python

The Manhattan distance between two vectors/arrays (say A and B), is calculated as Σ|Ai – Bi| where Ai is the ith element in the first array and Bi is the ith element in the second array.

Code Implementation

A = [1,2,3]
B = [5,3,2]

dis = 0

for i in range(len(A)):
    dis += abs(A[i] - B[i])

print("First Array is: ", A)
print("Second Array is: ", B)
print("Manhattan Distance is: ", dis)

The output of the code mentioned above is displayed below.

First Array is:  [1, 2, 3]
Second Array is:  [5, 3, 2]
Manhattan Distance is:  6


I hope you understood the concept and code logic for all the distance computations mentioned in the tutorial. Thank you for reading the tutorial!

Happy Learning!😇