find the distance between two points in python

To find the distance between two points in Python, you can use the distance formula from geometry. The formula for the distance between two-dimensional points (x1, y1) and (x2, y2) is:

main.py
distance = √((x2 - x1)**2 + (y2 - y1)**2)
42 chars
2 lines

In Python, you can code this formula in a function:

main.py
import math

def distance(x1, y1, x2, y2):
    return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
93 chars
5 lines

Here, we're importing the math module to use its square root function. Then, the function distance takes four arguments: the x and y coordinates of the first point and the x and y coordinates of the second point. It returns the calculated distance between those points.

You can use this function by calling it with your desired input values:

main.py
print(distance(1, 2, 4, 6))
# Output: 5.0
42 chars
3 lines

This output means the distance between the points (1, 2) and (4, 6) is 5.0.

gistlibby LogSnag