Get Absolute Value in Python Without Using abs() Function

Over my 10+ years of working with Python, I’ve often come across situations where I needed to calculate the absolute value of a number but couldn’t use the built-in abs() function.

At first, this might sound unusual; after all, abs() is quick and reliable. But in real-world projects, especially coding interviews, algorithm challenges, or restricted environments, you might be asked to implement absolute value logic manually.

In this tutorial, I’ll show you how to get the absolute value in Python without using abs(). I’ll walk you through multiple methods that I’ve personally used, keeping the explanations simple and practical.

What is Absolute Value in Python?

Before we jump into the methods, let’s quickly revisit what absolute value means. The absolute value of a number is its distance from zero on the number line, regardless of direction. In other words:

  • Absolute value of -7 is 7
  • Absolute value of 7 is 7
  • Absolute value of 0 is 0

In Python, we usually use the built-in abs() function for this. But since we are avoiding abs(), let’s explore alternative approaches.

Method 1 – Use Conditional Statements in Python

The easiest way to calculate absolute value in Python is by using an if-else statement. I’ve used this approach many times when teaching beginners because it’s simple and easy to understand.

def my_absolute_value(num):
    if num < 0:
        return -num
    else:
        return num

# Testing the function
print(my_absolute_value(-15))  # Output: 15
print(my_absolute_value(25))   # Output: 25
print(my_absolute_value(0))    # Output: 0

I executed the above example code and added the screenshot below.

python absolute value

Here, I check whether the number is negative. If it is, I multiply it by -1 to make it positive. Otherwise, I return the number as it is.

Method 2 – Use Python Multiplication Trick

Another interesting way I’ve used in coding challenges is by multiplying the number by its sign.

This trick uses the fact that True = 1 and False = 0 in Python.

def my_absolute_value(num):
    return (num * (num >= 0)) + (-num * (num < 0))

# Testing the function
print(my_absolute_value(-42))   # Output: 42
print(my_absolute_value(42))    # Output: 42
print(my_absolute_value(0))     # Output: 0

I executed the above example code and added the screenshot below.

python math abs

Here’s how it works:

  • If num >= 0, the first part (num * (num >= 0)) gives num, and the second part becomes 0.
  • If num < 0, the first part becomes 0, and the second part (-num * (num < 0)) gives the positive value.

It’s a neat one-liner that avoids conditionals while still being very readable.

Method 3 – Use Python Ternary Operator

Python supports a ternary operator (inline if-else), which is a compact way of writing conditions.

I often use this method in quick scripts where I want concise code.

def my_absolute_value(num):
    return -num if num < 0 else num

# Testing the function
print(my_absolute_value(-100))   # Output: 100
print(my_absolute_value(100))    # Output: 100
print(my_absolute_value(0))      # Output: 0

I executed the above example code and added the screenshot below.

absolute value in python

This is essentially the same as the first method but written in a single line. It’s elegant and Pythonic.

Method 4 – Use Python Bitwise Operations (for Integers)

This method is a bit advanced but very efficient. I’ve used it when working with low-level optimizations.

def my_absolute_value(num):
    mask = num >> 31   # Extract sign bit (works in 32-bit signed integers)
    return (num ^ mask) - mask

# Testing the function
print(my_absolute_value(-56))   # Output: 56
print(my_absolute_value(56))    # Output: 56

I executed the above example code and added the screenshot below.

absolute value python

This works because:

  • For negative numbers, shifting right fills with 1s, creating a mask.
  • XOR and subtraction, then flip the sign.

While this is not something I’d recommend for beginners, it’s a clever trick that shows how Python handles numbers under the hood.

Method 5 – Use Python Math Formula (Square Root of Square)

Another mathematical approach I’ve used in algorithmic problems is based on the formula: Absolute value of x = √(x²)

def my_absolute_value(num):
    return (num ** 2) ** 0.5

# Testing the function
print(my_absolute_value(-9))   # Output: 9.0
print(my_absolute_value(9))    # Output: 9.0

This method always returns a float, even if the input is an integer. So, while it’s mathematically correct, it may not be the most practical in all scenarios.

Which Method Should You Use?

From my experience, here’s when to use each method:

  • Conditional Statements (Method 1): Best for beginners, clear and easy.
  • Ternary Operator (Method 3): Great for concise, Pythonic code.
  • Multiplication Trick (Method 2): Fun and clever for coding challenges.
  • Bitwise Operations (Method 4): Useful for performance-focused tasks.
  • Math Formula (Method 5): Good for demonstrating mathematical concepts.

If you’re writing production code, I’d recommend sticking with the conditional or ternary methods for clarity.

While Python’s built-in abs() is the simplest way to get absolute values, it’s always good to know alternative approaches.

I’ve personally used these methods in interviews, contests, and even real-world projects where built-in functions were restricted.

Now that you’ve seen multiple ways to get the absolute value in Python without using abs(), you can choose the one that best fits your needs.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.