<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          福利來了!Python內(nèi)置函數(shù)最全總結(jié),建議收藏!

          共 8593字,需瀏覽 18分鐘

           ·

          2023-01-03 21:06

          1 abs()

          絕對值或復(fù)數(shù)的模

          In [1]: abs(-6)
          Out[1]: 6

          2 all()

          接受一個迭代器,如果迭代器的所有元素都為真,那么返回True,否則返回False

          In [2]: all([1,0,3,6])
          Out[2]: False

          In [3]: all([1,2,3])
          Out[3]: True

          3 any()

          接受一個迭代器,如果迭代器里有一個元素為真,那么返回True,否則返回False

          In [4]: any([0,0,0,[]])
          Out[4]: False

          In [5]: any([0,0,1])
          Out[5]: True

          4 ascii()

          調(diào)用對象的repr() 方法,獲得該方法的返回值
          In [30]: class Student():
              ...:     def __init__(self,id,name):
              ...:         self.id = id
              ...:         self.name = name
              ...:     def __repr__(self):
              ...:         return 'id = '+self.id +', name = '+self.name

          In [33]: print(xiaoming)
          id = 001, name = xiaoming

          In [34]: ascii(xiaoming)
          Out[34]: 'id = 001, name = xiaoming'

          5 dict()

          創(chuàng)建數(shù)據(jù)字典

          In [92]: dict()
          Out[92]: {}

          In [93]: dict(a='a',b='b')
          Out[93]: {'a''a''b''b'}

          In [94]: dict(zip(['a','b'],[1,2]))
          Out[94]: {'a': 1, 'b': 2}

          In [95]: dict([('a',1),('b',2)])
          Out[95]: {'a': 1, 'b': 2}

          6 dir()

          不帶參數(shù)時返回當(dāng)前范圍內(nèi)的變量,方法和定義的類型列表;帶參數(shù)時返回參數(shù)的屬性,方法列表。

          In [96]: dir(xiaoming)
          Out[96]:
          ['__class__',
           '__delattr__',
           '__dict__',
           '__dir__',
           '__doc__',
           '__eq__',
           '__format__',
           '__ge__',
           '__getattribute__',
           '__gt__',
           '__hash__',
           '__init__',
           '__init_subclass__',
           '__le__',
           '__lt__',
           '__module__',
           '__ne__',
           '__new__',
           '__reduce__',
           '__reduce_ex__',
           '__repr__',
           '__setattr__',
           '__sizeof__',
           '__str__',
           '__subclasshook__',
           '__weakref__',

           'name']

          7 divmod()

          分別取商和余數(shù)

          In [97]: divmod(10,3)
          Out[97]: (3, 1)

          8  isinstance(object, classinfo)

          判斷object是否為類classinfo的實例,是返回true

          In [20]: class Student():
              ...:     ...:     def __init__(self,id,name):
              ...:     ...:         self.id = id
              ...:     ...:         self.name = name
              ...:     ...:     def __repr__(self):
              ...:     ...:         return 'id = '+self.id +', name = '+self.name
              ...:

          In [21]: xiaoming = Student('001','xiaoming')

          In [22]: isinstance(xiaoming,Student)
          Out[22]: True

          9 issubclass(class, classinfo)

          如果class是classinfo類的子類,返回True:

          In [27]: class undergraduate(Student):
              ...:     def studyClass(self):
              ...:         pass
              ...:     def attendActivity(self):
              ...:         pass
              ...:

          In [28]: issubclass(undergraduate,Student)
          Out[28]: True

          In [29]: issubclass(object,Student)
          Out[29]: False

          In [30]: issubclass(Student,object)
          Out[30]: True
          如果class是classinfo元組中某個元素的子類,也會返回True
          In [26]: issubclass(int,(int,float))
          Out[26]: True

          10 iter(object, sentinel)

          返回一個可迭代對象, sentinel可省略

          In [72]: lst = [1,3,5]

          In [73]: for i in iter(lst):
              ...:     print(i)
              ...:
          1
          3
          5

          sentinel 理解為迭代對象的哨兵,一旦迭代到此元素,立即終止:

          In [81]: class TestIter(object):
              ...:         def __init__(self):
              ...:             self.l=[1,3,2,3,4,5]
              ...:             self.i=iter(self.l)
              ...:         def __call__(self):  #定義了__call__方法的類的實例是可調(diào)用的
              ...:             item = next(self.i)
              ...:             print ("__call__ is called,which would return",item)
              ...:             return item
              ...:         def __iter__(self): #支持迭代協(xié)議(即定義有__iter__()函數(shù))
              ...:             print ("__iter__ is called!!")
              ...:             return iter(self.l)
              ...:

          In [82]:     t = TestIter()
              ...:     t1 = iter(t, 3)
              ...:     for i in t1:
              ...:         print(i)
              ...:
          __call__ is called,which would return 1
          1
          __call__ is called,which would return 3

          11 max(iterable,*[, key, default])

          返回最大值:

          In [99]: max(3,1,4,2,1)
          Out[99]: 4

          In [100]: max((),default=0)
          Out[100]: 0

          In [89]: di = {'a':3,'b1':1,'c':4}
          In [90]: max(di)
          Out[90]: 'c'

          In [102]: a = [{'name':'xiaoming','age':18,'gender':'male'},{'name':'
               ...: xiaohong'
          ,'age':20,'gender':'female'}]
          In [104]: max(a,key=lambda x: x['age'])
          Out[104]: {'name''xiaohong''age': 20, 'gender''female'}

          12 min(iterable,*[, key, default])

          返回最小值

          13 memoryview(obj)

          返回由給定實參創(chuàng)建的“內(nèi)存視圖”對象, Python 代碼訪問一個對象的內(nèi)部數(shù)據(jù),只要該對象支持 緩沖區(qū)協(xié)議 而無需進行拷貝

          14 next(iterator,[, default])

          返回可迭代對象的下一個元素

          In [129]: it = iter([5,3,4,1])

          In [130]: next(it)
          Out[130]: 5

          In [131]: next(it)
          Out[131]: 3

          In [132]: next(it)
          Out[132]: 4

          In [133]: next(it)
          Out[133]: 1

          In [134]: next(it,0) #迭代到頭,默認返回值為0
          Out[134]: 0

          In [135]: next(it)
          ----------------------------------------------------------------------
          StopIteration                        Traceback (most recent call last)
          <ipython-input-135-bc1ab118995a> in <module>
          ----> 1 next(it)

          StopIteration:

          15 object()

          返回一個沒有特征的新對象。object 是所有類的基類。

          In [137]: o = object()

          In [138]: type(o)
          Out[138]: object


          推薦閱讀:

          入門: 最全的零基礎(chǔ)學(xué)Python的問題  | 零基礎(chǔ)學(xué)了8個月的Python  | 實戰(zhàn)項目 |學(xué)Python就是這條捷徑


          干貨:爬取豆瓣短評,電影《后來的我們》 | 38年NBA最佳球員分析 |   從萬眾期待到口碑撲街!唐探3令人失望  | 笑看新倚天屠龍記 | 燈謎答題王 |用Python做個海量小姐姐素描圖 |碟中諜這么火,我用機器學(xué)習(xí)做個迷你推薦系統(tǒng)電影


          趣味:彈球游戲  | 九宮格  | 漂亮的花 | 兩百行Python《天天酷跑》游戲!


          AI: 會做詩的機器人 | 給圖片上色 | 預(yù)測收入 | 碟中諜這么火,我用機器學(xué)習(xí)做個迷你推薦系統(tǒng)電影


          小工具: Pdf轉(zhuǎn)Word,輕松搞定表格和水印! | 一鍵把html網(wǎng)頁保存為pdf!|  再見PDF提取收費! | 用90行代碼打造最強PDF轉(zhuǎn)換器,word、PPT、excel、markdown、html一鍵轉(zhuǎn)換 | 制作一款釘釘?shù)蛢r機票提示器! |60行代碼做了一個語音壁紙切換器天天看小姐姐!


          年度爆款文案


          點閱讀原文,看B站50個Python實戰(zhàn)視頻!

          瀏覽 54
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  视频一区综合网 | 三级片男人的天堂 | 特级日本欧美日韩亚洲精品综合免费在线 | 超碰在线观 | 操逼片五月婷婷 |