標籤:ipython core index oat 包括 浮點 int() contains 字元
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]: False
View Code
3. any(): 和all()相反,當參數中任何一個值為True時,all() 都為True
In [3]: any([‘1‘,‘abc‘, None])Out[3]: True
View Code
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]: iOut[7]: b‘123‘In [8]: type(i)Out[8]: bytes
View 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]: 97
View Code
9. dict(): 定義字典類型
In [13]: dict()Out[13]: {}View Code
10. dir(): 返回當前範圍內的變數、方法和定義的類型列表
In [14]: s = ‘abc‘In [15]: dir(s)Out[15]: [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘,......
View Code
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 11 22 3View Code
13. eval(): 將字串str當成有效運算式來求值並返回計算結果。
In [19]: eval(‘1+2+3+4+5‘)Out[19]: 15
View 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]: 140019516325088
View Code
20. input(): 接收使用者輸入
In [32]: i = input(‘enter your number:‘)enter your number:10In [33]: iOut[33]: ‘10‘
View Code
21. int():定義整數類型
In [34]: i = int()In [35]: type(i)Out[35]: int
View Code
22. isinstance(object, classinfo): 如果參數object是classinfo的執行個體,或者object是classinfo類的子類的一個執行個體, 返回True。如果object不是一個給定類型的的對象, 則返回結果總是False
In [36]: isinstance(‘abc‘, str)Out[36]: TrueIn [37]: isinstance(123, str)Out[37]: False
View Code
23. len(): 擷取iterable的索引長度
In [39]: len(‘123‘)Out[39]: 3
View Code
24. list(): 定義清單類型
In [42]: l = list()In [43]: lOut[43]: []In [44]: type(l)Out[44]: list
View 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 + il = 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]: 97
View Code
32. pow(x, y): 函數是計算x的y次方.
In [7]: pow(2,4)Out[7]: 16
View Code
33. print(): 列印
In [8]: print(‘hello‘)hello
View 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]: 0In [18]: round(0.6)Out[18]: 1
View Code
36. set(): 定義set類型,set類型特點:無序, 不重複
In [20]: s = set({1,2,3,4})In [21]: sOut[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]: str
View Code
39. sum(): 可迭代的整數列表求和
In [30]: sum([1,2,3])Out[30]: 6In [31]: sum((1,2,3))Out[31]: 6
View Code
40. tuple(): 定義元群組類型
In [37]: t = tuple(‘123‘)In [38]: tOut[38]: (‘1‘, ‘2‘, ‘3‘)In [39]: type(t)Out[39]: tuple
View Code
41. type() 查看值的類型
In [40]: l = [‘a‘,‘b‘,‘c‘]In [41]: type(l)Out[41]: list
View Code
[ Python - 2 ] 常見內建函數