Python3 built-in function--bin

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.