目录
suibi

python绝对值函数怎么表示(如何求某个数的绝对值python方法)


Python 有两种获取数字绝对值的方法:

  • 内置abs()函数返回绝对值。
  • math.fabs()函数还返回浮点绝对值。

abs()函数获取绝对值

内置abs()函数返回绝对值,要使用该函数,只需直接调用:

abs(-23.43)    # 返回: 23.43

Python中整数的绝对值

# Some random integers
variableA = 28
variableB = -101
variableC = 0
variableD = -9116439

# Get the absolute values of those integers
absA = abs(variableA)
absB = abs(variableB)
absC = abs(variableC)
absD = abs(variableD)

# Output the results
print("Absolute values of integers with `abs()`:")
print("|", variableA, "| = ", absA, sep="")
print("|", variableB, "| = ", absB, sep="")
print("|", variableC, "| = ", absC, sep="")
print("|", variableD, "| = ", absD, sep="")

输出:

Absolute values of integers with `abs()`:
|28| = 28
|-101| = 101
|0| = 0
|-9116439| = 9116439

Python中浮点值的绝对值

# Some random floating-point values
variableA = -12.34
variableB = -1.8457425364
variableC = -0.00000000001
variableD = 9424.5895279095

# Get the absolute value of each variable
absVarA = abs(variableA)
absVarB = abs(variableB)
absVarC = abs(variableC)
absVarD = abs(variableD)

# Output the results
print("Absolute values of floating point values with `abs()`:")
print("|", variableA, "| = ", absVarA, sep="")
print("|", variableB, "| = ", absVarB, sep="")
print("|", variableC, "| = ", absVarC, sep="")
print("|", variableD, "| = ", absVarD, sep="")

输出:

|-12.34| = 12.34
|-1.8457425364| = 1.8457425364
|-1e-11| = 1e-11
|9424.5895279095| = 9424.5895279095

math.fabs()函数获取浮点绝对值

除了标准abs()函数之外,Python 还具有math.fabs()函数。它的绝对值作为浮点值返回。功能类似于abs(),但关键区别在于是返回一个浮点值。

import math

math.fabs(-8)    # Returns: 8.0
import math

# Some random values
valueA = -4
valueB = -56
valueC = 26
valueD = -2.992474203

# Get the floating-point absolute value from each
fabs_A = math.fabs(valueA)
fabs_B = math.fabs(valueB)
fabs_C = math.fabs(valueC)
fabs_D = math.fabs(valueD)

# Output the results
print("Absolute floating-point values with `fabs()`:")
print("|", valueA, "| = ", fabs_A, sep="")
print("|", valueB, "| = ", fabs_B, sep="")
print("|", valueC, "| = ", fabs_C, sep="")
print("|", valueD, "| = ", fabs_D, sep="")

输出:

Absolute floating-point values with `fabs()`:
|-4| = 4.0
|-56| = 56.0
|26| = 26.0
|-2.992474203| = 2.992474203

Python 列表的绝对值

# Some random numbers
values = [-40, 58, -69, -84, 51, 76, -12, 36]

# Get the absolute value for each number
absValues = [abs(number) for number in values]

# Output data
print("Original numbers:\n", values)
print("Absolute values:\n", absValues)

在这个程序中,我们首先创建一个列表values。它的值有正整数和负整数。在其方括号 ([和]) 之间,用一个for循环从列表values取值,用abs()函数获取每个number的绝对值。

Original numbers:
 [-40, 58, -69, -84, 51, 76, -12, 36]
Absolute values:
 [40, 58, 69, 84, 51, 76, 12, 36]

Python 循环从列表中获取绝对值

我们除了简单地获取绝对值之外还想进行处理,那么常规for循环通常是更好的选择。假设我们想要获得列表中负值的绝对值,但将正值乘以 2。

# Some positive and negative values
values = [-40, 58, -69, -84, 51, 76, -12, 36]

# Take the absolute value of negative values, but
# multiply positive values with 2
processed = []
for number in values:
    if number < 0:
        processed.append(abs(number))
    else:
        processed.append(number * 2)

# Output data
print("Original numbers:\n", values)
print("Processed numbers:\n", processed)

输出:

Original numbers:
 [-40, 58, -69, -84, 51, 76, -12, 36]
Processed numbers:
 [40, 116, 69, 84, 102, 152, 12, 72]

写笔记

咨询

您的电子邮箱地址不会被公开。