![]() |
|
[Tut] Python abs() Function - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python abs() Function (/thread-98526.html) |
[Tut] Python abs() Function - xSicKxBot - 11-30-2020 Python abs() Function Python’s built-in Code: abs(x)Code: xCode: __abs__()Code: -xCode: +xCode: +xArgument | Code: xint, float, complex, object with Code: __abs__()Return Value | Code: |x|Returns the absolute value of the input argument. Integer input –> Integer output Float input –> Float output Complex input –> Complex output Interactive Code Shell https://trinket.io/embed/python/40d2918774 Example Integer abs() The following code snippet shows you how to use the absolute value 42 of a positive integer value 42. Code: # POSITIVE INTEGER
x = 42
abs_x = abs(x) print(f"Absolute value of {x} is {abs_x}")
# Absolute value of 42 is 42The following code snippet shows you how to use the absolute value 42 of a negative integer value -42. Code: # NEGATIVE INTEGER
x = -42
abs_x = abs(x) print(f"Absolute value of {x} is {abs_x}")
# Absolute value of -42 is 42The following code snippet shows you how to use the absolute value 42.42 of a positive integer value 42.42. Code: # 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.42The following code snippet shows you how to use the absolute value 42.42 of a negative integer value -42.42. Code: # 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.42The following code snippet shows you how to use the absolute value of a complex number (3+10j). Code: # 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.44030650891055Python’s built-in function Code: abs(x)Code: xCode: fabs(x)Code: math.fabs(x)Code: abs(x)Code: xHere’s a minimal example: Code: x = 42 # abs()
print(abs(x))
# 42 # math.fabs()
import math
print(math.fabs(x))
# 42.0Python’s built-in function Code: abs(x)Code: xCode: np.abs(x)Code: np.abs(x)Code: abs(x)Code: xCode: np.abs(arr)Code: arrHere’s a minimal example: Code: 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 Code: abs()If you pass Code: abs()Code: nCode: nCode: abs()Code: # Int returns int
>>> abs(20)
20
# Float returns float
>>> abs(20.0)
20.0
>>> abs(-20.0)
20.0The first example returns an int, the second returns a float, and the final example returns a float and demonstrates that Code: abs()Complex numbers are made up of two parts and can be written as Code: a + bjCode: aCode: bCode: a + bjCode: math.sqrt(a**2 + b**2)Code: >>> abs(3 + 4j)
5.0
>>> math.sqrt(3**2 + 4**2)
5.0Here you can see that Code: abs()Code: abs(a + bj)Code: 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. |