[Tut] Python abs() Function - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Python abs() Function (/thread-98526.html) |
[Tut] Python abs() Function - xSicKxBot - 11-30-2020 Python abs() Function <div><figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <div class="ast-oembed-container"><iframe 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> </div> </figure> <p class="has-pale-cyan-blue-background-color has-background">Python’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> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><strong>Argument</strong></td> <td><code>x</code></td> <td>int, float, complex, object with <code>__abs__()</code> implementation</td> </tr> <tr> <td><strong>Return Value</strong></td> <td><code>|x|</code></td> <td><strong>Returns the absolute value of the input argument.</strong><br />Integer input –> Integer output<br />Float input –> Float output<br />Complex input –> Complex output</td> </tr> </tbody> </table> </figure> <h2>Interactive Code Shell</h2> <p> <iframe src="https://trinket.io/embed/python/40d2918774" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p> <h2>Example Integer abs()</h2> <p>The following code snippet shows you how to use the absolute value 42 of a positive integer value 42.</p> <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 x = 42 abs_x = abs(x) print(f"Absolute value of {x} is {abs_x}") # Absolute value of 42 is 42</pre> <p>The following code snippet shows you how to use the absolute value 42 of a negative integer value -42.</p> <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 x = -42 abs_x = abs(x) print(f"Absolute value of {x} is {abs_x}") # Absolute value of -42 is 42</pre> <h2>Example Float abs()</h2> <p>The following code snippet shows you how to use the absolute value 42.42 of a positive integer value 42.42.</p> <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 x = 42.42 abs_x = abs(x) print(f"Absolute value of {x} is {abs_x}") # Absolute value of 42.42 is 42.42</pre> <p>The following code snippet shows you how to use the absolute value 42.42 of a negative integer value -42.42.</p> <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 x = -42.42 abs_x = abs(x) print(f"Absolute value of {x} is {abs_x}") # Absolute value of -42.42 is 42.42</pre> <h2>Example Complex abs()</h2> <p>The following code snippet shows you how to use the absolute value of a complex number (3+10j).</p> <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 complex_number = (3+10j) abs_complex_number = abs(complex_number) print(f"Absolute value of {complex_number} is {abs_complex_number}") # Absolute value of (3+10j) is 10.44030650891055 </pre> <h2>Python abs() vs fabs()</h2> <p class="has-pale-cyan-blue-background-color has-background">Python’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’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>“fabs”</strong></em> is shorthand for <em><strong>“float absolute value”</strong></em>. </p> <p>Here’s a minimal example:</p> <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() print(abs(x)) # 42 # math.fabs() import math print(math.fabs(x)) # 42.0</pre> <h2>Python abs() vs np.abs()</h2> <p class="has-pale-cyan-blue-background-color has-background">Python’s built-in function <code>abs(x)</code> calculates the absolute number of the argument <code>x</code>. Similarly, NumPy’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’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> <p>Here’s a minimal example:</p> <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() print(abs(x)) # 42 # numpy.abs() import numpy as np print(np.fabs(x)) # 42.0 # numpy.abs() array a = np.array([-1, 2, -4]) print(np.abs(a)) # [1 2 4]</pre> </p> <p><strong>abs</strong> and <strong>np</strong>. absolute are completely identical. It doesn’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> <h2>Summary</h2> <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> <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> <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 >>> abs(20) 20 # Float returns float >>> abs(20.0) 20.0 >>> abs(-20.0) 20.0</pre> <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> <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> <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) 5.0 >>> math.sqrt(3**2 + 4**2) 5.0</pre> <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> <h2>Where to Go From Here?</h2> <p>Enough theory, let’s get some practice!</p> <p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p> <p><strong>Practice projects is how you sharpen your saw in coding!</strong></p> <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> <p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p> <p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p> <p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p> <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> </div> https://www.sickgaming.net/blog/2020/11/29/python-abs-function/ |