# How to Calculate Distance Between Two Points in Python

To calculate the distance between two points in Python, use the math.sqrt() function. The math.sqrt() is a built-in math module’s method that returns the square root of a number.

``````import math

x = input("Enter the first two co-ordinates: ")
y = input("Enter the second two co-ordinates: ")
x1, y1 = x.split(',')
x2, y2 = y.split(',')
x1 = int(x1)
x2 = int(x2)
y1 = int(y1)
y2 = int(y2)
distance = math.sqrt(((x2 - x1) ** 2) + (y2 - y1) ** 2)
print("The distance between the points is : ", str(distance))``````

#### Output

``````Enter the first two co-ordinates: 4, 5
Enter the second two co-ordinates: 6, 7

The distance between the points is : 2.8284271247461903``````

We used a sqrt() function from a math module to get the output in this program. We are using the same formula here.

## Calculate the Distance Between Two Points manually

We can implement a formula into a program to calculate the distance between two points in Python.

``````x1 = int(input("Enter the value of x1: "))
x2 = int(input("Enter the value of x2: "))
y1 = int(input("Enter the value of y1: "))
y2 = int(input("Enter the value of y2: "))
distance = (((x2 - x1) ** 2) + (y2 - y1) ** 2) ** (1 / 2)
print("The distance between the points is : ", str(distance))``````

#### Output

``The distance between the points is : 5.385164807134504``

In this program, we get input for the points x1,  x2, y1, and y2. The coordinates are ( x1, y1 ) and ( x2, y2 ). Then we calculate the distance by using a function called:

Distance = sqrt( ( x2 – x1 )² + ( y2 – y1)²)

See the below Python program.

``````x = input("Enter the first co-ordinates: ")
y = input("Enter the second co-ordinates: ")
x1, y1 = x.split(',')
x2, y2 = y.split(',')
x1 = int(x1)
x2 = int(x2)
y1 = int(y1)
y2 = int(y2)
distance = (((x2 - x1) ** 2) + (y2 - y1) ** 2) ** (1 / 2)
print("The distance between the points is : ", str(distance))``````

#### Output

``````Enter the first co-ordinates: 4, 5
Enter the second co-ordinates: 6, 7

The distance between the points is : 2.8284271247461903``````

In this program, we get inputs in the form of coordinate points; hence the input is the comma-separated value. The coordinates are x1, y1, and x2, y2. So, we can give the input like for x we can give 4, 5 and for y we can give 6, 7. Then, the distance is calculated using the distance formula.

That’s it for this tutorial.

## Related posts

How to Calculate distance between Two Points in 3D in Python

How to Calculate Euclidean distance in Python