{"id":24099,"date":"2021-10-14T09:12:25","date_gmt":"2021-10-14T03:42:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=24099"},"modified":"2021-11-05T20:53:04","modified_gmt":"2021-11-05T15:23:04","slug":"python-program-for-fabs-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-fabs-function\/","title":{"rendered":"Python Program for fabs() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for copysign() Function<\/a> The math. fabs() method returns a float containing the absolute value of a number.<\/p>\n The term absolute refers to a non-negative number. If the value has a negative sign, this removes it.<\/p>\n This method, unlike Python abs(), always converts the value to a float value.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n x: <\/strong>This is required. It is a number. If we try anything other than a number, we get a TypeError.<\/p>\n Return Value: <\/strong>It returns a float value which represents the absolute value of x.<\/p>\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 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 Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples<\/a>.<\/p>\n In the previous article, we have discussed Python Program for copysign() Function fabs() Function in Python: The math. fabs() method returns a float containing the absolute value of a number. The term absolute refers to a non-negative number. If the value has a negative sign, this removes it. This method, unlike Python abs(), always converts …<\/p>\n
\nfabs() Function in Python:<\/strong><\/p>\nmath.fabs(x)<\/pre>\n
Given list = [1, -2, -3, 4]\r\nGiven first number = 45\r\nGiven second number = -70<\/pre>\n
The absolute value of above given number 45 = 45.0\r\nThe absolute value of above given number -70 = 70.0\r\nThe absolute value of given list element gvnlst[2] = 3.0<\/pre>\n
Given list = [86, 34, 20, 10]\r\nGiven first number = 90\r\nGiven second number = -40<\/pre>\n
The absolute value of above given number 90 = 90.0\r\nThe absolute value of above given number -40 = 40.0\r\nThe absolute value of given list element gvnlst[1] = 34.0<\/pre>\n
Program for fabs() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, -2, -3, 4]\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb1 = 45\r\n# Apply math.fabs() function to the given number to get the absolute value of\r\n# the given number as float and print it.\r\nprint(\"The absolute value of above given number\",\r\n gvn_numb1, \" = \", math.fabs(gvn_numb1))\r\n# similarly do the same for the other number.\r\ngvn_numb2 = -70\r\nprint(\"The absolute value of above given number\",\r\n gvn_numb2, \" = \", math.fabs(gvn_numb2))\r\n# Apply math.fabs() function to the given list element and print it.\r\nprint(\r\n \"The absolute value of given list element gvnlst[2] = \", math.fabs(gvn_lst[2]))\r\n<\/pre>\n
The absolute value of above given number 45 = 45.0\r\nThe absolute value of above given number -70 = 70.0\r\nThe absolute value of given list element gvnlst[2] = 3.0<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(float, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n \r\n# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numb1= int(input(\"Enter some random number = \"))\r\n# Apply math.fabs() function to the given first number to get the absolute value of\r\n# the given number as float and print it.\r\nprint(\"The absolute value of above given number\",\r\n gvn_numb1, \" = \", math.fabs(gvn_numb1))\r\n# similarly do the same for the other number.\r\ngvn_numb2 = int(input(\"Enter some random number = \"))\r\nprint(\"The absolute value of above given number\",\r\n gvn_numb2, \" = \", math.fabs(gvn_numb2))\r\n# Apply math.fabs() function to the given list element and print it.\r\nprint(\r\n \"The absolute value of given list element gvnlst[1] = \", math.fabs(gvn_lst[1]))\r\n\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 86 34 20 10\r\nEnter some random number = 90\r\nThe absolute value of above given number 90 = 90.0\r\nEnter some random number = -40\r\nThe absolute value of above given number -40 = 40.0\r\nThe absolute value of given list element gvnlst[1] = 34.0<\/pre>\n
\n