Three ways to find the absolute value of python

created at 07-01-2021 views: 4

Three ways to find the absolute value of python

  1. Conditional judgment
  2. Built-in function abs()
  3. Built-in module math.fabs
import math

# Method 1, Keywords: Conditional judgment
def abs_value1():
    a = int(input('please enter a number:'))
    if a > 0:

# Method 2, Keywords: Built-in function
def abs_value2():
    a = int(input('please input-number:'))
    b = abs(a)

# Method 3, Keywords: Built-in module
def abs_value3():
    a = int(input('Please enter a number:'))
    b = math.fabs(a)

The difference between abs() and fabs()

  • abs() is a built-in function, and fabs() is defined in the math module.
  • The fabs() function is only suitable for float and integer types, and abs() is also suitable for complex numbers.
  • abs() returns float and int types, math.fabs() returns float types
created at:07-01-2021
edited at: 07-01-2021: