Python Get the Absolute Value of a Number
In Python, getting the absolute value of a number is very simple: you can use the built-in abs() function. This function takes a number as its argument and returns its absolute value.
Example
number
=
10
absolute_value
=
abs
(
number
)
(
absolute_value
)
Code analysis:
number = -10: define a variablenumberand assign it the value-10。absolute_value = abs(number): When usingabs()function to getnumber's absolute value, and assign the result to the variableabsolute_value。print(absolute_value): printabsolute_valuevalue.
Output result:
10
Python3 Examples