w3resource

Python: Compute the distance between two points

Python Basic: Exercise-40 with Solution

Write a Python program to compute the distance between the points (x1, y1) and (x2, y2).

Pictorial Presentation:

Compute the distance between two points

Sample Solution:-

Python Code:

import math
p1 = [4, 0]
p2 = [6, 6]
distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )

print(distance)

Sample Output:

6.324555320336759 

Flowchart:

Flowchart: Compute the distance between two points.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to compute the future value of a specified principal amount, rate of interest, and a number of years.
Next: Write a Python program to check whether a file exists.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Unpack variables from iterable:

# One can unpack all iterables (tuples, list etc)
>>> a, b, c = 1, 2, 3
>>> a, b, c
(1, 2, 3)

>>> a, b, c = [1, 2, 3]
>>> a, b, c
(1, 2, 3)