calculate distances in python

To calculate distance between two points, you can use the math module in Python. The math module provides a function called sqrt() to calculate the square root of a number, which can be used to calculate the distance using the distance formula. Here is an example code to calculate the distance between two points (x1, y1) and (x2, y2):

main.py
import math

def calculate_distance(x1, y1, x2, y2):
    distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
    return distance

distance = calculate_distance(1, 2, 3, 4)
print(distance)
186 chars
9 lines

In this code, we are importing the math module to use the sqrt() function. We define a function called calculate_distance() that takes four arguments: x1, y1, x2, y2. The function calculates the distance between the two points using the distance formula and returns the result. We then call the calculate_distance() function with some sample values and print the result.

gistlibby LogSnag