這篇文章主要介紹“怎么使用Python內(nèi)置函數(shù)”,在日常操作中,相信很多人在怎么使用Python內(nèi)置函數(shù)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”怎么使用Python內(nèi)置函數(shù)”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
我們注重客戶提出的每個(gè)要求,我們充分考慮每一個(gè)細(xì)節(jié),我們積極的做好成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)服務(wù),我們努力開拓更好的視野,通過不懈的努力,創(chuàng)新互聯(lián)贏得了業(yè)內(nèi)的良好聲譽(yù),這一切,也不斷的激勵(lì)著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),微信平臺(tái)小程序開發(fā),網(wǎng)站開發(fā),技術(shù)開發(fā)實(shí)力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術(shù)開發(fā)工程師。
返回?cái)?shù)字絕對(duì)值
>>> abs(-100) 100 >>> abs(10) 10 >>>
判斷給定的可迭代參數(shù) iterable 中的所有元素是否都為 TRUE,如果是返回 True,否則返回 False
>>> all([100,100,100]) True >>> all([3,0,1,1]) False >>>
判斷給定的可迭代參數(shù) iterable 是否全部為 False,則返回 False,如果有一個(gè)為 True,則返回 True
>>> any([0,0,0,0]) False >>> any([0,0,0,1]) True >>>
調(diào)用對(duì)象的repr()方法,獲取該方法的返回值
>>> ascii('test') "'test'" >>>
將十進(jìn)制轉(zhuǎn)換為二進(jìn)制
>>> bin(100) '0b1100100' >>>
將十進(jìn)制轉(zhuǎn)換為八進(jìn)制
>>> oct(100) '0o144' >>>
將十進(jìn)制轉(zhuǎn)換為十六進(jìn)制
>>> hex(100) '0x64' >>>
測試對(duì)象是True,還是False
>>> bool(1) True >>> bool(-1) True >>> bool() False >>>
將一個(gè)字符轉(zhuǎn)換為字節(jié)類型
>>> s = "blxt" >>> bytes(s,encoding='utf-8') b'blxt' >>>
將字符、數(shù)值類型轉(zhuǎn)換為字符串類型
>>> str(123) '123' >>>
檢查一個(gè)對(duì)象是否是可調(diào)用的
False >>> callable(str) True >>> callable(int) True >>> callable(0) False >>>
查看十進(jìn)制整數(shù)對(duì)應(yīng)的ASCll字符
>>> chr(100) 'd' >>>
查看某個(gè)ascii對(duì)應(yīng)的十進(jìn)制
>>> ord('a') 97 >>>
修飾符對(duì)應(yīng)的函數(shù)不需要實(shí)例化,不需要 self 參數(shù),但第一個(gè)參數(shù)需要是表示自身類的 cls 參數(shù),可以來調(diào)用類的屬性,類的方法,實(shí)例化對(duì)象等
#!/usr/bin/python # -*- coding: UTF-8 -*- class A(object): bar = 1 def func1(self): print ('foo') @classmethod def func2(cls): print ('func2') print (cls.bar) cls().func1() # 調(diào)用 foo 方法
輸出結(jié)果:
func2 1 foo
將字符串編譯成python能識(shí)別或者可以執(zhí)行的代碼。也可以將文字讀成字符串再編譯
>>> blxt = "print('hello')" >>> test = compile(blxt,'','exec') >>> test <code object <module> at 0x02E9B840, file "", line 1> >>> exec(test) hello >>>
創(chuàng)建一個(gè)復(fù)數(shù)
>>> complex(13,18) (13+18j) >>>
刪除對(duì)象屬性
#!/usr/bin/python # -*- coding: UTF-8 -*- class Coordinate: x = 10 y = -5 z = 0 point1 = Coordinate() print('x = ',point1.x) print('y = ',point1.y) print('z = ',point1.z) delattr(Coordinate, 'z') print('--刪除 z 屬性后--') print('x = ',point1.x) print('y = ',point1.y) # 觸發(fā)錯(cuò)誤 print('z = ',point1.z)
輸出結(jié)果:
>>> x = 10 y = -5 z = 0 --刪除 z 屬性后-- x = 10 y = -5 Traceback (most recent call last): File "C:\Users\fdgh\Desktop\test.py", line 22, in <module> print('z = ',point1.z) AttributeError: 'Coordinate' object has no attribute 'z' >>>
創(chuàng)建數(shù)據(jù)字典
>>> dict() {} >>> dict(a=1,b=2) {'a': 1, 'b': 2} >>>
函數(shù)不帶參數(shù)時(shí),返回當(dāng)前范圍內(nèi)的變量、方法和定義的類型列表
>>> dir() ['Coordinate', '__annotations__', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'point1', 'y'] >>>
分別取商和余數(shù)
>>> divmod(11,2) (5, 1) >>>
返回一個(gè)可以枚舉的對(duì)象,該對(duì)象的next()方法將返回一個(gè)元組
>>> blxt = ['a','b','c','d'] >>> list(enumerate(blxt)) [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')] >>>
將字符串str當(dāng)成有效表達(dá)式來求值并返回計(jì)算結(jié)果取出字符串中內(nèi)容
>>> blxt = "5+1+2" >>> eval(blxt) 8 >>>
執(zhí)行字符串或complie方法編譯過的字符串,沒有返回值
>>> blxt = "print('hello')" >>> test = compile(blxt,'','exec') >>> test <code object <module> at 0x02E9B840, file "", line 1> >>> exec(test) hello >>>
過濾器,構(gòu)建一個(gè)序列
#過濾列表中所有奇數(shù) #!/usr/bin/python # -*- coding: UTF-8 -*- def is_odd(n): return n % 2 == 1 newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) print(newlist)
輸出結(jié)果:
[ 1, 3, 5, 7, 9 ]
將一個(gè)字符串或整數(shù)轉(zhuǎn)換為浮點(diǎn)數(shù)
>>> float(3) 3.0 >>> float(10) 10.0 >>>
格式化輸出字符串
>>> "{0} {1} {3} {2}".format("a","b","c","d") 'a b d c' >>> print("網(wǎng)站名:{name},地址:{url}".format(name="blxt",url="www.blxt.best")) 網(wǎng)站名:blxt,地址:www.blxt.best >>>
創(chuàng)建一個(gè)不可修改的集合
>>> frozenset([2,4,6,6,7,7,8,9,0]) frozenset({0, 2, 4, 6, 7, 8, 9}) >>>
獲取對(duì)象屬性
>>>class A(object): ... bar = 1 ... >>> a = A() >>> getattr(a, 'bar') # 獲取屬性 bar 值 1 >>> getattr(a, 'bar2') # 屬性 bar2 不存在,觸發(fā)異常 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'A' object has no attribute 'bar2' >>> getattr(a, 'bar2', 3) # 屬性 bar2 不存在,但設(shè)置了默認(rèn)值 3 >>>
返回一個(gè)描述當(dāng)前全局變量的字典
>>> print(globals()) # globals 函數(shù)返回一個(gè)全局變量的字典,包括所有導(dǎo)入的變量。 {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, 'a': 'runoob', '__package__': None}
函數(shù)用于判斷對(duì)象是否包含對(duì)應(yīng)的屬性
>>>class A(object): ... bar = 1 ... >>> a = A() >>> hasattr(a,'bar') True >>> hasattr(a,'test') False
返回對(duì)象的哈希值
>>>class A(object): ... bar = 1 ... >>> a = A() >>> hash(a) -2143982521 >>>
返回對(duì)象的幫助文檔
>>>class A(object): ... bar = 1 ... >>> a = A() >>> help(a) Help on A in module __main__ object: class A(builtins.object) | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | bar = 1 >>>
返回對(duì)象的內(nèi)存地址
>>>class A(object): ... bar = 1 ... >>> a = A() >>> id(a) 56018040 >>>
獲取用戶輸入內(nèi)容
>>> input() ... test 'test' >>>
用于將一個(gè)字符串或數(shù)字轉(zhuǎn)換為整型
>>> int('14',16) 20 >>> int('14',8) 12 >>> int('14',10) 14 >>>
來判斷一個(gè)對(duì)象是否是一個(gè)已知的類型,類似 type()
>>> test = 100 >>> isinstance(test,int) True >>> isinstance(test,str) False >>>
用于判斷參數(shù) class 是否是類型參數(shù) classinfo 的子類
#!/usr/bin/python # -*- coding: UTF-8 -*- class A: pass class B(A): pass print(issubclass(B,A)) # 返回 True
返回一個(gè)可迭代對(duì)象,sentinel可省略
>>>lst = [1, 2, 3] >>> for i in iter(lst): ... print(i) ... 1 2 3
返回對(duì)象的長度
>>> dic = {'a':100,'b':200} >>> len(dic) 2 >>>
返回可變序列類型
>>> a = (123,'xyz','zara','abc') >>> list(a) [123, 'xyz', 'zara', 'abc'] >>>
返回一個(gè)將function應(yīng)用于iterable中每一項(xiàng)并輸出其結(jié)果的迭代器
>>>def square(x) : # 計(jì)算平方數(shù) ... return x ** 2 ... >>> map(square, [1,2,3,4,5]) # 計(jì)算列表各個(gè)元素的平方 [1, 4, 9, 16, 25] >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函數(shù) [1, 4, 9, 16, 25] # 提供了兩個(gè)列表,對(duì)相同位置的列表數(shù)據(jù)進(jìn)行相加 >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]) [3, 7, 11, 15, 19]
返回最大值
>>> max (1,2,3,4,5,6,7,8,9) 9 >>>
返回最小值
>>> min (1,2,3,4,5,6,7,8) 1 >>>
返回給定參數(shù)的內(nèi)存查看對(duì)象(memory view)
>>>v = memoryview(bytearray("abcefg", 'utf-8')) >>> print(v[1]) 98 >>> print(v[-1]) 103 >>> print(v[1:4]) <memory at 0x10f543a08> >>> print(v[1:4].tobytes()) b'bce' >>>
返回可迭代對(duì)象的下一個(gè)元素
>>> a = iter([1,2,3,4,5]) >>> next(a) 1 >>> next(a) 2 >>> next(a) 3 >>> next(a) 4 >>> next(a) 5 >>> next(a) Traceback (most recent call last): File "<pyshell#72>", line 1, in <module> next(a) StopIteration >>>
返回一個(gè)沒有特征的新對(duì)象
>>> a = object() >>> type(a) <class 'object'> >>>
返回文件對(duì)象
>>>f = open('test.txt') >>> f.read() '123/123/123'
base為底的exp次冪,如果mod給出,取余
>>> pow (3,1,4) 3 >>>
打印對(duì)象
返回property屬性
class C(object): def __init__(self): self._x = None def getx(self): return self._x def setx(self, value): self._x = value def delx(self): del self._x x = property(getx, setx, delx, "I'm the 'x' property.")
生成一個(gè)不可變序列
>>> range(10) range(0, 10) >>>
返回一個(gè)反向的iterator
>>> a = 'test' >>> a 'test' >>> print(list(reversed(a))) ['t', 's', 'e', 't'] >>>
四舍五入
>>> round (3.33333333,1) 3.3 >>>
返回一個(gè)set對(duì)象,可實(shí)現(xiàn)去重
>>> a = [1,2,3,4,5,5,6,5,4,3,2] >>> set(a) {1, 2, 3, 4, 5, 6} >>>
返回一個(gè)表示有1range所指定的索引集的slice對(duì)象
>>> a = [1,2,3,4,5,5,6,5,4,3,2] >>> a[slice(0,3,1)] [1, 2, 3] >>>
對(duì)所有可迭代的對(duì)象進(jìn)行排序操作
>>> a = [1,2,3,4,5,5,6,5,4,3,2] >>> sorted(a,reverse=True) [6, 5, 5, 5, 4, 4, 3, 3, 2, 2, 1] >>>
將方法轉(zhuǎn)換為靜態(tài)方法
#!/usr/bin/python # -*- coding: UTF-8 -*- class C(object): @staticmethod def f(): print('blxt'); C.f(); # 靜態(tài)方法無需實(shí)例化 cobj = C() cobj.f() # 也可以實(shí)例化后調(diào)用
輸出結(jié)果:
test test
求和
a = [1,2,3,4,5,5,6,5,4,3,2] >>> sum(a) 40 >>>
返回一個(gè)代理對(duì)象
class A: def add(self, x): y = x+1 print(y) class B(A): def add(self, x): super().add(x) b = B() b.add(2) # 3
不可變的序列類型
>>> a = 'www' >>> b =tuple(a) >>> b ('w', 'w', 'w') >>>
將可迭代的對(duì)象作為參數(shù),將對(duì)象中對(duì)應(yīng)的元素打包成一個(gè)個(gè)元組,然后返回由這些元組組成的列表
>>>a = [1,2,3] >>> b = [4,5,6] >>> c = [4,5,6,7,8] >>> zipped = zip(a,b) # 打包為元組的列表 [(1, 4), (2, 5), (3, 6)] >>> zip(a,c) # 元素個(gè)數(shù)與最短的列表一致 [(1, 4), (2, 5), (3, 6)] >>> zip(*zipped) # 與 zip 相反,*zipped 可理解為解壓,返回二維矩陣式 [(1, 2, 3), (4, 5, 6)]
到此,關(guān)于“怎么使用Python內(nèi)置函數(shù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)頁名稱:怎么使用Python內(nèi)置函數(shù)
URL標(biāo)題:http://muchs.cn/article4/pdjeoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)頁設(shè)計(jì)公司、標(biāo)簽優(yōu)化、企業(yè)網(wǎng)站制作、外貿(mào)建站、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)