python def函數,python筆記——常用的內置函數

 2023-12-06 阅读 25 评论 0

摘要:官方說明文檔:https://docs.python.org/3/library/functions.html 內置函數 abs()  函數返回(數字)的絕對值 all()  如果()里的所有元素不為0、''、False,all()返回True,否則返回False >>> all(['a', 'b

官方說明文檔: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

轉載于:https://www.cnblogs.com/MT-IT/p/10882583.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/190540.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息