官方說明文檔:https://docs.python.org/3/library/functions.html
內置函數
abs() 函數返回(數字)的絕對值
all() 如果()里的所有元素不為0、''、False,all()返回True,否則返回False
>>> all(['a', 'b', 'c', 'd']) #列表list,元素都不為空或0 True >>> all(['a', 'b', '', 'd']) #列表list,存在一個為空的元素 False >>> all([0, 1,2, 3]) #列表list,存在一個為0的元素 False >>> all(('a', 'b', 'c', 'd')) #元組tuple,元素都不為空或0 True >>> all(('a', 'b', '', 'd')) #元組tuple,存在一個為空的元素 False >>> all((0, 1,2, 3)) #元組tuple,存在一個為0的元素 False >>> all([]) # 空列表 True >>> all(()) # 空元組 True
python def函數?any() 如果()里的任何元素不為0、''、False,any()返回True。如果iterable為空,返回False
>>> any(['a', 'b', 'c', 'd']) #列表list,元素都不為空或0 True >>> any(['a', 'b', '', 'd']) #列表list,存在一個為空的元素 True >>> any([0, '', False]) #列表list,元素全為0,'',false False >>> any(('a', 'b', 'c', 'd')) #元組tuple,元素都不為空或0 True >>> any(('a', 'b', '', 'd')) #元組tuple,存在一個為空的元素 True >>> any((0, '', False)) #元組tuple,元素全為0,'',false False >>> any([]) # 空列表 False >>> any(()) # 空元組 False
bin() 十進制轉二進制
oct() 十進制轉八進制
hex() 十進制轉十六進制
print(bin(10),oct(10),hex(10))
#0b1010 0o12 0xa
bytes(字符串,編碼格式)
一個字節8位
utf-8 一個漢字3個字節
gbk ? 一個漢字2個字節
n = '測試'
m = bytes(n,encoding="utf-8") m1 = bytes(n,encoding="gbk") print(m,m1) print(str(bytes(n,encoding="utf-8"),encoding="utf-8")) #字節轉化成字符串 #b'\xe6\xb5\x8b\xe8\xaf\x95' b'\xb2\xe2\xca\xd4' #測試
?
Python 函數,format() ? ? ? 格式化輸出
s1 = 'i am {0},age {1}'.format('xx',20) print(s1) s2 = 'i am {0},age {1}'.format(*['xx',21]) print(s2) s3 = 'i am {name},age {age}'.format(name='xx',age=22) print(s3) dic = {'name':'xx','age':23} s4 = 'i am {name},age {age}'.format(**dic) print(s4) #i am xx,age 20 #i am xx,age 21 #i am xx,age 22 #i am xx,age 23