{"id":121324,"date":"2020-11-29T17:45:22","date_gmt":"2020-11-29T17:45:22","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=17541"},"modified":"2020-11-29T17:45:22","modified_gmt":"2020-11-29T17:45:22","slug":"python-abs-function","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/11\/29\/python-abs-function\/","title":{"rendered":"Python abs() Function"},"content":{"rendered":"<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python Built-in Functions - abs()\" width=\"1333\" height=\"1000\" src=\"https:\/\/www.youtube.com\/embed\/2C6i5uLxGoU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p class=\"has-pale-cyan-blue-background-color has-background\">Python&#8217;s <a href=\"https:\/\/blog.finxter.com\/python-built-in-functions\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Built-In Functions\">built-in<\/a> <code><strong>abs(x)<\/strong><\/code> function returns the absolute value of the argument <code>x<\/code> that can be an integer, float, or object implementing the <code>__abs__()<\/code> function. For a complex number, the function returns its magnitude. The absolute value of any numerical input argument <code>-x<\/code> or <code>+x<\/code> is the corresponding positive value <code>+x<\/code>. <\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>Argument<\/strong><\/td>\n<td><code>x<\/code><\/td>\n<td>int, float, complex, object with <code>__abs__()<\/code> implementation<\/td>\n<\/tr>\n<tr>\n<td><strong>Return Value<\/strong><\/td>\n<td><code>|x|<\/code><\/td>\n<td><strong>Returns the absolute value of the input argument.<\/strong><br \/>Integer input &#8211;> Integer output<br \/>Float input &#8211;> Float output<br \/>Complex input &#8211;> Complex output<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2>Interactive Code Shell<\/h2>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/40d2918774\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<h2>Example Integer abs()<\/h2>\n<p>The following code snippet shows you how to use the absolute value 42 of a positive integer value 42.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># POSITIVE INTEGER\nx = 42\nabs_x = abs(x) print(f\"Absolute value of {x} is {abs_x}\")\n# Absolute value of 42 is 42<\/pre>\n<p>The following code snippet shows you how to use the absolute value 42 of a negative integer value -42.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># NEGATIVE INTEGER\nx = -42\nabs_x = abs(x) print(f\"Absolute value of {x} is {abs_x}\")\n# Absolute value of -42 is 42<\/pre>\n<h2>Example Float abs()<\/h2>\n<p>The following code snippet shows you how to use the absolute value 42.42 of a positive integer value 42.42.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># POSITIVE FLOAT\nx = 42.42\nabs_x = abs(x) print(f\"Absolute value of {x} is {abs_x}\")\n# Absolute value of 42.42 is 42.42<\/pre>\n<p>The following code snippet shows you how to use the absolute value 42.42 of a negative integer value -42.42.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># NEGATIVE FLOAT\nx = -42.42\nabs_x = abs(x) print(f\"Absolute value of {x} is {abs_x}\")\n# Absolute value of -42.42 is 42.42<\/pre>\n<h2>Example Complex abs()<\/h2>\n<p>The following code snippet shows you how to use the absolute value of a complex number (3+10j).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># COMPLEX NUMBER\ncomplex_number = (3+10j)\nabs_complex_number = abs(complex_number) print(f\"Absolute value of {complex_number} is {abs_complex_number}\")\n# Absolute value of (3+10j) is 10.44030650891055\n<\/pre>\n<h2>Python abs() vs fabs()<\/h2>\n<p class=\"has-pale-cyan-blue-background-color has-background\">Python&#8217;s built-in function <code>abs(x)<\/code> calculates the absolute number of the argument <code>x<\/code>. Similarly, the <code>fabs(x)<\/code> function of the math module calculates the same absolute value. The difference is that <code>math.fabs(x)<\/code> always returns a float number while Python&#8217;s built-in <code>abs(x)<\/code> returns an integer if the argument <code>x<\/code> is an integer as well. The name <em><strong>&#8220;fabs&#8221;<\/strong><\/em> is shorthand for <em><strong>&#8220;float absolute value&#8221;<\/strong><\/em>. <\/p>\n<p>Here&#8217;s a minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = 42 # abs()\nprint(abs(x))\n# 42 # math.fabs()\nimport math\nprint(math.fabs(x))\n# 42.0<\/pre>\n<h2>Python abs() vs np.abs()<\/h2>\n<p class=\"has-pale-cyan-blue-background-color has-background\">Python&#8217;s built-in function <code>abs(x)<\/code> calculates the absolute number of the argument <code>x<\/code>. Similarly, NumPy&#8217;s <code>np.abs(x)<\/code> function calculates the same absolute value. There are two differences: (1) <code>np.abs(x)<\/code> always returns a float number while Python&#8217;s built-in <code>abs(x)<\/code> returns an integer if the argument <code>x<\/code> is an integer, and (2) <code>np.abs(arr)<\/code> can be also applied to a NumPy array <code>arr<\/code> that calculates the absolute values element-wise. <\/p>\n<p>Here&#8217;s a minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = 42 # abs()\nprint(abs(x))\n# 42 # numpy.abs()\nimport numpy as np\nprint(np.fabs(x))\n# 42.0 # numpy.abs() array\na = np.array([-1, 2, -4])\nprint(np.abs(a))\n# [1 2 4]<\/pre>\n<\/p>\n<p><strong>abs<\/strong> and <strong>np<\/strong>. absolute are completely identical. It doesn&#8217;t matter which one you use. There are several advantages to the short names: They are shorter and they are known to <strong>Python<\/strong> programmers because the names are identical to the built-in <strong>Python<\/strong> functions.<\/p>\n<h2>Summary<\/h2>\n<p>The <code>abs()<\/code> function is a built-in function that returns the absolute value of a number. The function accepts integers, floats, and complex numbers as input.<\/p>\n<p>If you pass <code>abs()<\/code> an integer or float, <code>n<\/code>, it returns the non-negative value of <code>n<\/code> and preserves its type. In other words, if you pass an integer, <code>abs()<\/code> returns an integer, and if you pass a float, it returns a float.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Int returns int\n>>> abs(20)\n20\n# Float returns float\n>>> abs(20.0)\n20.0\n>>> abs(-20.0)\n20.0<\/pre>\n<p>The first example returns an int, the second returns a float, and the final example returns a float and demonstrates that <code>abs()<\/code> always returns a positive number.<\/p>\n<p>Complex numbers are made up of two parts and can be written as <code>a + bj<\/code> where <code>a<\/code> and <code>b<\/code> are either ints or floats. The absolute value of <code>a + bj<\/code> is defined mathematically as <code>math.sqrt(a**2 + b**2)<\/code>. Thus, the result is always positive and always a float (since taking the square root always returns a float). <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> abs(3 + 4j)\n5.0\n>>> math.sqrt(3**2 + 4**2)\n5.0<\/pre>\n<p>Here you can see that <code>abs()<\/code> always returns a float and that the result of <code>abs(a + bj)<\/code> is the same as <code>math.sqrt(a**2 + b**2)<\/code>.<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/python-abs\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python abs() Function<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;s built-in abs(x) function returns the absolute value of the argument x that can be an integer, float, or object implementing the __abs__() function. For a complex number, the function returns its magnitude. The absolute value of any numerical input argument -x or +x is the corresponding positive value +x. Argument x int, float, complex, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-121324","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/121324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=121324"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/121324\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=121324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=121324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=121324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}