English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Fonction en Python

Types de données en Python

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python abs() usage and examples

Python built-in functions

The abs() method returns the absolute value of the given number. If it is a complex number, then abs() returns its magnitude.

The syntax of the abs() method is:

abs(num)

abs() parameter

The abs() method takes one parameter:

  • num-The number to return its absolute value. The number can be:

    1. Integer

    2. Floating-point number

    3. Complex number

From the value returned by abs()

The abs() method returns the absolute value of the given number.

  • For integers-Return the absolute value of an integer

  • For floating-point numbers-Return the absolute value of a floating-point number

  • For complex numbers-Return the size of the number

Example1: Get the absolute value of a number

# Random integer
integer = -20
print('-2The absolute value of 0 is:
# Random floating-point number
floating = -30.33
print('-30.33The absolute value is:

The output when running the program is:

-2The absolute value of 0 is: 20
-30.33The absolute value is: 30.33

Example2: Get the magnitude of a complex number

# Random complex number
complex = (3 - 4j)
print('3 - 4The size of j is:', abs(complex))

The output when running the program is:

3 - 4The size of j is: 5.0

Python built-in functions