English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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)
The abs() method takes one parameter:
num-The number to return its absolute value. The number can be:
Integer
Floating-point number
Complex number
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
# 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
# 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