新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:Python bin()
bin 函數(shù)將整數(shù)作為輸入,并返回其等效的二進(jìn)制字符串。

**bin(integer)** #where integer is an integer number
bin()參數(shù):
bin 函數(shù)只接受一個(gè)強(qiáng)制參數(shù),即一個(gè)整數(shù)。如果傳遞任何非整數(shù)參數(shù),該方法將引發(fā)類型錯(cuò)誤。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 整數(shù) | 一個(gè)整數(shù),其二進(jìn)制等價(jià)于要計(jì)算的 | 需要 |
bin()返回值
bin 函數(shù)返回一個(gè)二進(jìn)制字符串。該二進(jìn)制字符串是傳遞的整數(shù)參數(shù)的等效二進(jìn)制
| 投入 | 輸出 | | 整數(shù) | 二進(jìn)制串 |
Python 中bin()方法的示例
示例 1:在 Python 中將整數(shù)轉(zhuǎn)換為等效的二進(jìn)制
int_num = 10
print('The binary equivalent of 10 is:', bin(int_num))
輸出:
The binary equivalent of 10 is: 0b1010
Here 0b indicates that its a binary number示例 2:傳遞非整數(shù)參數(shù)
int_num = 10.5
print('The binary equivalent of 10.5 is:', bin(int_num))
輸出:
TypeError: 'float' object cannot be interpreted as an integer 分享題目:創(chuàng)新互聯(lián)Python教程:Python bin()
分享URL:http://fisionsoft.com.cn/article/dpoophd.html


咨詢
建站咨詢
