Python abs()

Python abs() is a built-in function that helps you find the absolute value of a number. Let's explain what the absolute value means. The absolute value of a number is simply its distance from zero.
Table of Contents

Understanding Python abs() Function

Python abs() is a built-in function that returns the absolute value of a given number. Let’s explain what the absolute value means. The absolute value of a number is its distance from zero on the number line, regardless of its direction. This means the abs in python function will always return a non-negative value. You will get a positive value even if the number is positive, negative, or complex. Let’s explore some examples.

Syntax of Python abs()

absolute_value = abs(number)

Explanation

  • absolute_value: Variable that will store the result, which is the absolute value of the input number.
  • abs(): Built-in Python function used to calculate the absolute value.
  • number: Value (it can be an integer, a floating-point number, or a complex number) for which you want to find the absolute value.

Example of Python abs(): Calculating the Difference in Temperatures

Let’s say you’re creating a program comparing temperatures from two sensors. Sometimes, you’re not interested in which sensor has a higher or lower temperature but instead in the magnitude of the difference between the two readings. This is where Python abs() becomes useful.

sensor1_temp = 25
sensor2_temp = 18
temperature_difference = sensor1_temp - sensor2_temp
absolute_difference = abs(temperature_difference)
print(f"The absolute temperature difference is: {absolute_difference}°C")

Explanation

  • sensor1_temp = 25: Assigns the value 25 (representing degrees Celsius, for instance) to the variable sensor1_temp.
  • sensor2_temp = 18: Assigns the value 18 to the variable sensor2_temp.
  • temperature_difference = sensor1_temp - sensor2_temp: Calculate the difference between the two sensor readings. In this case, it’s 25 - 18, which equals 7. Note that if sensor2_temp were higher than sensor1_temp, this difference would be negative.
  • absolute_difference = abs(temperature_difference): This is where the abs() function comes in. We apply it to the temperature_difference. Since the difference is positive here (7), abs() will return 7. If the difference were negative (e.g., -7), abs() would still return 7, giving us the magnitude of the difference.
  • print(f"The absolute temperature difference is: {absolute_difference}°C"): Prints the result to the console. The output will be: The absolute temperature difference is: 7°C.

By using abs(), you can easily find the magnitude of the difference without needing extra conditional logic (like if statements) to check whether the difference is positive or negative.

Output

The absolute temperature difference is: 7°C


Absolute Value of a Negative Integer with Python abs()

When you have a negative integer, Python abs() removes the negative sign and returns the positive version of that number. It tells you how far the number is from zero on the number line without considering whether it’s to the left (negative) or right (positive) of zero. For example, if you have the number -7, the abs() function will return 7.

Syntax

positive_integer = abs(negative_integer)

Explanation

  • positive_integer: Variable will hold the positive integer, which is the result of the absolute value.
  • abs(): Function that does the work of finding the absolute value.
  • negative_integer: Negative integer that you’re putting into the abs() function.

Example

negative_integer = -25
positive_integer = abs(negative_integer)
print(positive_integer)

Explanation

  • negative_integer = -25: Sets a variable named negative_integer to the value -25.
  • positive_integer = abs(negative_integer): Function finds the absolute value of -25 and result is assigned to positive_integer.
  • print(positive_integer): Outputs the value of positive_integer, which will be 25.

Output

25


Absolute Value of a Floating-Point Number with Python abs()

The abs python function works similarly with floating-point numbers (numbers with a decimal point). Python abs() will return the positive equivalent of that number if you have a negative floating-point number. It ignores the negative sign and gives the number’s distance from zero. For example, if you use the abs() function with -5.67, the result will be 5.67.

Syntax

positive_float = abs(negative_float)

Explanation

  • positive_float: Variable will store the positive floating-point number after the function is applied.
  • abs(): Function calculates the absolute value.
  • negative_float: Negative floating-point number you are giving to the abs() function.

Example

negative_float = -12.34
positive_float = abs(negative_float)
print(positive_float)

Explanation

  • negative_float = -12.34: Assigns the value -12.34 to the variable negative_float.
  • positive_float = abs(negative_float): Function is used to find the absolute value of negative_float.
  • print(positive_float): Prints the value of positive_float, which will be 12.34.

Output

12.34


Magnitude of a Complex Number with Python abs()

When dealing with complex numbers, python abs doesn’t just remove a negative sign. Instead, it calculates the magnitude of the complex number. A complex number has a real part and an imaginary part. The magnitude represents the distance of the complex number from the origin (0,0) in the complex plane, and magnitude is always a positive real number. For a complex number, the magnitude is calculated using the Pythagorean theorem. For a complex number a + bj, the magnitude will be sqrt(a^2 + b^2).

Syntax

magnitude = abs(complex_number)

Explanation

  • magnitude: Variable will hold the calculated magnitude of the complex number.
  • abs(): Function used to calculate the magnitude.
  • complex_number: Complex number (e.g., 3 + 4j) for which you want to find the magnitude.

Example

complex_num = 2 + 3j
magnitude = abs(complex_num)
print(magnitude)

Explanation

  • complex_num = 2 + 3j: Assigns the complex number 2 + 3j to the variable complex_num.
  • magnitude = abs(complex_num): Function calculates the magnitude of the complex_num.
  • print(magnitude): Print the calculated magnitude which will be 3.605551275463989.

Output

3.605551275463989


Conclusion

Python abs() function is a versatile tool for finding the absolute value of numbers. Whether you are working with negative integers, floating-point numbers, or complex numbers, abs in python provides a simple way to determine a number’s distance from zero. It simplifies many mathematical and programming tasks by ensuring that you are always working with non-negative values. Remember abs() function will always return a non-negative value.


Also Read

Python any()


Python Reference

python abs()

Table of Contents