First in English document:
-
-
bin
(x)
-
Convert an integer number to a binary string prefixed with "0b". The result is a valid Python expression. If x was not a Python int
object, it had to define an __index__()
method, which returns an integer. Some Examples:
>>>
Bin(3) '0b11 'bin(-ten)' -0b1010 '
If prefix "0b" is desired or not, you can use either of the following ways.
>>>
Format ('#b 'format(' B ')(' 0b1110 ', ' 1110 ')F'{: #b} 'f'{14:b}'(' 0b1110 ', ' 1110 ')
See also for more format()
information.
For the moment, we'll only discuss what happens when x is an integer type of data.
sort out function Information table:
Function prototypes |
Bin (x) |
Parameter interpretation |
X |
Integer, parameter cannot be empty. |
return value |
<class ' str ' > String type, Binary integer. |
Function description |
Converts an integer to a binary integer and returns it as the type of the string. |
It is easy to understand that the function accepts and accepts only one integer and returns it as a binary form.
>>> Bin (0)'0b0'print(Bin ( -729))-0b1011011001
It is important to note that the return value of the function is a string and the return value should not be evaluated.
>>> type (Bin (729))<class'str'>
>>> bin * 2'0b10100b1010'
If you need to calculate, you need to use the INT function to convert the string to int type data.
>>> Int (Bin (729), base = 2) #base parameter is not available, otherwise an error 729 will be
Of course, parameters can not only accept decimal integers, octal, hexadecimal is also possible, as long as the int type data is legal.
1 >>> bin (0b10010)2'0b10010'3 >> > Bin (0o12345)4'0b1010011100101'5 >>> Bin (0x2d9) 6 ' 0b1011011001 '
Python3 built-in function--bin