[ Python - 2 ] 常見內置函數
1. abs(): 絕對值
In [1]: abs(-10)
Out[1]: 10
View Code
2. all(): 當參數中任何一個值為False時,all() 都為False
all(iterable) --> bool
bool值為False:0、None、()、‘‘、{}、[]
In [2]: all([‘1‘,‘abc‘, None]) Out[2]: FalseView Code
3. any(): 和all()相反,當參數中任何一個值為True時,all() 都為True
In [3]: any([‘1‘,‘abc‘, None]) Out[View Code3]: True
4. bin(): 將整數x轉換為二進制字符串
In [4]: bin(100) Out[4]: ‘0b1100100‘View Code
5. bool(): 判斷值為True or False
In [5]: bool(123)
Out[5]: True
View Code
6. type(): 講值轉換為字符類型
In [6]: i = bytes(‘123‘, encoding=‘utf-8‘) In [7]: i Out[7]: b‘123‘ In [8]: type(i) Out[8]: bytesView Code
7. chr(): 返回整數i對應的ASCII字符。
In [9]: chr(97) Out[9]: ‘a‘View Code
8. ord(): 與chr()相反,返回ASCII字符對應的整數i
In [12]: ord(‘a‘) Out[12]: 97View Code
9. dict(): 定義字典類型
In [13]: dict()
Out[13]: {}
View Code
10. dir(): 返回當前範圍內的變量、方法和定義的類型列表
In [14]: s = ‘abc‘ In [15]: dir(s) Out[15]: [‘__add__View Code‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ... ...
11. divmod(): divmod(a,b)方法返回的是a//b(除法取整)以及a對b的余數,返回結果類型為tuple,多用於分頁
In [16]: divmod(9,2)
Out[16]: (4, 1)
View Code
12. enumerate(): 對於一個iterable, enumerate將其組成一個索引序列,利用它可以同時獲得索引和值
In [17]: d = {1:‘aa‘, 2:‘bb‘, 3:‘cc‘} In [18]: for index, item in enumerate(d): ...: print(index,item) ...: 0 1 1 2 2 3View Code
13. eval(): 將字符串str當成有效的表達式來求值並返回計算結果。
In [19]: eval(‘1+2+3+4+5‘) Out[19]: 15View Code
14. filter(): 包括兩個參數,分別是function和iterable, 該函數根據function參數返回的結果是否為真來過濾list參數中的項,最後返回一個新列表
In [20]: f = filter(lambda x:x > 2, [1,2,3,4,5]) In [21]: print(list(f)) [3, 4, 5]View Code
15. float(): 浮點數類型
In [22]: float(100)
Out[22]: 100.0
View Code
16. globals(): 在當前作用域下,查看全局變量
In [23]: s = ‘abc‘ In [24]: globals() ... ‘quit‘: <IPython.core.autocall.ExitAutocall at 0x7f58ca9f5710>, ‘s‘: ‘abc‘}View Code
17. help(): 查看幫助
In [25]: help(‘abc‘)View Code
18. hex(): 本函數是轉換一個整數對象為十六進制的字符串
In [26]: hex(123) Out[26]: ‘0x7b‘View Code
19. id(): 返回的是對象的“身份證號”,唯一且不變
In [27]: a = ‘abc‘ In [28]: id(a) Out[28]: 140019516325088View Code
20. input(): 接收用戶輸入
In [32]: i = input(‘enter your number:‘) enter your number:10 In [33]: i Out[33]: ‘10‘View Code
21. int():定義整數類型
In [34]: i = int() In [35]: type(i) Out[35]: intView Code
22. isinstance(object, classinfo): 如果參數object是classinfo的實例,或者object是classinfo類的子類的一個實例, 返回True。如果object不是一個給定類型的的對象, 則返回結果總是False
In [36]: isinstance(‘abc‘, str) Out[36]: True In [37]: isinstance(123, str) Out[37]: FalseView Code
23. len(): 獲取iterable的索引長度
In [39]: len(‘123‘) Out[39]: 3View Code
24. list(): 定義列表類型
In [42]: l = list() In [43]: l Out[43]: [] In [44]: type(l) Out[44]: listView Code
25. locals(): 局部變量函數locals
def f(): x = 1 y = 2 return locals() f1 = f() print(f1)View Code
26. map(function, iterable): 接收一個函數 f 和一個 list,並通過把函數 f 依次作用在 list 的每個元素上,得到一個新的 list 並返回。
def f(i): return i + i l = map(f,[1,2,3,4,5]) print(list(l)) # [2, 4, 6, 8, 10]View Code
27. max(): 卻列表中的最大值
In [46]: max(1,2,3,4,)
Out[46]: 4
View Code
28. min(): 取列表中的最小值
In [47]: min(1,2,3,4)
Out[47]: 1
View Code
29. oct(): 將一個整數轉換成8進制字符串
In [48]: oct(12) Out[48]: ‘0o14‘View Code
30. open(‘path to file’, ‘mode‘): 打開一個文件
f = open(‘D:\\test.txt‘, ‘r‘) r = f.read() print(r) f.close()View Code
31. ord(): 以一個字符作為參數,返回對應的ASCII數值
In [2]: ord(‘a‘) Out[2]: 97View Code
32. pow(x, y): 函數是計算x的y次方.
In [7]: pow(2,4)
Out[7]: 16
View Code
33. print(): 打印
In [8]: print(‘hello‘) helloView Code
34. range(): 在python3中,使用range並不會直接返回一個列表,需要配合list()
In [10]: list(range(0, 10))
Out[10]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
View Code
35. round(): 返回浮點數x的五舍六入值。
In [17]: round(0.5) Out[17]: 0 In [18]: round(0.6) Out[18]: 1View Code
36. set(): 定義set類型,set類型特點:無序, 不重復
In [20]: s = set({1,2,3,4}) In [21]: s Out[21]: {1, 2, 3, 4}View Code
37. sorted(): 用於同一種類型進行排序
In [24]: sorted([1,2,3,4,9,5,6]) Out[24]: [1, 2, 3, 4, 5, 6, 9] In [25]: sorted([‘a‘,‘c‘,‘b‘]) Out[25]: [‘a‘, ‘b‘, ‘c‘]View Code
38. str(): 字符串類型
In [27]: s = str(‘abc‘) In [28]: type(s) Out[28]: strView Code
39. sum(): 可叠代的整數列表求和
In [30]: sum([1,2,3]) Out[30]: 6 In [31]: sum((1,2,3)) Out[31]: 6View Code
40. tuple(): 定義元組類型
In [37]: t = tuple(‘123‘) In [38]: t Out[38]: (‘1‘, ‘2‘, ‘3‘) In [39]: type(t) Out[39]: tupleView Code
41. type() 查看值的類型
In [40]: l = [‘a‘,‘b‘,‘c‘] In [41]: type(l) Out[41]: listView Code
[ Python - 2 ] 常見內置函數