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
-7is7 - Absolute value of
7is7 - Absolute value of
0is0
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: 0I executed the above example code and added the screenshot below.

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: 0I executed the above example code and added the screenshot below.

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: 0I executed the above example code and added the screenshot below.

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: 56I executed the above example code and added the screenshot below.

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.0This 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:
- Find the Sum of an Array in Python
- Remove Duplicates from an Array in Python
- Check if an Array Index Exists in Python
- Remove NaN from an Array in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.