Skip to Content
Learn
Distance Formula
Euclidean Distance

Euclidean Distance is the most commonly used distance formula. To find the Euclidean distance between two points, we first calculate the squared distance between each dimension. If we add up all of these squared differences and take the square root, we’ve computed the Euclidean distance.

Let’s take a look at the equation that represents what we just learned:

(a1b1)2+(a2b2)2++(anbn)2\sqrt{(a_1-b_1)^2+(a_2-b_2)^2+\ldots+(a_n - b_n)^2}

The image below shows a visual of Euclidean distance being calculated:

The Euclidean distance between two points.

d=(a1b1)2+(a2b2)2d = \sqrt{(a_1-b_1)^2+(a_2-b_2)^2}

Instructions

1.

Create a function named euclidean_distance() that takes two lists as parameters named pt1 and pt2.

In the function, create a variable named distance, set it equal to 0, and return distance.

2.

After defining distance, create a for loop to loop through the dimensions of each point.

Add the squared difference between each dimension to distance.

Remember, in Python, you can square the variable num by using num ** 2.

3.

Outside of the for loop, take the square root of distance and return that value.

4.

Print the Euclidean distance between [1, 2] and [4, 0].

Print the Euclidean distance between [5, 4, 3] and [1, 7, 9].

Why can’t you find the difference between [2, 3, 4] and [1, 2]?

Folder Icon

Sign up to start coding

Already have an account?