find the midpoint between two points in python

To find the midpoint between two points ((x1, y1) and (x2, y2)), we can use the following formula:

main.py
midpoint = ((x1 + x2) / 2, (y1 + y2) / 2)
42 chars
2 lines

Here is the Python code to find the midpoint between two points:

main.py
import math

def midpoint(x1, y1, x2, y2):
    return ((x1 + x2) / 2, (y1 + y2) / 2)
    
# Example usage
mid = midpoint(1, 2, 3, 4)
print(mid)  # Output: (2.0, 3.0)
166 chars
9 lines

In this code, we first import the math module to use mathematical operations. Then we define a function named midpoint that takes four parameters as input (x1, y1, x2, y2). We then use the formula mentioned above to calculate the midpoint and return it as a tuple.

gistlibby LogSnag