python內置函數

經常使用: html

#abs(),all(),any(),bin(),bool(),chr(),dict(),dir(),divmod(),enumerate(),eval(),filter(),float(),
#help(),hex(),id(),input(),int(),len(),list(),map(),max(),min(),oct(),open(),ord(),print(),
#range(),reversed(),round(),set(),sorted(),str(),sum(),tuple(),type(),vars(),zip()

 https://docs.python.org/3.5/library/functions.htmlpython

1.abs()絕對值算法

1 print(abs(-34))    #絕對值
2 #輸出結果:34

2.all()全爲真,則真,不然爲假數組

1 print(all([1,2,4,bool("")]))   #若是不全是真,則爲假
2 #輸出結果:False

3.any()有一個爲真,則真,不然全爲假則假spa

1 print(any([1,2,3,bool("")]))   #全爲假則假,不然爲真
2 #輸出結果:True

4.ascii()調用__repr__()這個方法,返回一個字符串,和repr()同樣code

1 #定義一個類
2 class Foo:
3     def __repr__(self):
4         return "bbbbb"
5 f = Foo()
6 ret = ascii(f)
7 print(ret)
8 
9 #輸出結果:bbbbb

5.bin()轉換數字爲二進制htm

1 print(bin(10))     #轉換爲二進制
2 #執行結果:0b1010

6.bool值,判斷真假blog

1 print(bool(0))     #默認0爲false,其餘數字爲true,
2 輸出結果:False
3 #另外None、空字符串、空元組、空列表、空字典等都是false

7.bytearray()轉換爲字節數組格式排序

1 p = bytearray("搜狗",encoding="utf-8")   #轉爲字節數組
2 print(p)    #一箇中文表示3個字節
3 #輸出結果:bytearray(b'\xe6\x90\x9c\xe7\x8b\x97')

8.bytes()轉換爲字符串格式ip

1 p2 = bytes("aaa搜狗aaa",encoding="utf-8")   #轉爲字符串
2 print(p2)
3 #輸出結果:b'aaa\xe6\x90\x9c\xe7\x8b\x97aaa'

9.callable()判斷是否可執行

 1 f = lambda x:x+1
 2 print(f(5))
 3 print(callable(f))     #callable能檢查到f()是否能執行
 4 li = []
 5 print(callable(li))    #結果爲false,表示li()是不可執行的
 6 
 7 #執行結果:
 8 6
 9 True
10 False

10.chr()把ASCII碼裏的數字轉換爲字符;ord()把ASCII碼裏的字符轉換爲數字

1 print(chr(99))
2 print(ord("h")
3 #輸出結果:
4 c
5 104

 11.enumerate()加編號

1 for i,item in enumerate(li,3):    #初始值3能夠定義
2     print(i,item)
3 #輸出結果:
4 3 sdd
5 4 dff
6 5 dddd

12.eval()能執行字符串裏的算法

1 s = "6*8"
2 print(eval(s))   #eval能運算字符裏的算法
3 #輸出結果:48

13.filter()篩選(數據先後數量變少了),map()過濾(數據先後數量不變)

 1 s1 = [11,22,33,44]
 2 new_s1 = map(lambda x:x+100,s1)   #過濾,每一個元素都會循環
 3 print(list(new_s1))
 4 
 5 def func(x):
 6     if x>33:
 7         return True
 8     else:
 9         return False
10 n = filter(func,s1)     #篩選True的值
11 print(list(n))
12 
13 #執行結果:
14 [111, 122, 133, 144]
15 [44]

14.frozenset()凍結集合,即不能添加修改等

1五、hash()把字符串轉換爲hash值存放,這樣子能夠省空間了

1六、max()拿到一組數據中的最大值,min()拿到一組數據中的最大值

17.oct()轉換爲八進制

1 print(oct(100))      #八進制
2 #輸出結果:0o144

1八、round()四捨五入

1 print(round(8.4))
2 #輸出結果:8

19.sorted()排序

20.sum()求和

21.dir()返回的是key,var()返回的是全部

22.zip()一一對應

x = [1,2,3]
y = ["h","i","j"]
zipped = zip(x,y)     #一一對應
print(list(zipped))

#執行結果:
[(1, 'h'), (2, 'i'), (3, 'j')]
相關文章
相關標籤/搜索