內(nèi)置函數(shù)就是Python給你提供的,拿來(lái)直接用的函數(shù),比如print.,input等。
截止到python版本3.6.2,python一共提供了68個(gè)內(nèi)置函數(shù),具體如下????
abs()dict()help()min()setattr()
all()dir()hex()next()slice()
any()divmod()id()object()sorted()
ascii()enumerate()input()oct()staticmethod()
bin()eval()int()open()str()bool()exec()isinstance()ord()sum()
bytearray()?lter()issubclass()pow()super()
bytes()?oat()iter()print()tuple()
callable()format()len()property()type()
chr()frozenset()list()range()vars()
classmethod()getattr()locals()repr()zip()
compile()globals()map()reversed()__import__()complex()hasattr()max()round()
delattr()hash()memoryview()set()
本文將這68個(gè)內(nèi)置函數(shù)綜合整理為12大類(lèi),正在學(xué)習(xí)Python基礎(chǔ)的讀者一定不要錯(cuò)過(guò),建議收藏學(xué)習(xí)!
和數(shù)字相關(guān)
1.數(shù)據(jù)類(lèi)型
bool:布爾型(True,False)
int:整型(整數(shù))
float:浮點(diǎn)型(小數(shù))
complex:復(fù)數(shù)
2.進(jìn)制轉(zhuǎn)換
bin()將給的參數(shù)轉(zhuǎn)換成二進(jìn)制
otc()將給的參數(shù)轉(zhuǎn)換成八進(jìn)制
hex()將給的參數(shù)轉(zhuǎn)換成十六進(jìn)制
print(bin(10))#二進(jìn)制:0b1010print(hex(10))#十六進(jìn)制:0xaprint(oct(10))#八進(jìn)制:0o12
3.數(shù)學(xué)運(yùn)算
abs()返回絕對(duì)值
divmode()返回商和余數(shù)
round()四舍五入
pow(a,b)求a的b次冪,如果有三個(gè)參數(shù).則求完次冪后對(duì)第三個(gè)數(shù)取余
sum()求和
min()求最小值
max()求最大值
print(abs(-2))#絕對(duì)值:2print(divmod(20,3))#求商和余數(shù):(6,2)print(round(4.50))#五舍六入:4print(round(4.51))#5print(pow(10,2,3))#如果給了第三個(gè)參數(shù).表示最后取余:1print(sum([1,2,3,4,5,6,7,8,9,10]))#求和:55print(min(5,3,9,12,7,2))#求最小值:2print(max(7,3,15,9,4,13))#求最大值:15
和數(shù)據(jù)結(jié)構(gòu)相關(guān)
1.序列
(1)列表和元組
list()將一個(gè)可迭代對(duì)象轉(zhuǎn)換成列表
tuple()將一個(gè)可迭代對(duì)象轉(zhuǎn)換成元組
print(list((1,2,3,4,5,6)))#[1,2,3,4,5,6]print(tuple([1,2,3,4,5,6]))#(1,2,3,4,5,6)
(2)相關(guān)內(nèi)置函數(shù)
reversed()將一個(gè)序列翻轉(zhuǎn),返回翻轉(zhuǎn)序列的迭代器
slice()列表的切片
lst="你好啊"it=reversed(lst)#不會(huì)改變?cè)斜?返回一個(gè)迭代器,設(shè)計(jì)上的一個(gè)規(guī)則print(list(it))#['啊','好','你']
lst=[1,2,3,4,5,6,7]print(lst[1:3:1])#[2,3]
s=slice(1,3,1)#切片用的print(lst[s])#[2,3]
(3)字符串
str()將數(shù)據(jù)轉(zhuǎn)化成字符串
print(str(123)+'456')#123456format()與具體數(shù)據(jù)相關(guān),用于計(jì)算各種小數(shù),精算等.
s="helloworld!"print(format(s,"^20"))#劇中print(format(s,"<20"))#左對(duì)齊print(format(s,">20"))#右對(duì)齊
#helloworld!
#helloworld!
#helloworld!print(format(3,'b'))#二進(jìn)制:11print(format(97,'c'))#轉(zhuǎn)換成unicode字符:aprint(format(11,'d'))#?進(jìn)制:11print(format(11,'o'))#八進(jìn)制:13print(format(11,'x'))#十六進(jìn)制(?寫(xiě)字母):bprint(format(11,'X'))#十六進(jìn)制(大寫(xiě)字母):Bprint(format(11,'n'))#和d?樣:11print(format(11))#和d?樣:11print(format(123456789,'e'))#科學(xué)計(jì)數(shù)法.默認(rèn)保留6位小數(shù):1.234568e+08print(format(123456789,'0.2e'))#科學(xué)計(jì)數(shù)法.保留2位小數(shù)(小寫(xiě)):1.23e+08print(format(123456789,'0.2E'))#科學(xué)計(jì)數(shù)法.保留2位小數(shù)(大寫(xiě)):1.23E+08print(format(1.23456789,'f'))#小數(shù)點(diǎn)計(jì)數(shù)法.保留6位小數(shù):1.234568print(format(1.23456789,'0.2f'))#小數(shù)點(diǎn)計(jì)數(shù)法.保留2位小數(shù):1.23print(format(1.23456789,'0.10f'))#小數(shù)點(diǎn)計(jì)數(shù)法.保留10位小數(shù):1.2345678900print(format(1.23456789e+3,'F'))#小數(shù)點(diǎn)計(jì)數(shù)法.很大的時(shí)候輸出INF:1234.567890
bytes()把字符串轉(zhuǎn)化成bytes類(lèi)型
bs=bytes("今天吃飯了嗎",encoding="utf-8")print(bs)#b'\xe4\xbb\x8a\xe5\xa4\xa9\xe5\x90\x83\xe9\xa5\xad\xe4\xba\x86\xe5\x90\x97'bytearray()返回一個(gè)新字節(jié)數(shù)組.這個(gè)數(shù)字的元素是可變的,并且每個(gè)元素的值得范圍是[0,256)
ret=bytearray("alex",encoding='utf-8')print(ret[0])#97print(ret)#bytearray(b'alex')
ret[0]=65#把65的位置A賦值給ret[0]print(str(ret))#bytearray(b'Alex')
ord()輸入字符找?guī)ё址幋a的位置
chr()輸入位置數(shù)字找出對(duì)應(yīng)的字符
ascii()是ascii碼中的返回該值不是就返回u
print(ord('a'))#字母a在編碼表中的碼位:97print(ord('中'))#'中'字在編碼表中的位置:20013print(chr(65))#已知碼位,求字符是什么:Aprint(chr(19999))#丟foriinrange(65536):#打印出0到65535的字符
print(chr(i),end="")print(ascii("@"))#'@'
repr()返回一個(gè)對(duì)象的string形式
s="今天\n吃了%s頓\t飯"%3print(s)#今天#吃了3頓飯print(repr(s))#原樣輸出,過(guò)濾掉轉(zhuǎn)義字符\n\t\r不管百分號(hào)%
#'今天\n吃了3頓\t飯'
2.數(shù)據(jù)集合
字典:dict創(chuàng)建一個(gè)字典
集合:set創(chuàng)建一個(gè)集合
frozenset()創(chuàng)建一個(gè)凍結(jié)的集合,凍結(jié)的集合不能進(jìn)行添加和刪除操作。
3.相關(guān)內(nèi)置函數(shù)
len()返回一個(gè)對(duì)象中的元素的個(gè)數(shù)
sorted()對(duì)可迭代對(duì)象進(jìn)行排序操作(lamda)
語(yǔ)法:sorted(Iterable,key=函數(shù)(排序規(guī)則),reverse=False)
Iterable:可迭代對(duì)象
key:排序規(guī)則(排序函數(shù)),在sorted內(nèi)部會(huì)將可迭代對(duì)象中的每一個(gè)元素傳遞給這個(gè)函數(shù)的參數(shù).根據(jù)函數(shù)運(yùn)算的結(jié)果進(jìn)行排序
reverse:是否是倒敘.True:倒敘,False:正序
以上內(nèi)容為大家介紹了68個(gè)Python內(nèi)置函數(shù)詳解,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。http://www.dietsnews.net/