round()方法返回 數值 的小數點四捨五入到n個數字。文法以下是round()方法的文法:round( x [, n] )參數 x --這是一個數值,表示需要格式化的數值 n --這也是一個數值,表示小數點後保留多少位傳回值該方法返回 數值x 的小數點四捨五入到n個數字PS: round 對傳入的資料進行四捨五入,如果ngigits不傳,預設是0(就是說保留整數部分).ngigits例子下面的例子顯示了round()方法的使用#!/usr/bin/python2print
從今天開始,我會每天更新一個python函數,並附帶執行個體及函數使用時的注意點。我給這個函數專題起了個名字叫“python函數每日一講”,希望大家支援。呵呵下面開始,今天的函數是abs()先看官方英文文檔解釋abs(x)Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a
all(iterable)版本:該函數在python2.5版本首次出現,適用於2.5以上版本,包括python3,相容python3版本。說明:如果iterable的所有元素不為0、''、False或者iterable為空白,all(iterable)返回True,否則返回False;函數等價於:def all(iterable): for element in iterable: if not element: return
any(iterable)版本:該函數適用於2.5以上版本,相容python3版本。說明:如果iterable的任何元素不為0、''、False,all(iterable)返回True。如果iterable為空白,返回False。函數等價於:注意比較該函數與all()函數的區別,any是任意,而all是全部。建議比較學習兩者的區別與聯絡。可以參考《python函數每日一講 - all()》def any(iterable): for element in iterable:
《python函數每日一講》專題上周一直在和大家分享python的內建函數,都是按照字母排序來講解的,但是上周講解的4個函數都是不太常用的,今天我很高興,呵呵,因為今天我要和大家分享一個很常用的函數:bin()函數bin(x)英文說明:Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to
bool([x])英文說明:Convert a value to a Boolean, using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is a subclass of int. Class bool cannot be subclassed
callable(object)中文說明:檢查對象object是否可調用。如果返回True,object仍然可能調用失敗;但如果返回False,調用對象ojbect絕對不會成功。注意:類是可調用的,而類的執行個體實現了__call__()方法才可調用。版本:該函數在python2.x版本中都可用。但是在python3.0版本中被移除,而在python3.2以後版本中被重新添加。英文說明:Return True if the object argument appears callable,
chr(i)中文說明:返回整數i對應的ASCII字元。與ord()作用相反。參數x:取值範圍[0, 255]之間的正數。版本:該函數在python2和python3各個版本中都可用。不存在相容性問題。英文說明:Return a string of one character whose ASCII code is the integer i. For example, chr(97) returns the string 'a'. This is the inverse of
今天來分享一個python中非常有用而且非常簡單的函數cmp,從拼字上大家可能猜到了,這是一個比較函數,嗯,是的,它的傳回值只有三個,正數,0,負數,下面來看一下:cmp(x, y)中文說明:比較兩個對象x和y,如果x y,返回正數。版本:該函數只有在python2中可用,而且在python2所有版本中都可用。但是在python3中該函數已經被刪減掉,這點要特別注意。英文說明:Compare the two objects x and y and return an integer
complex([real[, imag]])中文說明:建立一個值為real + imag * j的複數或者轉化一個字串或數為複數。如果第一個參數為字串,則不需要指定第二個參數。參數real: int, long, float或字串;參數imag: int, long, float。英文說明:Create a complex number with the value real + imag*j or convert a string or number to a complex number.
delattr(object, name)中文說明:刪除object對象名為name的屬性。這個函數的命名真是簡單易懂啊,和jquery裡面差不多,但是功能不一樣哦,注意一下。參數object:對象。參數name:屬性名稱字串。版本:各版本中都支援該函數,python3中仍可用。英文說明:This is a relative of setattr(). The arguments are an object and a string. The string must be the name
python每日一函數 - divmod數文書處理函數divmod(a,b)函數中文說明:divmod(a,b)方法返回的是a//b(除法取整)以及a對b的餘數返回結果類型為tuple參數:a,b可以為數字(包括複數)版本:在python2.3版本之前不允許處理複數,這個大家要注意一下英文說明:Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient
id(object)功能:返回的是對象的“社會安全號碼”,唯一且不變,但在不重合的生命週期裡,可能會出現相同的id值。此處所說的對象應該特指複合類型的對象(如類、list等),對於字串、整數等類型,變數的id是隨值的改變而改變的。Python版本: Python2.x Python3.xPython英文官方文檔解釋:Return the “identity” of an object. This is an integer (or long integer) which is