---Advertisement---

Python abs() Function: Absolute Value Examples and Usage Best Guide 2025

By Bhavani

Updated On:

---Advertisement---
Python abs() Function

Python abs() Function: Absolute Value Examples and Usage Guide

The Python abs() function is a powerful built-in feature available in the Python standard library. This function returns the absolute value of the given number. The absolute value represents the non-negative value of a number, regardless of its sign.

The abs() function can handle:

  • Integers
  • Floating-point numbers
  • Complex numbers (returns the magnitude)

Syntax of abs() Function

abs(value)

Parameters (value)

  • value: This parameter can be an integer, float, or complex number.

Return Value

  • If the input is an integer, the output will be an integer.
  • If the input is a float, the output will be a float.
  • If the input is a complex number, the output will be the magnitude of that complex number.

Examples of Python abs() Function

Example 1: Absolute Value of Integer and Float Numbers

The following example demonstrates how to get the absolute value of an integer and a float:

# Testing abs() for an integer and float

int_num = -25

float_num = -10.50

print(“The absolute value of an integer number is:”, abs(int_num))

print(“The absolute value of a float number is:”, abs(float_num))

Output:

The absolute value of an integer number is: 25

The absolute value of a float number is: 10.5

Example 2: Absolute Value of a Complex Number

To calculate the absolute value (magnitude) of a complex number, you can use the following example:

# Testing abs() for a complex number

complex_num = (3 + 10j)

print(“The magnitude of the complex number is:”, abs(complex_num))

Output:

The magnitude of the complex number is: 10.44030650891055

Key Points to Remember

  • The abs() function is versatile and works efficiently with integers, floats, and complex numbers.
  • It takes only one argument: the value whose absolute value needs to be calculated.
  • The function ensures accurate results while maintaining Python’s simple syntax.

Python Lambda Functions with Examples

Download Python

Leave a Comment

Index