bin(x)
英文說明:Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
New in version 2.6.
中文說明:將整數(shù)x轉(zhuǎn)換為二進(jìn)制字符串,如果x不為Python中int類型,x必須包含方法__index__()并且返回值為integer;
參數(shù)x:整數(shù)或者包含__index__()方法切返回值為integer的類型;
版本:bin函數(shù)是python2.6中新增函數(shù),使用時要注意版本問題。
實例講解:
#整數(shù)的情況 >>> bin(521) #這里的顯示結(jié)果形式與我們平時習(xí)慣有些差別,主要是前面多了0b,這是表示二進(jìn)制的意思。 '0b1000001001' #非整型的情況,必須包含__index__()方法切返回值為integer的類型 >>> class myType: ... def __index__(self): ... return 35 >>> myvar = myType() >>> bin(myvar) '0b1000001001'
PS:改函數(shù)非常簡單,但是要注意版本,和參數(shù)類型。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com