python內置函數

abs 返回參數的絕對值python

>>> abs(-3)
3

all 可迭代對象的每一個元素是否都爲真數組

>>> all('nidaye')
True
>>> all([1,0])
False

any 可迭代對象是否有爲真的元素spa

>>> any('nidaye')
True
>>> any([1,0])
True

ascii 返回一個表示對象的字符串3d

>>> ascii([1,0])
'[1, 0]'

bin 返回二進制表示法code

>>> bin(2)
'0b10'
>>> bin(4)
'0b100'

bytearray 返回一個新的字節數組,這個數組的內容能夠改變對象

>>> b = bytearray(a, encoding="utf-8")
>>> b
bytearray(b'today')
>>> b[0]
116
>>> b[0] = 50
>>> b[0]
50

bytes 字符串轉二進制blog

callable 是否能被調用(我的理解,加上()是否不報錯)索引

>>> callable(abs)
True
>>> a = 0
>>> callable(a)
False

chr 傳入的參數做爲索引,返回ASCII表中該索引對應的值utf-8

>>> chr(50)
'2'
>>> chr(49)
'1'
>>> chr(88)
'X'

compile 將字符串編譯爲字節代碼對象,感受沒啥用ci

>>> code = compile('print(666)', '', 'exec')
>>> code
<code object <module> at 0x7fd48761e390, file "", line 1>

complex 返回複數形式(估計真的是不少人一生都用不上)

delattr 刪除對象的一個屬性

divmod 返回一個元組,該元組的第一個元素是二者的商,第二個元素是二者的餘數

>>> divmod(5, 2)
(2, 1)

enumerate 枚舉[ɪˈnju:məreɪt]  傳進一個可迭代對象,返回一個enumerate對象,該對象的每一項都是又(索引, 值)組成的元組。

>>> a = enumerate(range(6))
>>> a
<enumerate object at 0x7ff53dc415a0>
>>> for i in a:
...   print(i)
... 
(0, 0)
(1, 1)
(2, 2)
(3, 3)
(4, 4)
(5, 5)

>>> b = {'today': '4.28', 'weather': 'notbad'}
>>> for i in enumerate(b):
...   print(i)
... 
(0, 'weather')
(1, 'today')

eval 執行一個字符串表達式,並返回其表達式的返回值

>>> a = 'print("努力吧!")'
>>> eval(a)
努力吧!

 exec 執行存儲在字符串中或文件中的python語句,相比eval可執行更復雜的python代碼

frozenset 返回一個凍結的集合,該集合不能添加或刪除任何元素

>>> a = frozenset([1,2,3])
>>> a
frozenset({1, 2, 3})
>>> a.add(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
>>> a.remove(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'remove'

getattr 獲取對象的屬性

globals 獲取當前位置的所有全局變量

hasattr 對象是否有對應的屬性

>>> hasattr(dict, 'enumerate')
False
>>> hasattr(dict, 'pop')
True

hash 獲取一個對象的hash值

相關文章
相關標籤/搜索