{"id":24546,"date":"2021-10-21T10:11:51","date_gmt":"2021-10-21T04:41:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=24546"},"modified":"2021-11-05T19:37:36","modified_gmt":"2021-11-05T14:07:36","slug":"python-abs-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-abs-function-with-examples\/","title":{"rendered":"Python abs() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for any() Function<\/a> The abs() function calculates the absolute value of the given number.<\/p>\n If the number is a complex number, the abs() function returns the magnitude of the number.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n n: <\/strong>This is required. It is a number. It can be an integer, floating number, or complex number.<\/p>\n Return value:<\/strong><\/p>\n The abs() method returns the absolute value of a number.<\/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 Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Program for any() Function abs() Function in Python: The abs() function calculates the absolute value of the given number. If the number is a complex number, the abs() function returns the magnitude of the number. Syntax: abs(n) Parameters n: This is required. It is a number. It …<\/p>\n
\nabs() Function in Python:<\/strong><\/p>\nabs(n)<\/pre>\n
\n
Given first number = -10\r\nGiven second number = 2+6j<\/pre>\n
The absolute value of the given number -10 = 10\r\nThe absolute value of the given number (2+6j) = 6.324555320336759<\/pre>\n
Given first number = -12.55\r\nGiven second number = 30<\/pre>\n
The absolute value of the given number -12.55 = 12.55\r\nThe absolute value of the given number 30 = 30<\/pre>\n
Program for abs() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb1 = -10\r\n# Apply abs() function to the given number to get the absolute value of\r\n# the given number and store it in another variable.\r\nrslt_charctr1 = abs(gvn_numb1)\r\n# Print the absolute value of the given number.\r\nprint(\"The absolute value of the given number\",\r\n gvn_numb1, \"=\", rslt_charctr1)\r\n# Similarly, do the same for the other number and print the result.\r\ngvn_numb2 = 2+6j\r\nrslt_charctr2 = abs(gvn_numb2)\r\nprint(\"The absolute value of the given number\",\r\n gvn_numb2, \"=\", rslt_charctr2)\r\n<\/pre>\n
The absolute value of the given number -10 = 10\r\nThe absolute value of the given number (2+6j) = 6.324555320336759<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number as user input using the float(input()) function and store it in a variable.\r\ngvn_numb1 = float(input(\"Enter some random number = \"))\r\n# Apply abs() function to the given number to get the absolute value of\r\n# the given number.\r\n# store it in another variable.\r\nrslt_charctr1 = abs(gvn_numb1)\r\n# Print the absolute value of the given number.\r\nprint(\"The absolute value of the given number\",\r\n gvn_numb1, \"=\", rslt_charctr1)\r\n# Similarly, do the same for the other number and print the result.\r\ngvn_numb2 = int(input(\"Enter some random number = \"))\r\nrslt_charctr2 = abs(gvn_numb2)\r\nprint(\"The absolute value of the given number\",\r\n gvn_numb2, \"=\", rslt_charctr2)\r\n<\/pre>\n
Enter some random number = -12.55\r\nThe absolute value of the given number -12.55 = 12.55\r\nEnter some random number = 30\r\nThe absolute value of the given number 30 = 30<\/pre>\n
\n