In the previous article, we have discussed Python Program for Section Formula (Point that Divides a Line in Given Ratio)
Given two points of a line, the task is to find the slope of a given line.
Let A(x1,y1) and B(x2,y2) are the two points on a straight line.
Formula :
Formula to find the slope of a given line is:
slope=(y2-y1)/(x2-x1)
Examples:
Example1:
Input:
Given First Point = ( 5, 3 ) Given Second Point = ( 1, 2 )
Output:
The slope of the line for the given two points is : 0.25
Example2:
Input:
Given First Point = ( 5, 1 ) Given Second Point = ( 6, 2 )
Output:
The slope of the line for the given two points is : 1.0
Program to Find Slope of a Line in Python
Below are the ways to find the slope of a 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_Slope() function.
- Create a function to say Find_Slope() which takes the given two points of a line i.e, a1, a2, b1, b2 as the arguments and returns the slope of the given line.
- Inside the function, calculate the slope of the line with the given two points using the above mathematical formula and convert it into float using the float() function.
- Store it in a variable.
- Return the above result i.e, the slope of the line.
- The Exit of the Program.
Below is the implementation:
# Create a function to say Find_Slope() which takes the given two points of a line i.e, # a1, a2, b1, b2 as the arguments and returns the slope of the given line. def Find_Slope(a1, a2, b1, b2): # Inside the function, calculate the slope of the line with the given two points # using the above mathematical formula and convert it into float using the # float() function. # Store it in a variable. rslt_slope = (float)(b2-b1)/(a2-a1) # Return the above result i.e, the slope of the line. return rslt_slope # Give the first point as static input and store it in two variables. a1 = 5 b1 = 3 # Give the second point as static input and store it in another two variables. a2 = 1 b2 = 2 print("The slope of the line for the given two points is :") # Pass the given two points of a line i.e, a1, a2, b1, b2, as the arguments to the # Find_Slope() function. print(Find_Slope(a1, a2, b1, b2))
Output:
The slope of the line for the given two points is : 0.25
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_Slope() function.
- Create a function to say Find_Slope() which takes the given two points of a line i.e, a1, a2, b1, b2 as the arguments and returns the slope of the given line.
- Inside the function, calculate the slope of the line with the given two points using the above mathematical formula and convert it into float using the float() function.
- Store it in a variable.
- Return the above result i.e, the slope of the line.
- The Exit of the Program.
Below is the implementation:
# Create a function to say Find_Slope() which takes the given two points of a line i.e, # a1, a2, b1, b2 as the arguments and returns the slope of the given line. def Find_Slope(a1, a2, b1, b2): # Inside the function, calculate the slope of the line with the given two points # using the above mathematical formula and convert it into float using the # float() function. # Store it in a variable. rslt_slope = (float)(b2-b1)/(a2-a1) # Return the above result i.e, the slope of the line. return rslt_slope # 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 slope of the line for the given two points is :") # Pass the given two points of a line i.e, a1, a2, b1, b2, as the arguments to the # Find_Slope() function. print(Find_Slope(a1, a2, b1, b2))
Output:
Enter some random first point values separated by spaces = 5 1 Enter some random second point values separated by spaces = 6 2 The slope of the line for the given two points is : 1.0
Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.