本篇文章為大家展示了Python內(nèi)置函數(shù)都有哪些,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站專注于鄲城網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供鄲城營(yíng)銷型網(wǎng)站建設(shè),鄲城網(wǎng)站制作、鄲城網(wǎng)頁設(shè)計(jì)、鄲城網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造鄲城網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供鄲城網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
Python 內(nèi)置函數(shù)最全匯總
1 abs()
絕對(duì)值或復(fù)數(shù)的模
In [1]: abs(-6)Out[1]: 6
2 all()
接受一個(gè)迭代器,如果迭代器的所有元素都為真,那么返回True,否則返回False
In [2]: all([1,0,3,6])Out[2]: FalseIn [3]: all([1,2,3])Out[3]: True
3 any()
接受一個(gè)迭代器,如果迭代器里有一個(gè)元素為真,那么返回True,否則返回False
In [4]: any([0,0,0,[]])Out[4]: FalseIn [5]: any([0,0,1])Out[5]: True
4 ascii()
調(diào)用對(duì)象的repr() 方法,獲得該方法的返回值
In [30]: class Student(): ...: def __init__(self,id,name): ...: self.id = id ...: self.name = name ...: def __repr__(self): ...: return \'id = \'+self.id +\', name = \'+self.nameIn [33]: print(xiaoming)id = 001, name = xiaomingIn [34]: ascii(xiaoming)Out[34]: \'id = 001, name = xiaoming\'
5 bin()
將十進(jìn)制轉(zhuǎn)換為二進(jìn)制
In [35]: bin(10)Out[35]: \'0b1010\'
6 oct()
將十進(jìn)制轉(zhuǎn)換為八進(jìn)制
In [36]: oct(9)Out[36]: \'0o11\'
7 hex()
將十進(jìn)制轉(zhuǎn)換為十六進(jìn)制
In [37]: hex(15)Out[37]: \'0xf\'
8 bool()
測(cè)試一個(gè)對(duì)象是True, 還是False.
In [38]: bool([0,0,0])Out[38]: TrueIn [39]: bool([])Out[39]: FalseIn [40]: bool([1,0,1])Out[40]: True
9 bytes()
將一個(gè)字符串轉(zhuǎn)換成字節(jié)類型
In [44]: s = "apple"In [45]: bytes(s,encoding=\'utf-8\')Out[45]: b\'apple\'
10 str()
將字符類型、數(shù)值類型等轉(zhuǎn)換為字符串類型
In [46]: integ = 100In [47]: str(integ)Out[47]: \'100\'
11 callable()
判斷對(duì)象是否可以被調(diào)用,能被調(diào)用的對(duì)象就是一個(gè)callable 對(duì)象,比如函數(shù) str, int 等都是可被調(diào)用的,但是例子4 中xiaoming這個(gè)實(shí)例是不可被調(diào)用的:
In [48]: callable(str)Out[48]: TrueIn [49]: callable(int)Out[49]: TrueIn [50]: xiaomingOut[50]: id = 001, name = xiaomingIn [51]: callable(xiaoming)Out[51]: False
12 chr()
查看十進(jìn)制整數(shù)對(duì)應(yīng)的ASCII字符
In [54]: chr(65)Out[54]: \'A\'
13 ord()
查看某個(gè)ascii對(duì)應(yīng)的十進(jìn)制數(shù)
14 classmethod()
classmethod 修飾符對(duì)應(yīng)的函數(shù)不需要實(shí)例化,不需要 self 參數(shù),但第一個(gè)參數(shù)需要是表示自身類的 cls 參數(shù),可以來調(diào)用類的屬性,類的方法,實(shí)例化對(duì)象等。
In [66]: class Student(): ...: def __init__(self,id,name): ...: self.id = id ...: self.name = name ...: def __repr__(self): ...: return \'id = \'+self.id +\', name = \'+self.name ...: @classmethod ...: def f(cls): ...: print(cls)
15 complie()
將字符串編譯成python 能識(shí)別或可以執(zhí)行的代碼,也可以將文字讀成字符串再編譯。
In [74]: s = "print(\'helloworld\')"In [75]: r = compile(s,"<string>", "exec")In [76]: rOut[76]: <code object <module> at 0x0000000005DE75D0, file "<string>", line 1>In [77]: exec(r)helloworld
16 complex()
創(chuàng)建一個(gè)復(fù)數(shù)
In [81]: complex(1,2)Out[81]: (1+2j)
17 delattr()
刪除對(duì)象的屬性
In [87]: delattr(xiaoming,\'id\')In [88]: hasattr(xiaoming,\'id\')Out[88]: False
18 dict()
創(chuàng)建數(shù)據(jù)字典
In [92]: dict()Out[92]: {}In [93]: dict(a=\'a\',b=\'b\')Out[93]: {\'a\': \'a\', \'b\': \'b\'}In [94]: dict(zip([\'a\',\'b\'],[1,2]))Out[94]: {\'a\': 1, \'b\': 2}In [95]: dict([(\'a\',1),(\'b\',2)])Out[95]: {\'a\': 1, \'b\': 2}
19 dir()
不帶參數(shù)時(shí)返回當(dāng)前范圍內(nèi)的變量,方法和定義的類型列表;帶參數(shù)時(shí)返回參數(shù)的屬性,方法列表。
In [96]: dir(xiaoming)Out[96]:[\'__class__\', \'__delattr__\', \'__dict__\', \'__dir__\', \'__doc__\', \'__eq__\', \'__format__\', \'__ge__\', \'__getattribute__\', \'__gt__\', \'__hash__\', \'__init__\', \'__init_subclass__\', \'__le__\', \'__lt__\', \'__module__\', \'__ne__\', \'__new__\', \'__reduce__\', \'__reduce_ex__\', \'__repr__\', \'__setattr__\', \'__sizeof__\', \'__str__\', \'__subclasshook__\', \'__weakref__\', \'name\']
20 divmod()
分別取商和余數(shù)
In [97]: divmod(10,3)Out[97]: (3, 1)
21 enumerate()
返回一個(gè)可以枚舉的對(duì)象,該對(duì)象的next()方法將返回一個(gè)元組。
In [98]: s = ["a","b","c"] ...: for i ,v in enumerate(s,1): ...: print(i,v) ...:1 a2 b3 c
22 eval()
將字符串str 當(dāng)成有效的表達(dá)式來求值并返回計(jì)算結(jié)果取出字符串中內(nèi)容
In [99]: s = "1 + 3 +5" ...: eval(s) ...:Out[99]: 9
23 exec()
執(zhí)行字符串或complie方法編譯過的字符串,沒有返回值
In [74]: s = "print(\'helloworld\')"In [75]: r = compile(s,"<string>", "exec")In [76]: rOut[76]: <code object <module> at 0x0000000005DE75D0, file "<string>", line 1>In [77]: exec(r)helloworld
24 filter()
過濾器,構(gòu)造一個(gè)序列,等價(jià)于
[ item for item in iterables if function(item)]
在函數(shù)中設(shè)定過濾條件,逐一循環(huán)迭代器中的元素,將返回值為True時(shí)的元素留下,形成一個(gè)filter類型數(shù)據(jù)。
In [101]: fil = filter(lambda x: x>10,[1,11,2,45,7,6,13])In [102]: list(fil)Out[102]: [11, 45, 13]
25 float()
將一個(gè)字符串或整數(shù)轉(zhuǎn)換為浮點(diǎn)數(shù)
In [103]: float(3)Out[103]: 3.0
26 format()
格式化輸出字符串,format(value, format_spec)實(shí)質(zhì)上是調(diào)用了value的format(format_spec)方法。
In [104]: print("i am {0},age{1}".format("tom",18))i am tom,age18
27 frozenset()
創(chuàng)建一個(gè)不可修改的集合。
In [105]: frozenset([1,1,3,2,3])Out[105]: frozenset({1, 2, 3})
28 getattr()
獲取對(duì)象的屬性
29 globals()
返回一個(gè)描述當(dāng)前全局變量的字典
30 hasattr()
In [110]: hasattr(xiaoming,\'name\')Out[110]: TrueIn [111]: hasattr(xiaoming,\'id\')Out[111]: False
上述內(nèi)容就是Python內(nèi)置函數(shù)都有哪些,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁標(biāo)題:Python內(nèi)置函數(shù)都有哪些
文章位置:http://chinadenli.net/article16/pijsgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、商城網(wǎng)站、用戶體驗(yàn)、品牌網(wǎng)站設(shè)計(jì)、微信小程序、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)