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

Python basic tutorial

Python flow control

Funções no Python

Tipos de Dados do Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python abs() usage and example

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: 'abs(integer))
# Random floating-point number
floating = -30.33
print('-30.33The absolute value is: 'abs(floating))

When running the program, the output 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))

When running the program, the output is:

3 - 4The size of j is: 5.0

Python built-in functions