One that is often used in computing work Python 內置函數是 abs() 函數.abs() The function accepts a numeric value as input,and returns the absolute value of that input.Absolute value refers to the non-negative value of a number.因此,例如,-10的絕對值是10.10的絕對值也是10.在本教程中,We'll see how in some small onesPython程序中使用abs()函數的例子.
我們將使用Jupyter Notebook來運行這些例子,Because it's a great tool for quick and easy code testing.第一個例子使用abs()函數,接受整數80的輸入.在輸出中,我們看到80的絕對值是80.
在abs()Example 2 of the function,We can use it againabs()函數.這次我們向 abs() The function is passed a negative integer.This negative integer is -75.在輸出中,我們看到 -75 的絕對值是 75.
Python abs()The third example of is an example of getting the absolute value of temperature.This means that this time we pass in a float value,because it has one decimal place.結果是一樣的,abs()顯示98.6的絕對值是98.6.太棒了.
Maybe you live in a no(很幸運)的地區,There will be cases where the temperature enters a negative value.在這個 Python abs() in the fourth example,We initialize another temperature variable to creepy negative 27.5.Then we pass this negative floating point value to abs()函數.結果是,-27.5的絕對值是27.5.Not very warm though,但還是可以接受的!
Example 5 shows how to use it when doing calculations that require absolute values abs() 函數.This example computes the Manhattan distance between two points,That is, the distance between points measured along a right-angled axis.If taking a grid-like path,The distance to travel from one point to another.讓我們用abs()function to see what it does.
abs()Functions also work with complex numbers.These numbers are such as 5+7j或9-3j.讓我們看一個abs()Examples of how to handle these values.
abs()函數在Pythonis used to get the absolute value of a number.it's for integers、Both floating point and complex numbers are valid,As we have seen in all the examples above.