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:
The image below shows a visual of Euclidean distance being calculated:
Instructions
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
.
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
.
Outside of the for
loop, take the square root of distance
and return that value.
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]
?