• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] Python abs() Function

Python abs() Function

Python’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`.

## Example Integer abs()

The following code snippet shows you how to use the absolute value 42 of a positive integer value 42.

```# POSITIVE INTEGER
x = 42
abs_x = abs(x) print(f"Absolute value of {x} is {abs_x}")
# Absolute value of 42 is 42```

The following code snippet shows you how to use the absolute value 42 of a negative integer value -42.

```# NEGATIVE INTEGER
x = -42
abs_x = abs(x) print(f"Absolute value of {x} is {abs_x}")
# Absolute value of -42 is 42```

## Example Float abs()

The following code snippet shows you how to use the absolute value 42.42 of a positive integer value 42.42.

```# 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```

The following code snippet shows you how to use the absolute value 42.42 of a negative integer value -42.42.

```# 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```

## Example Complex abs()

The following code snippet shows you how to use the absolute value of a complex number (3+10j).

```# 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
```

## Python abs() vs fabs()

Python’s built-in function `abs(x)` calculates the absolute number of the argument `x`. Similarly, the `fabs(x)` function of the math module calculates the same absolute value. The difference is that `math.fabs(x)` always returns a float number while Python’s built-in `abs(x)` returns an integer if the argument `x` is an integer as well. The name “fabs” is shorthand for “float absolute value”.

Here’s a minimal example:

```x = 42 # abs()
print(abs(x))
# 42 # math.fabs()
import math
print(math.fabs(x))
# 42.0```

## Python abs() vs np.abs()

Python’s built-in function `abs(x)` calculates the absolute number of the argument `x`. Similarly, NumPy’s `np.abs(x)` function calculates the same absolute value. There are two differences: (1) `np.abs(x)` always returns a float number while Python’s built-in `abs(x)` returns an integer if the argument `x` is an integer, and (2) `np.abs(arr)` can be also applied to a NumPy array `arr` that calculates the absolute values element-wise.

Here’s a minimal example:

```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]```

abs and np. 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 Python programmers because the names are identical to the built-in Python functions.

## Summary

The `abs()` function is a built-in function that returns the absolute value of a number. The function accepts integers, floats, and complex numbers as input.

If you pass `abs()` an integer or float, `n`, it returns the non-negative value of `n` and preserves its type. In other words, if you pass an integer, `abs()` returns an integer, and if you pass a float, it returns a float.

```# Int returns int
>>> abs(20)
20
# Float returns float
>>> abs(20.0)
20.0
>>> abs(-20.0)
20.0```

The first example returns an int, the second returns a float, and the final example returns a float and demonstrates that `abs()` always returns a positive number.

Complex numbers are made up of two parts and can be written as `a + bj` where `a` and `b` are either ints or floats. The absolute value of `a + bj` is defined mathematically as `math.sqrt(a**2 + b**2)`. Thus, the result is always positive and always a float (since taking the square root always returns a float).

```>>> abs(3 + 4j)
5.0
>>> math.sqrt(3**2 + 4**2)
5.0```

Here you can see that `abs()` always returns a float and that the result of `abs(a + bj)` is the same as `math.sqrt(a**2 + b**2)`.

## Where to Go From Here?

Enough theory, let’s get some practice!

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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python abs() Function first appeared on Finxter.

https://www.sickgaming.net/blog/2020/11/...-function/

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] How To Apply A Function To Each Element Of A List? xSicKxBot 0 23 04-25-2021, 07:02 PM Last Post: xSicKxBot [Tut] The Reduce Function in Python 3: Simply Explained xSicKxBot 0 22 04-21-2021, 11:44 PM Last Post: xSicKxBot [Tut] Python vars() Function xSicKxBot 0 78 02-18-2021, 04:10 PM Last Post: xSicKxBot [Tut] Python object() Function xSicKxBot 0 86 02-16-2021, 08:16 PM Last Post: xSicKxBot [Tut] Python repr() Function — A Helpful Guide with Example xSicKxBot 0 85 02-15-2021, 02:40 PM Last Post: xSicKxBot [Tut] Python str() Function xSicKxBot 0 79 02-14-2021, 06:05 AM Last Post: xSicKxBot [Tut] Python int() Function xSicKxBot 0 88 02-04-2021, 08:21 PM Last Post: xSicKxBot [Tut] How to Chain Multiple Function Calls in Python? xSicKxBot 0 84 01-27-2021, 05:47 PM Last Post: xSicKxBot [Tut] A Guide to Python’s pow() Function xSicKxBot 0 103 12-22-2020, 02:24 PM Last Post: xSicKxBot [Tut] Python complex() — A Useless Python Feature? xSicKxBot 0 93 12-19-2020, 02:05 AM Last Post: xSicKxBot

Forum Jump:

 Active Threads (Indie Deal) FREE Disgraced, Waifu Desir... Last Post: xSicKxBot Today 07:52 PM » Replies: 0 » Views: 2 Unreal Engine May 2021 Free Marketplace ... Last Post: xSicKxBot Today 07:52 PM » Replies: 0 » Views: 3 Microsoft - Humans and AI: Meet Juan Lav... Last Post: xSicKxBot Today 07:51 PM » Replies: 0 » Views: 2 Fedora - Introducing the Fedora i3 Spin Last Post: xSicKxBot Today 07:51 PM » Replies: 0 » Views: 2 News - Detective Pikachu Star Doesn’t Se... Last Post: xSicKxBot Today 07:51 PM » Replies: 0 » Views: 2 News - NPD Reveals Top 10 Best-Selling S... Last Post: xSicKxBot Today 07:51 PM » Replies: 0 » Views: 2 News - Blizzard Loses Millions Of Monthl... Last Post: xSicKxBot Today 07:50 PM » Replies: 0 » Views: 6 AppleInsider - Apple Pay launches in Isr... Last Post: xSicKxBot Today 12:18 PM » Replies: 0 » Views: 10 Microsoft - Teachers: Thank you for insp... Last Post: xSicKxBot Today 12:18 PM » Replies: 0 » Views: 6 News - UK Charts: New Pokémon Snap Tops ... Last Post: xSicKxBot Today 12:17 PM » Replies: 0 » Views: 7