詳細信息
2.1 abs(x)
返回絕對值
>>> abs(-5) 5
2.2 all(iterable)
如果這個可迭代的元素都為真,就返回true。非0就為真,負數也為真,空也為真
>>> all([-1,2,3,4,5]) True >>> all((-1,2,3,4)) True >>> all([]) True >>> all([-1,0,2,3,4]) False
2.3 any(iterable)
可迭代的元素中,有一個為真,則返回真,空列表返回假。
>>> any([-1,0,1,2,3]) True >>> any([]) False >>> any([0]) False >>> any([1]) True
2.4 ascii(object)
把內存對象變成一個可打印的字符串格式
>>> ascii([1,2,3,4]) '[1, 2, 3, 4]'
2.5 bin(x)
把一個整數轉換為二進制數
>>> bin(11111) '0b10101101100111' >>> bin(-1223) '-0b10011000111' >>> bin(1.2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'float' object cannot be interpreted as an integer
2.6 boll([X])
不為空則為真,反之為假;判斷正確為真,錯誤為假
>>> bool([1,2,3,4]) True >>> bool([]) False >>> bool("1") True >>> bool("sfasfsa") True >>> bool("") False >>> bool(-1) True >>> bool(0) False >>> bool() False >>> bool({}) False >>> bool({"sdf":1}) True >>> bool(()) False >>> bool((1,2)) True >>> bool(3>5) False >>> bool(3<5) True
2.7 bytearray([source[,encoding[,errors]]])
字節數組,并且可以修改二進制的字節
>>> b=bytearray("abcd",encoding="utf-8") >>> b[0] # 打印第一個元素的ascii值 97 >>> b[0]=100 # 修改第一個元素的ascii值,賦值只能是ascii值 >>> b bytearray(b'dbcd')
2.8 bytes([source[, encoding[, errors]]])
字符串轉換成字節
>>> b=bytes("abcd",encoding="utf-8") >>> b b'abcd' >>> b[0] 97 >>> b[0]=100 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'bytes' object does not support item assignment
2.9 callable(object)
判斷一個對象是否可以被調用,只有在后面有括號的,表示可以調用,比如:函數、類
>>> callable([]) False >>> def bus():pass ... >>> callable(bus) True
2.10 chr(i)
通過ascii的值,找到對應的字符
>>> chr(99) 'c'
2.11 ord(c)
根據字符,找到對應的ascii值
>>> ord("c") 99
2.12 dict(**kwarg)、dict(mapping,**kwarg)、dict(iterable, **kwarg)
生成一個字典
#傳入非固定關鍵字參數 >>> dict(name="bigberg",age=22) {'name': 'bigberg', 'age': 22} # 傳入列表 >>> s_list=[("name","bigberg"),("age",22)] >>> dict(s_list) {'name': 'bigberg', 'age': 22} >>> n_list=[['names',['zhangsan','lisi','wangwu']],['job',['doctor','teacher','police']]] >>> dict(n_list) {'names': ['zhangsan', 'lisi', 'wangwu'], 'job': ['doctor', 'teacher', 'police']}
2.13 dir(object)
查看方法
dir(list): 查看列表的方法
dir(dict): 查看字典的方法
2.14 divmod(a,b)
地板除,獲得一個元組,元組第一個元素是商,第二個元素是余數。
>>> divmod(14,3) (4, 2)
2.15 enumerate(iterable,start=0)
獲取一個列表,列表中的每個元素都是一個元組,元組的第一個數是iterable的索引,第二個數是iterable的元素。
fruits = ['apple', 'orange', 'banana'] print(list(enumerate(fruits))) #
2.16 eval(expression, globals=None, locals=None)
把字典類型的字符串變成字典,把一個整數類型的字符變成int類型,或者加減乘除這種簡單轉換成表達式。
>>> s = "5+989" >>> eval(s) 994
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com