Python學習教程:Python 内置函數最全彙總(一)
1 abs()
絕對值或複數的模
In [1]: abs(-6)Out[1]: 6
2 all()
接受一個疊代器,如果疊代器的所有元素都為真,那麼傳回True,否則傳回False
In [2]: all([1,0,3,6])Out[2]: FalseIn [3]: all([1,2,3])Out[3]: True
3 any()
接受一個疊代器,如果疊代器裡有一個元素為真,那麼傳回True,否則傳回False
In [4]: any([0,0,0,[]])Out[4]: FalseIn [5]: any([0,0,1])Out[5]: True
4 ascii()
調用對象的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()
将十進制轉換為二進制
In [35]: bin(10)Out[35]: '0b1010'
6 oct()
将十進制轉換為八進制
In [36]: oct(9)Out[36]: '0o11'
7 hex()
将十進制轉換為十六進制
In [37]: hex(15)Out[37]: '0xf'
8 bool()
測試一個對象是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()
将一個字元串轉換成位元組類型
In [44]: s = "apple"In [45]: bytes(s,encoding='utf-8')Out[45]: b'apple'
10 str()
将字元類型、數值類型等轉換為字元串類型
In [46]: integ = 100In [47]: str(integ)Out[47]: '100'
11 callable()
判斷對象是否可以被調用,能被調用的對象就是一個callable 對象,比如函數 str, int 等都是可被調用的,但是例子4 中xiaoming這個執行個體是不可被調用的:
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()
檢視十進制整數對應的ASCII字元
In [54]: chr(65)Out[54]: 'A'
13 ord()
檢視某個ascii對應的十進制數
14 classmethod()
classmethod 修飾符對應的函數不需要執行個體化,不需要 self 參數,但第一個參數需要是表示自身類的 cls 參數,可以來調用類的屬性,類的方法,執行個體化對象等。
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 能識别或可以執行的代碼,也可以将文字讀成字元串再編譯。
In [74]: s = "print('helloworld')"In [75]: r = compile(s,"", "exec")In [76]: rOut[76]:
at 0x0000000005DE75D0, file "", line 1>In [77]: exec(r)helloworld
16 complex()
建立一個複數
In [81]: complex(1,2)Out[81]: (1+2j)
17 delattr()
删除對象的屬性
In [87]: delattr(xiaoming,'id')In [88]: hasattr(xiaoming,'id')Out[88]: False
18 dict()
建立資料字典
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()
不帶參數時傳回目前範圍内的變量,方法和定義的類型清單;帶參數時傳回參數的屬性,方法清單。
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()
分别取商和餘數
In [97]: divmod(10,3)Out[97]: (3, 1)
21 enumerate()
傳回一個可以枚舉的對象,該對象的next()方法将傳回一個元組。
In [98]: s = ["a","b","c"] ...: for i ,v in enumerate(s,1): ...: print(i,v) ...:1 a2 b3 c
22 eval()
将字元串str 當成有效的表達式來求值并傳回計算結果取出字元串中内容
In [99]: s = "1 + 3 +5" ...: eval(s) ...:Out[99]: 9
23 exec()
執行字元串或complie方法編譯過的字元串,沒有傳回值
at 0x0000000005DE75D0, file "", line 1>In [77]: exec(r)helloworld
24 filter()
過濾器,構造一個序列,等價于
[ item for item in iterables if function(item)]
在函數中設定過濾條件,逐一循環疊代器中的元素,将傳回值為True時的元素留下,形成一個filter類型資料。
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()
将一個字元串或整數轉換為浮點數
In [103]: float(3)Out[103]: 3.0
26 format()
格式化輸出字元串,format(value, format_spec)實質上是調用了value的format(format_spec)方法。
In [104]: print("i am {0},age{1}".format("tom",18))i am tom,age18
27 frozenset()
建立一個不可修改的集合。
In [105]: frozenset([1,1,3,2,3])Out[105]: frozenset({1, 2, 3})
28 getattr()
擷取對象的屬性
29 globals()
傳回一個描述目前全局變量的字典
30 hasattr()
In [110]: hasattr(xiaoming,'name')Out[110]: TrueIn [111]: hasattr(xiaoming,'id')Out[111]: False
下期的Python學習教程,接着跟大家總結下篇的30篇!願每個學習Python的你,都能學懂。