Python abs() Function Explained

The abs() function returns the absolute value of a number.

Beginners usually use it when they want:

  • a number without its negative sign
  • the distance between two numbers
  • the size of a value without caring whether it is positive or negative

It works with common Python number types such as integers, floats, and complex numbers.

Quick example

print(abs(-7))
print(abs(3.5))
print(abs(-2+3j))

Output:

7
3.5
3.605551275463989

Use abs() to get the absolute value of a number. For complex numbers, it returns the magnitude.

What abs() does

abs() returns the absolute value of a number.

That means:

  • negative integers and floats become positive
  • positive numbers stay the same
  • 0 stays 0

Examples:

  • abs(-5) gives 5
  • abs(9) gives 9
  • abs(0) gives 0

This is useful when you care about how large a number is, but not whether it is positive or negative.

Basic syntax

The syntax is:

abs(x)

Here:

  • x is usually an int, float, or complex number
  • abs() returns a new value
  • it does not change the original variable

Example:

number = -12
result = abs(number)

print(number)
print(result)

Output:

-12
12

Notice that number is still -12. If you want to keep the absolute value, you must store it in a variable.

If you are not sure what type a value has, the type() function can help.

Examples with integers and floats

Here are some basic examples:

print(abs(-10))
print(abs(8))
print(abs(-4.2))

Output:

10
8
4.2

What each line does:

  • abs(-10) returns 10
  • abs(8) returns 8
  • abs(-4.2) returns 4.2

This works because integers and floats are standard numeric types in Python. If you want a refresher, see Python numbers explained: int, float, complex.

Example with complex numbers

abs() also works with complex numbers.

For a complex number, it returns the distance from 0 in the complex plane. You do not need advanced math to use this. In practice, it gives the size, or magnitude, of the complex number.

Example:

value = 3 + 4j
print(abs(value))

Output:

5.0

Why 5.0?

  • the real part is 3
  • the imaginary part is 4
  • the magnitude is 5.0

For beginners, the main thing to remember is simple:

  • abs() on a complex number returns a float
  • it gives the number's magnitude, not a complex number back

Common beginner uses

Here are some common ways beginners use abs().

Finding the distance between two numbers

A very common pattern is:

abs(a - b)

Example:

a = 3
b = 10

distance = abs(a - b)
print(distance)

Output:

7

This tells you how far apart two numbers are.

Comparing how far a value is from zero

temperature_change = -6
print(abs(temperature_change))

Output:

6

This is useful when the size of the change matters more than the direction.

Working with negative input values safely

If a user enters a negative number but your program needs a positive value, abs() can help.

balance_change = -25
safe_value = abs(balance_change)

print(safe_value)

Output:

25

Checking the absolute difference in simple programs

guess = 42
target = 50

difference = abs(guess - target)
print(difference)

Output:

8

This is useful in guessing games, scoring programs, and simple comparisons.

Common mistakes

Passing a string to abs()

This causes an error:

print(abs("5"))

"5" is a string, not a number. abs() needs a numeric value.

Fix it by converting the string first with int() or float():

print(abs(int("5")))
print(abs(float("-3.2")))

Output:

5
3.2

If you are working with keyboard input, this mistake often happens because input() always returns a string. See how to convert a string to an int in Python.

Passing a list or dictionary

Unsupported types also cause an error:

print(abs([1, 2, 3]))

This raises a TypeError because lists do not have an absolute value.

Assuming abs() changes the original variable

abs() does not update the variable by itself.

x = -9
abs(x)

print(x)

Output:

-9

If you want to store the result:

x = -9
x = abs(x)

print(x)

Output:

9

Confusing absolute value with rounding

abs() does not round numbers.

print(abs(-4.7))

Output:

4.7

If you expected 5, that is a different operation. abs() removes the negative sign. It does not change the decimal part.

Return values and types

abs() returns different types depending on the input:

  • int input → returns an int
  • float input → returns a float
  • complex input → returns a float

Example:

print(type(abs(-5)))
print(type(abs(-3.2)))
print(type(abs(3 + 4j)))

Output:

<class 'int'>
<class 'float'>
<class 'float'>

This matters because beginners often check value types while debugging.

If your value comes from user input, convert it first and then check the type if needed.

Useful debugging steps:

value = "-5"
print(value)
print(type(value))

user_input = "-3.7"
print(abs(float(user_input)))

Output:

-5
<class 'str'>
3.7

FAQ

What does abs() mean in Python?

It returns the absolute value of a number. This means the value without a negative sign.

Does abs() work with floats?

Yes. It works with integers, floats, and complex numbers.

Does abs() change the original variable?

No. It returns a new value. You must assign it if you want to store the result.

Why does abs(input()) give an error?

input() returns a string. Convert it first with int() or float() before using abs().

What does abs() return for a complex number?

It returns the magnitude as a float.

See also