Python Get the Absolute Value of a Number

Document 对象参考手册Python3 Examples

In Python, getting the absolute value of a number is very simple; you can use the built-inabs()function. This function takes a number as an argument and returns its absolute value.

Example

number = -10
absolute_value = abs(number)
print(absolute_value)

Code Explanation:

  • number = -10: define a variablenumber, and assign it the value-10。
  • absolute_value = abs(number): useabs()function to getnumberthe absolute value, and assign the result to the variableabsolute_value。
  • print(absolute_value): printabsolute_valuethe value of

Output result:

10

Document 对象参考手册Python3 Examples

Other Extensions