Python provides the abs()
function in order to calculate and return the absolute value of the given number. Absolute value is used in mathematics in order to make the calculation easier. The given value can be different types of a number of presentations like float, complex, negative, hexadecimal, etc.
Python提供了abs()
函数,以便计算并返回给定数字的绝对值。 在数学中使用绝对值是为了使计算更容易。 给定值可以是许多表示形式的不同类型,例如浮点数,复数,负数,十六进制等。
abs()函数语法 (abs() Function Syntax)
abs() function has very simple syntax where only a single argument is accepted in order to calculate its absolute value.
abs()函数的语法非常简单,其中仅接受单个参数以计算其绝对值。
abs(NUMBER)
- `abs` is the function name that will return the absolute value of the given NUMBER. “ abs”是函数名称,它将返回给定NUMBER的绝对值。
- `NUMBER`is the number we want to calculate its absolute value. NUMBER can be floating-point, negative numbers, complex numbers, hexadecimal, binary, etc. “ NUMBER”是我们要计算其绝对值的数字。 NUMBER可以是浮点数,负数,复数,十六进制,二进制等。
整数的绝对值 (Absolute Value For A Integer)
We will start with a simple example where we will calculate the absolute value of an integer.
我们将从一个简单的示例开始,在该示例中,我们将计算整数的绝对值。
abs(5)
//Equal to the 5
abs(55)
//Equal to the 55
abs(550)
//Equal to the 550
abs(0)
//Equal to the 0
abs(-5)
//Equal to the 5
abs(-55)
//Equal to the 55
abs(-550)
//Equal to the 550
abs(-999)
//Equal to the 999
We can see from the examples that an integer will be converted into an integer as an absolute value. The positive integers will be converted to the same value as an absolute value. The negative integers will be converted to the positive same numbers as integers. 55
absolute value is 55
and -55
absolute value is 55
too.
从示例中我们可以看到,整数将转换为整数作为绝对值。 正整数将转换为与绝对值相同的值。 负整数将转换为与整数相同的正数。 55
绝对值是55
, -55
绝对值也是55
。
浮点数的绝对值 (Absolute Value For A Floating Point)
One of the most used scenarios for the abs() function or absolute value is for floating-point numbers.
abs()函数或绝对值最常用的方案之一是浮点数。
abs(0.5)
//Equal to the 0.5
abs(1.5)
//Equal to the 1.5
abs(-1.5)
//Equal to the 1.5
abs(-0.5)
//Equal to the 0.5
abs(-100.9)
//Equal to the 100.9
abs(100.9)
//Equal to the 100.9
复数的绝对值(Absolute Value For A Complex Number)
abs() function also can be used for complex numbers. We will provide different complex numbers in these examples.
abs()函数也可用于复数。 在这些示例中,我们将提供不同的复数。
abs(5-4j)
//Equal to the 6.4031242374328485
abs(30-4j)
//Equal to the 30.265491900843113
abs(300-4j)
//Equal to the 300.0266654815868
abs(31-4j)
//Equal to the 31.25699921617557
abs(1-4j)
//Equal to the 4.123105625617661
abs(2-4j)
//Equal to the 4.47213595499958
abs(10-40j)
//Equal to the 41.23105625617661
二进制数的绝对值(Absolute Value For A Binary Number)
Binary numbers can be used for absolute calculation like below.
二进制数可用于绝对计算,如下所示。
abs(0b1101101)
//Equal to the 109
abs(0b110110)
//Equal to the 54
abs(0b11011)
//Equal to the 27
abs(0b1101)
//Equal to the 13
abs(0b110)
//Equal to the 6
abs(0b11)
//Equal to the 3
abs(0b1)
//Equal to the 1
八进制的绝对值(Absolute Value For Octal)
We can calculate the absolute values of the octal numbers like below.
我们可以如下计算八进制数的绝对值。
abs(0o11011010)
//Equal to the 2363912
abs(0o11011014)
//Equal to the 2363916
abs(0o1102014)
//Equal to the 295948
abs(0o1152014)
//Equal to the 316428
abs(0o152014)
//Equal to the 54284
abs(0o152614)
//Equal to the 54668
abs(0o15267)
//Equal to the 6839
十六进制的绝对值(Absolute Value For Hexadecimal)
We can also use abs() function in order to calculate hexadecimal values.
我们还可以使用abs()函数来计算十六进制值。
abs(0x23ADF042)
//Equal to the598601794
abs(0x23ADF04)
//Equal to the37412612
abs(0x23ADF0)
//Equal to the2338288
abs(0x23ADF)
//Equal to the146143
abs(0x23AD)
//Equal to the9133
abs(0x23A)
//Equal to the570
abs(0x23)
//Equal to the35
abs(0x2)
//Equal to the2
清单项目的绝对值(Absolute Value For The List Items)
Python is a practical language where we can calculate the absolute values of the given list items. We will use the map()
function and provide the abs() function with the list.
Python是一种实用的语言,我们可以在其中计算给定列表项的绝对值。 我们将使用map()
函数,并为abs()函数提供列表。
numbers=[10,15,20,-10,-15-20,0.5,-0.5]
numbers_obsolute = map(abs,numbers)
print(list(numbers_obsolute))
[10, 15, 20, 10, 35, 0.5, 0.5]
翻译自: https://www.poftut.com/python-abs-function-for-absolute-value-tutorial-with-examples/