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
🌐
GeeksforGeeks
geeksforgeeks.org › python › abs-in-python
abs() in Python - GeeksforGeeks
August 30, 2024 - Hence we will use the abs() method to calculate the exact time, distance, and speed. ... We declared 3 functions to calculate speed, distance, and time. Then passed the positive and negative integer and float point values to them using the Python abs() function.
🌐
Real Python
realpython.com › ref › builtin-functions › abs
abs() | Python’s Built-in Functions – Real Python
The abs() function in Python returns a number’s absolute value, which is its distance from zero on the number line, regardless of its sign. ... For real numbers, returns a non-negative number of the same type (int or float). For complex numbers, it returns the magnitude as a float (that is, the square root of the sum of squares of real and imaginary parts). ... >>> from fractions import Fraction >>> abs(Fraction("1/3")) Fraction(1, 3) >>> abs(Fraction("-3/4")) Fraction(3, 4)
🌐
Programiz
programiz.com › python-programming › methods › built-in › abs
Python abs()
Python __import__() Python super() Python complex() Python Numbers, Type Conversion and Mathematics · Python Mathematical Functions · Python Random Module · Python Programming Built-in Functions · Python int() The abs() function returns the absolute value of the given number.
🌐
W3Schools
w3schools.com › python › ref_func_abs.asp
Python abs() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The abs() function returns the absolute value of the specified number.
🌐
freeCodeCamp
freecodecamp.org › news › python-absolute-value-python-abs-tutorial
Python Absolute Value – Python abs Tutorial
June 20, 2022 - The abs() built-in Python function returns the absolute value of a number.
🌐
LearnDataSci
learndatasci.com › solutions › python-absolute-value
Python Absolute Value – abs() for real and complex numbers – LearnDataSci
In Python, we use j to represent the imaginary number because i is often used to represent indexes. For complex numbers, we use the Pythagorean theorem to calculate magnitude, like so: $$\large \vert 6 + 7j \vert = \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.
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
Return the absolute value of a number. The argument may be an integer, a floating-point number, or an object implementing __abs__().
Find elsewhere
🌐
PyPI
pypi.org › project › abs-imports
abs-imports · PyPI
A tool to automatically replace relative imports with absolute ones.
      » pip install abs-imports
    
Published   Feb 13, 2021
Version   0.2.1
🌐
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.
🌐
Guru99
guru99.com › home › python › python abs() function: absolute value examples
Python abs() Function: Absolute Value Examples
August 12, 2024 - Python abs() is a built-in function available with the standard library of python. It returns the absolute value for the given number. Absolute value of a number is the value without considering its sign. The number can be integer, floating point number or complex number.
🌐
Trymito
trymito.io › excel-to-python › functions › math › ABS
Excel to Python: ABS Function - A Complete Guide | Mito
That means you don't need worry about managing data types, handling errors, or the edge case differences between Excel and Python formulas. Install Mito to start using Excel formulas in Python. # Import the mitosheet Excel functions from mitosheet.public.v3 import *; # Use Mito's ABS function df['Absolute Value'] = ABS(df['A'])
🌐
Real Python
realpython.com › absolute-vs-relative-python-imports
Absolute vs Relative Imports in Python – Real Python
September 29, 2023 - What happens when you have code in one module that needs to access code in another module or package? You import it! ... The first thing Python will do is look up the name abc in sys.modules.
🌐
Codecademy
codecademy.com › docs › python:numpy › math methods › .abs()
Python:NumPy | Math Methods | .abs() | Codecademy
June 12, 2025 - Calculates the absolute value of a given number or each element in an array.
🌐
Python.org
discuss.python.org › python help
EPSILON = 1E-14 + abs Function - Python Help - Discussions on Python.org
October 4, 2021 - Hey y’all. I’m back. Hopefully I don’t get too annoying over the next little bit, but I’m really interested in learning to code. The course I’m currently taking in Uni uses the following code to demonstrate comparisons of numbers/strings. The only block I’m really struggling with is the bit I’ve outlined with a comment (EPSILON block).
🌐
Server Academy
serveracademy.com › blog › python-absolute-value-abs-tutorial
Python Absolute Value Abs() Tutorial - Server Academy
The absolute value represents the distance of a number from zero on the number line, always as a positive value, and Python makes it simple to work with absolute values through the built-in abs() function and other methods in the math library.
🌐
Real Python
realpython.com › python-absolute-value
How to Find an Absolute Value in Python – Real Python
June 4, 2025 - The abs() function in Python accepts all numeric data types available, including the lesser-known fractions and decimals. For instance, you can get the absolute value of one-third or minus three-quarters defined as Fraction instances: ... >>> from fractions import Fraction >>> abs(Fraction("1/3")) Fraction(1, 3) >>> abs(Fraction("-3/4")) Fraction(3, 4)
🌐
Javatpoint
javatpoint.com › python-abs-function
abs() in Python | Python abs() Function with Examples - Javatpoint
abs() in Python | Python abs() Function with Examples on Python, Built in, Functions, abs Function, all Function, bin Function, bool Function, python Functions, new sum Function, bytes Function, new callable Function etc.
🌐
TradingCode
tradingcode.net › python › math › absolute-value
How to get absolute value of numbers in Python? • TradingCode
If your positive and negative values are in a Python array, then getting their absolute values is similar to working with lists. Here’s an example: import array # Create an array with random values values = array.array('i', [-40, 58, -69, -84, 51, 76, -12, 36]) # Make a new array with the absolute values absValues = array.array('i', [abs(value) for value in values]) # Output the results print("Original array values:\n", values) print("Absolute values:\n", absValues)
🌐
Iq-inc
iq-inc.com › python-absolute-value
Python Absolute Value – IQ Inc
This is a Python built-in function that is always available. abs(0.1) # 0.1 abs(-0.1) # 0.1 abs(-5) # 5 abs(-1e3) # 1000.0 · Sometimes though you might need to get the absolute value from every element in a list. In this case, you can use a list expansion and abs: ... Maybe though you are using N-dimensional arrays (ndarrays) via NumPy, in that case, you can get the element-wise absolute value of a ndarray using numpy.absolute: import numpy as np np.absolute([ [-1,2], [2,-1] ]) # np.array([ [1, 2], [2, 1] ])