{"id":668,"date":"2024-10-21T05:25:47","date_gmt":"2024-10-21T05:25:47","guid":{"rendered":"https:\/\/itsmycode.com\/?p=668"},"modified":"2024-10-21T05:25:48","modified_gmt":"2024-10-21T05:25:48","slug":"python-abs","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-abs\/","title":{"rendered":"Python abs()"},"content":{"rendered":"\n<p>The <strong><code>abs()<\/code> <\/strong>function in Python returns the absolute value of a given number, which means the <strong><code>abs()<\/code><\/strong> method removes the negative sign of a number.&nbsp;<\/p>\n\n\n\n<p>If the given number is complex, then the <strong><code>abs()<\/code><\/strong> function will return its magnitude.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"abs-syntax\"><strong>abs() Syntax&nbsp;<\/strong><\/h2>\n\n\n\n<p>The syntax of <strong><code>abs()<\/code><\/strong> method is&nbsp;<\/p>\n\n\n\n<p><strong>abs(number)<\/strong>&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"abs-parameters\">abs() Parameters<\/h2>\n\n\n\n<p>The <strong><code>abs()<\/code><\/strong> function takes a single argument, a number whose absolute value is to be returned. The number can be&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>integer<\/li>\n\n\n\n<li>floating-point number<\/li>\n\n\n\n<li>complex number<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"abs-return-value\">abs() Return Value<\/h2>\n\n\n\n<p><strong><code>abs()<\/code><\/strong> method returns an absolute value of a given number.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>For integers<\/strong>\u00a0\u2013 absolute integer value is returned<\/li>\n\n\n\n<li><strong>For floating numbers<\/strong>\u00a0\u2013 absolute floating-point value is returned<\/li>\n\n\n\n<li><strong>For complex numbers<\/strong>\u00a0\u2013 the magnitude of the number is returned<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-does-the-abs-function-do-in-python\">What does the abs() function do in Python?<\/h2>\n\n\n\n<p>The <strong><code>abs()<\/code><\/strong> function will turn any negative number into positive, the absolute value of a negative number is multiplied by (-1) and returns the positive number, and the absolute value of a positive number is the number itself.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1-get-absolute-value-of-a-number-in-python\">Example 1: Get absolute value of a number in Python<\/h2>\n\n\n\n<p>In this example, we will pass an integer, float into the <strong><code>abs()<\/code><\/strong> function, and returns the absolute value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python code to illustrate abs() built-in function\n\n# integer number\ninteger = -10\nprint('Absolute value of -10 is:', abs(integer))\n\n# positive integer number\npositive_integer =20\nprint('Absolute value of 20 is:', abs(positive_integer))\n\n# floating point number\nfloating = -33.33\nprint('Absolute value of -33.33 is:', abs(floating))\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Absolute value of -10 is: 10\nAbsolute value of 20 is: 20\nAbsolute value of -33.33 is: 33.33\nAbsolute value of 3-4j is: 5.0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-get-the-magnitude-of-a-complex-number\">Example 2: Get the magnitude of a complex number<\/h2>\n\n\n\n<p>In this example, we will pass a complex number into the <strong><code>abs()<\/code><\/strong> function and returns the absolute value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python code to illustrate abs() built-in function\n\n# complex number\ncomplex=(3-4j)\nprint('Absolute value of 3-4j is:', abs(complex))<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Absolute value of 3-4j is: 5.0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The abs() function in Python returns the absolute value of a given number, which means the abs() method removes the negative sign of a number.&nbsp; If the given number is complex, then the abs() function will return its magnitude. abs() Syntax&nbsp; The syntax of abs() method is&nbsp; abs(number)&nbsp; abs() Parameters The abs() function takes a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":669,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,1],"tags":[],"class_list":["post-668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=668"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":670,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/668\/revisions\/670"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/669"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}