abs() is a built-in function, so just replace all occurrences of math.abs with abs.

You should also use the isinstance() function for type checking instead of using type() and comparing, for example:

def distance_from_zero(num):
    if isinstance(num, (int, float)):
        return abs(num)
    else:
        return "Not an integer or float!"

Note that you may also want to include long and complex as valid numeric types.

Answer from Andrew Clark on Stack Overflow
🌐
Real Python
realpython.com › ref › builtin-functions › abs
abs() | Python’s Built-in Functions – Real Python
Learn how to calculate the Python absolute value with abs(), implement the math behind it from scratch, and customize it in your own classes. ... By Leodanis Pozo Ramos • Updated Feb. 5, 2026 • Reviewed by Brenda Weleschuk and Dan Bader ... Get a Python Cheat Sheet (PDF) and learn the basics ...
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
Return the natural logarithm of the absolute value of the Gamma function at x. Added in version 3.2. ... The mathematical constant π = 3.141592…, to available precision.
🌐
W3Schools
w3schools.com › python › ref_func_abs.asp
Python abs() Function
Built-in Modules Random Module ... Q&A Python Bootcamp Python Certificate Python Training ... The abs() function returns the absolute value of the specified number....
🌐
Programiz
programiz.com › python-programming › methods › built-in › abs
Python abs()
Python Mathematical Functions · Python Random Module · Python Programming Built-in Functions · Python int() The abs() function returns the absolute value of the given number. If the number is a complex number, abs() returns its magnitude. number = -20 · absolute_number = abs(number) ...
🌐
GeeksforGeeks
geeksforgeeks.org › abs-in-python
abs() in Python - GeeksforGeeks
August 30, 2024 - The Python abs() function return the absolute value. The absolute value of any number is always positive it removes the negative sign of a number in Python.
🌐
Trymito
trymito.io › excel-to-python › functions › math › ABS
Excel to Python: ABS Function - A Complete Guide | Mito
Math · ABS · Related FunctionsSUMROUNDCEIL · Excel's ABS function finds the absolute value of a number. The absolute value of a function is the non-negative value of a number. The absolute value function is commonly used, for example, to ...
Find elsewhere
🌐
Server Academy
serveracademy.com › blog › python-absolute-value-abs-tutorial
Python Absolute Value Abs() Tutorial - Server Academy
Here, abs() calculates the magnitude using the formula sqrt(real^2 + imag^2). For -3 + 4i, this results in 5.0. Python’s math library provides more advanced mathematical functions, including some ways to work with absolute values.
🌐
LearnDataSci
learndatasci.com › solutions › python-absolute-value
Python Absolute Value – abs() for real and complex numbers – LearnDataSci
In Python, we use j to represent ... \sqrt{6^2+7^2} \approx 9.22$$ Getting the absolute value of a value in Python is quick and easy to do using the abs() function....
🌐
Codecademy
codecademy.com › docs › python:numpy › math methods › .abs()
Python:NumPy | Math Methods | .abs() | Codecademy
June 12, 2025 - Elements where the condition is True get their absolute value computed, while False elements retain their original values. If the out parameter is used, unmodified elements keep their existing values in the output array. ... Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
🌐
GitHub
github.com › TheAlgorithms › Python › blob › master › maths › abs.py
Python/maths/abs.py at master · TheAlgorithms/Python
All Algorithms implemented in Python. Contribute to TheAlgorithms/Python development by creating an account on GitHub.
Author   TheAlgorithms
🌐
freeCodeCamp
freecodecamp.org › news › python-absolute-value-python-abs-tutorial
Python Absolute Value – Python abs Tutorial
June 20, 2022 - In Python, the imaginary part is created by adding the letter j as a suffix – not the letter i like it is in Mathematics. You add j to the end of a real number, like so: 1j or 4j.So, an example of a complex number in Python is 2 + 4j or 1 + 2j. Now, when it comes to the return value of the abs() function:
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get Absolute Values in Python: abs(), math.fabs() | note.nkmk.me
May 11, 2025 - In Python, you can get the absolute value of a number using either the built-in abs() function or the math module's fabs() function.
🌐
DataCamp
datacamp.com › tutorial › python-absolute-value-a-quick-tutorial
Python Absolute Value: A Quick Tutorial | DataCamp
April 11, 2024 - Key Takeaway: To use the abs() function, simply provide the number as an argument, and it will return its absolute value. For greater precision with floats, use the math.fabs() function.
🌐
Tutorialspoint
tutorialspoint.com › python › number_abs.htm
Python abs() function
The absolute value is concerned with the value or magnitude of a number instead of the sign attached to the number. That means, if a number is a positive value, the function returns the same value; but if the number is a negative value, the ...
🌐
Initial Commit
initialcommit.com › blog › python-absolute-value
Python Absolute Value
In this article, you learned how to use abs() to find the absolute value of integers, floating points, special numbers, and complex numbers in Python. You also took a look at the fabs() function from the math module, which is a useful alternative for when your output is required to be of the ...
🌐
Real Python
realpython.com › python-absolute-value
How to Find an Absolute Value in Python – Real Python
June 4, 2025 - This is sort of a mathematical trick because using a fractional exponent is equivalent to computing the 𝑛th root of a number. In this case, you take a squared number to the power of one-half (0.5) or one over two (½), which is the same as calculating the square root. Note that both Python implementations based on the algebraic definition suffer from a slight deficiency: ... >>> def absolute_value(x): ...
🌐
BeginnersBook
beginnersbook.com › 2019 › 03 › python-abs-function
Python abs() Function with examples
# complex number cnum = 4 - 5j print('Absolute value of 4 - 5j is:', abs(cnum)) cnum2 = 3 + 4j print('Absolute value of 3 + 4j is:', abs(cnum2))