程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Learn the abs() function in Python

編輯:Python

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()函數的例子.


Python abs()例一

我們將使用Jupyter Notebook來運行這些例子,Because it's a great tool for quick and easy code testing.第一個例子使用abs()函數,接受整數80的輸入.在輸出中,我們看到80的絕對值是80.


Python abs()例二

在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() 例三

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.太棒了.


Python abs() 例四

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,但還是可以接受的!


Python abs() 例五

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.


Python abs() 例六

abs()Functions also work with complex numbers.These numbers are such as 5+7j或9-3j.讓我們看一個abs()Examples of how to handle these values.


Python abs()函數總結

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.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved