In the previous article, we have discussed Python Program to Find Line Passing Through 2 Points

Given two points of a line (which are the start and endpoints) and the task is to find the midpoint of the given line in python.

**The formula for the midpoint of a line:**

Let the two points of a line are (x1, y2) and (x2, y2).

The formula for midpoint is:

Midpoint = ((x_{1}+x_{2})/2 , (y_{1}+y_{2})/2)

**Examples:**

**Example1:**

**Input:**

Given First Point = ( 3 , 1 ) Given Second Point = ( 4 , 5 )

**Output:**

The Midpoint of the given line is: ( 3 , 3 )

**Example2:**

**Input:**

Given First Point = ( 6 , -2 ) Given Second Point = ( 7, 4 )

**Output:**

The Midpoint of the given line is: ( 6 , 1 )

## Program to Find the Mid-Point of a Line in Python

Below are the ways to find the midpoint of the given line in python:

### Method #1: Using Mathematical Formula (Static Input)

**Approach:**

- Give the first point as static input and store it in two variables.
- Give the second point as static input and store it in another two variables.
- Pass the given two points of a line i.e, a1, a2, b1, b2 as the arguments to the
**Find_Midpoint****()**function. - Create a function to say
**Find_Midpoint()**which takes the given two points of a line i.e, a1, a2, b1, b2 as the arguments, and prints the midpoint of the given line. - Calculate the x coordinate of the midpoint of the given line using the above given mathematical formula and store it in a variable.
- Calculate the y coordinate of the midpoint of the given line using the above given mathematical formula and store it in another variable.
- Print the midpoint of the given line.
- The Exit of the Program.

**Below is the implementation:**

# Create a function to say Find_Midpoint() which takes the given two points of a line i.e, # a1, a2, b1, b2 as the arguments, and prints the midpoint of the given line. def Find_Midpoint(a1, a2, b1, b2): # Calculate the x coordinate of the midpoint of the given line using the above # given mathematical formula and store it in a variable. mid_x_Coordinate = (a1 + a2) // 2 # Calculate the y coordinate of the midpoint of the given line using the above # given mathematical formula and store it in another variable. mid_y_Coordinate = (b1 + b2) // 2 # Print the midpoint of the given line. print("(", mid_x_Coordinate, ",", mid_y_Coordinate, ")") # Give the first point as static input and store it in two variables. a1 = 3 b1 = 1 # Give the second point as static input and store it in another two variables. a2 = 4 b2 = 5 print("The Midpoint of the given line is:") # Pass the given two points of a line i.e, a1, a2, b1, b2 as the arguments to the # Find_Midpoint() function. Find_Midpoint(a1, a2, b1, b2)

**Output:**

The Midpoint of the given line is: ( 3 , 3 )

### Method #2: Using Mathematical Formula (User Input)

**Approach:**

- Give the first point as user input using map(),int(),split() functions and store it in two variables.
- Give the second point as user input using map(),int(),split() functions and store it in two variables.
- Pass the given two points of a line i.e, a1, a2, b1, b2 as the arguments to the
**Find_Midpoint****()**function. - Create a function to say
**Find_Midpoint()**which takes the given two points of a line i.e, a1, a2, b1, b2 as the arguments, and prints the midpoint of the given line. - Calculate the x coordinate of the midpoint of the given line using the above given mathematical formula and store it in a variable.
- Calculate the y coordinate of the midpoint of the given line using the above given mathematical formula and store it in another variable.
- Print the midpoint of the given line.
- The Exit of the Program.

**Below is the implementation:**

# Create a function to say Find_Midpoint() which takes the given two points of a line i.e, # a1, a2, b1, b2 as the arguments, and prints the midpoint of the given line. def Find_Midpoint(a1, a2, b1, b2): # Calculate the x coordinate of the midpoint of the given line using the above # given mathematical formula and store it in a variable. mid_x_Coordinate = (a1 + a2) // 2 # Calculate the y coordinate of the midpoint of the given line using the above # given mathematical formula and store it in another variable. mid_y_Coordinate = (b1 + b2) // 2 # Print the midpoint of the given line. print("(", mid_x_Coordinate, ",", mid_y_Coordinate, ")") # Give the first point as user input using map(),int(),split() functions # and store it in two variables. a1, b1 = map(int, input( 'Enter some random first point values separated by spaces = ').split()) # Give the second point as user input using map(),int(),split() functions # and store it in two variables. a2, b2 = map(int, input( 'Enter some random second point values separated by spaces = ').split()) print("The Midpoint of the given line is:") # Pass the given two points of a line i.e, a1, a2, b1, b2 as the arguments to the # Find_Midpoint() function. Find_Midpoint(a1, a2, b1, b2)

**Output:**

Enter some random first point values separated by spaces = 6 -2 Enter some random second point values separated by spaces = 7 4 The Midpoint of the given line is: ( 6 , 1 )

The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners. Using them you can develop code on your own and master coding skills.