龙空技术网

如何在Python中获取数字的绝对值?

信息科技云课堂 114

前言:

现在我们对“python求绝对值”大概比较着重,兄弟们都想要知道一些“python求绝对值”的相关知识。那么小编也在网络上网罗了一些对于“python求绝对值””的相关文章,希望小伙伴们能喜欢,大家快快来学习一下吧!

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

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

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

abs(-23.43)    # 返回: 23.43
Python中整数的绝对值
# Some random integersvariableA = 28variableB = -101variableC = 0variableD = -9116439# Get the absolute values of those integersabsA = abs(variableA)absB = abs(variableB)absC = abs(variableC)absD = abs(variableD)# Output the resultsprint("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 valuesvariableA = -12.34variableB = -1.8457425364variableC = -0.00000000001variableD = 9424.5895279095# Get the absolute value of each variableabsVarA = abs(variableA)absVarB = abs(variableB)absVarC = abs(variableC)absVarD = abs(variableD)# Output the resultsprint("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 mathmath.fabs(-8)    # Returns: 8.0
import math# Some random valuesvalueA = -4valueB = -56valueC = 26valueD = -2.992474203# Get the floating-point absolute value from eachfabs_A = math.fabs(valueA)fabs_B = math.fabs(valueB)fabs_C = math.fabs(valueC)fabs_D = math.fabs(valueD)# Output the resultsprint("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 numbersvalues = [-40, 58, -69, -84, 51, 76, -12, 36]# Get the absolute value for each numberabsValues = [abs(number) for number in values]# Output dataprint("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 valuesvalues = [-40, 58, -69, -84, 51, 76, -12, 36]# Take the absolute value of negative values, but# multiply positive values with 2processed = []for number in values:    if number < 0:        processed.append(abs(number))    else:        processed.append(number * 2)# Output dataprint("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]

标签: #python求绝对值