Kodeclik Logo

Our Programs

Learn More

Schedule

Kodeclik Blog

Absolute values in Python

The Python abs() function returns the absolute value of a number. It works with integers, floating-point numbers, and complex numbers. Here are several examples to illustrate its usage:

Absolute values in Python

Example 1: Absolute Value of Integers

The abs() function can be used to find the absolute value of positive and negative integers.

positive_num = 42
negative_num = -17

print("Absolute value of", positive_num, "is:", abs(positive_num))
print("Absolute value of", negative_num, "is:", abs(negative_num))

In this code, we apply abs() to both a positive and negative integer. For the positive number, the function returns the same value, while for the negative number, it returns its positive counterpart.

The output will be:

Absolute value of 42 is: 42
Absolute value of -17 is: 17

Example 2: Absolute Value of Floating-Point Numbers

The abs() function also works with floating-point numbers, maintaining their decimal precision.

positive_float = 3.14
negative_float = -0.001

print("Absolute value of", positive_float, "is:", abs(positive_float))
print("Absolute value of", negative_float, "is:", abs(negative_float))

This example demonstrates that abs() preserves the decimal places of floating-point numbers while returning their absolute values.

The output will be:

Absolute value of 3.14 is: 3.14
Absolute value of -0.001 is: 0.001

Example 3: Absolute Value of Complex Numbers

For complex numbers, abs() returns the magnitude of the number, calculated using the Pythagorean theorem.

complex_num = 3 + 4j

magnitude = abs(complex_num)
print("Magnitude of", complex_num, "is:", magnitude)

In this case, abs() calculates the magnitude of the complex number using the formula √(a² + b²), where a is the real part and b is the imaginary part.

The output will be:

Magnitude of (3+4j) is: 5.0

​​Example 4: Using abs() in Calculations

The abs() function can be useful in various calculations, such as finding the difference between two numbers regardless of their order.

num1 = 10
num2 = 25

difference = abs(num1 - num2)
print("The absolute difference between", num1, "and", num2, "is:", difference)

This example shows how abs() can be used to find the absolute difference between two numbers, which is useful when you're interested in the magnitude of the difference rather than which number is larger.

The absolute difference between 10 and 25 is: 15

Example 5: Using abs() with binary (base 2) numbers

In Python, binary numbers are prefixed with '0b'. The abs() function works directly with these binary literals.

# Positive binary number
positive_binary = 0b1010  # Binary representation of 10
abs_positive = abs(positive_binary)
print(f"Absolute value of binary 0b1010 is: {abs_positive}")

# Negative binary number
negative_binary = -0b1010  # Binary representation of -10
abs_negative = abs(negative_binary)
print(f"Absolute value of binary -0b1010 is: {abs_negative}")

In this example, we're using the '0b' prefix to denote binary literals. Python interprets these as integers, so we can apply abs() directly without any conversion.

The output will be:

Absolute value of binary 0b1010 is: 10
Absolute value of binary -0b1010 is: 10

Example 6: Using abs() with octal (base 8) numbers

For octal numbers, we use the '0o' prefix to denote an octal literal in Python.

octal_number = 0o17  # Octal representation of 15
absolute_value = abs(octal_number)
print(f"Absolute value of octal 0o17 is: {absolute_value}")

This code demonstrates using abs() with an octal number. Python automatically converts the octal to decimal before applying abs().

The output is:

Absolute value of octal 0o17 is: 15

Example 6: Using abs() with hexadecimal (base 12) numbers

Hexadecimal numbers in Python are prefixed with '0x'.

hex_number = 0xFF  # Hexadecimal representation of 255
negative_hex = -hex_number
absolute_value = abs(negative_hex)
print(f"Absolute value of hexadecimal -0xFF is: {absolute_value}")

Here, we take the absolute value of a negative hexadecimal number. Python converts it to decimal internally before applying abs().

Absolute value of hexadecimal -0xFF is: 255

These examples demonstrate that regardless of the base in which a number is represented, Python's abs() function always operates on the decimal (base 10) equivalent of the number. For non-decimal bases, Python first converts the number to its decimal representation before applying the abs() function.

abs() vs fabs() in Python

In addition to the abs() function, there is a function called fabs(). They are both used to calculate absolute values, but they have some key differences.

abs() is a built-in Python function, so it doesn't require importing any module. It works with integers, floating-point numbers, and complex numbers, as we have seen above.

fabs() on the other hand is part of the math module, so you need to import it using “from math import fabs”. It only works with real numbers (integers and floats), not complex numbers. Further,this function always returns a float, even if the input is an integer.

In most cases, abs() is more versatile and commonly used. However, if you're working exclusively with real numbers and need the best performance, math.fabs() might be preferable.

Thus the abs() function in Python is a very effective one, handling different numeric types and providing a simple way to obtain absolute values or magnitudes in various scenarios.

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.