Python の組み込み関数ビン bin() の『使用例』と『エラーになったコード例』です。
bin() は、整数 integer を受け取ることができました。
整数でないものを渡すと、『整数として解釈できませんでした』という旨のエラーメッセージが出ました。
bin() でエラーになったコード例
bin() でエラーになったコード例です。
Python 公式マニュアル
bin('')
Traceback (most recent call last):
...
TypeError: 'str' object cannot be interpreted as an integer
bin('1')
Traceback (most recent call last):
...
TypeError: 'str' object cannot be interpreted as an integer
bin(float(1.1))
Traceback (most recent call last):
...
TypeError: 'float' object cannot be interpreted as an integer
bin(complex(1, 2))
Traceback (most recent call last):
...
TypeError: 'complex' object cannot be interpreted as an integer
bin(None)
Traceback (most recent call last):
...
TypeError: 'NoneType' object cannot be interpreted as an integer
bin(())
Traceback (most recent call last):
...
TypeError: 'tuple' object cannot be interpreted as an integer
bin([])
Traceback (most recent call last):
...
TypeError: 'list' object cannot be interpreted as an integer
bin(set())
Traceback (most recent call last):
...
TypeError: 'set' object cannot be interpreted as an integer
bin({})
Traceback (most recent call last):
...
TypeError: 'dict' object cannot be interpreted as an integer
bin(lambda x: x)
Traceback (most recent call last):
...
TypeError: 'function' object cannot be interpreted as an integer
bin(range(0, 1))
Traceback (most recent call last):
...
TypeError: 'range' object cannot be interpreted as an integer
import datetime
bin(datetime.datetime.max)
Traceback (most recent call last):
...
TypeError: 'datetime.datetime' object cannot be interpreted as an integer
import datetime
bin(datetime.date.max)
Traceback (most recent call last):
...
TypeError: 'datetime.date' object cannot be interpreted as an integer
import datetime
bin(datetime.time.max)
Traceback (most recent call last):
...
TypeError: 'datetime.time' object cannot be interpreted as an integer
import datetime
bin(datetime.timedelta(0))
Traceback (most recent call last):
...
TypeError: 'datetime.timedelta' object cannot be interpreted as an integer
bool に bin()
真理値 の bool に bin() を使用したコード例です。
Python 公式マニュアル
bin(False)
'0b0'
bin(True)
'0b1'
int に bin()
整数 の int に bin() を使用したコード例です。
Python 公式マニュアル
bin(-5)
'-0b101'
bin(-4)
'-0b100'
bin(-3)
'-0b11'
bin(-2)
'-0b10'
bin(-1)
'-0b1'
bin(0)
'0b0'
bin(1)
'0b1'
bin(2)
'0b10'
bin(3)
'0b11'
bin(4)
'0b100'