{"id":22743,"date":"2021-09-27T15:43:12","date_gmt":"2021-09-27T10:13:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=22743"},"modified":"2021-11-22T18:35:37","modified_gmt":"2021-11-22T13:05:37","slug":"python-program-to-find-slope-of-a-line","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-slope-of-a-line\/","title":{"rendered":"Python Program to Find Slope of a Line"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Section Formula (Point that Divides a Line in Given Ratio)<\/a> Let\u00a0 A(x1<\/sub>,y1<\/sub>) and B(x2<\/sub>,y2<\/sub>) are the two points on a straight line.<\/p>\n Formula :<\/strong><\/p>\n Formula to find the slope of a given line is:<\/p>\n slope=(y2-y1)\/(x2-x1)<\/p><\/blockquote>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the slope of a given line in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find the best practical and ready-to-use Python Programming Examples<\/a> that you can simply run on a variety of platforms and never stop learning.<\/p>\n 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\u00a0 A(x1,y1) and B(x2,y2) are the two points on a straight line. Formula : Formula to find the …<\/p>\n
\nGiven two points of a line, the task is to find the slope of a given line.<\/p>\nGiven First Point = ( 5, 3 ) \r\nGiven Second Point = ( 1, 2 )<\/pre>\n
The slope of the line for the given two points is :\r\n0.25<\/pre>\n
Given First Point = ( 5, 1 ) \r\nGiven Second Point = ( 6, 2 )<\/pre>\n
The slope of the line for the given two points is :\r\n1.0<\/pre>\n
Program to Find Slope of a Line in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Create a function to say Find_Slope() which takes the given two points of a line i.e,\r\n# a1, a2, b1, b2 as the arguments and returns the slope of the given line.\r\n\r\n\r\ndef Find_Slope(a1, a2, b1, b2):\r\n # Inside the function, calculate the slope of the line with the given two points\r\n # using the above mathematical formula and convert it into float using the\r\n # float() function.\r\n\r\n # Store it in a variable.\r\n rslt_slope = (float)(b2-b1)\/(a2-a1)\r\n # Return the above result i.e, the slope of the line.\r\n return rslt_slope\r\n\r\n\r\n# Give the first point as static input and store it in two variables.\r\na1 = 5\r\nb1 = 3\r\n# Give the second point as static input and store it in another two variables.\r\na2 = 1\r\nb2 = 2\r\nprint(\"The slope of the line for the given two points is :\")\r\n# Pass the given two points of a line i.e, a1, a2, b1, b2, as the arguments to the\r\n# Find_Slope() function.\r\nprint(Find_Slope(a1, a2, b1, b2))\r\n<\/pre>\n
The slope of the line for the given two points is :\r\n0.25<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\n
# Create a function to say Find_Slope() which takes the given two points of a line i.e,\r\n# a1, a2, b1, b2 as the arguments and returns the slope of the given line.\r\n\r\n\r\ndef Find_Slope(a1, a2, b1, b2):\r\n # Inside the function, calculate the slope of the line with the given two points\r\n # using the above mathematical formula and convert it into float using the\r\n # float() function.\r\n\r\n # Store it in a variable.\r\n rslt_slope = (float)(b2-b1)\/(a2-a1)\r\n # Return the above result i.e, the slope of the line.\r\n return rslt_slope\r\n\r\n\r\n# Give the first point as user input using map(),int(),split() functions\r\n# and store it in two variables.\r\na1, b1 = map(int, input(\r\n 'Enter some random first point values separated by spaces = ').split())\r\n# Give the second point as user input using map(),int(),split() functions\r\n# and store it in two variables.\r\na2, b2 = map(int, input(\r\n 'Enter some random second point values separated by spaces = ').split())\r\nprint(\"The slope of the line for the given two points is :\")\r\n# Pass the given two points of a line i.e, a1, a2, b1, b2, as the arguments to the\r\n# Find_Slope() function.\r\nprint(Find_Slope(a1, a2, b1, b2))\r\n\r\n<\/pre>\n
Enter some random first point values separated by spaces = 5 1\r\nEnter some random second point values separated by spaces = 6 2\r\nThe slope of the line for the given two points is :\r\n1.0<\/pre>\n
\n