Python's built-in function bin ()

Source: Internet
Author: User
Bin (x)

英文说明: Converts an integer x to a binary string, and if x is not of type int in Python, x must contain the method __index__ () and the return value is integer;

parameter x: An integer or a type containing the __index__ () method that returns a value of integer;

version : The bin function is a new function in python2.6, and you should pay attention to the version problem when using.

Example:

The case of integers:

Here the results of the form and our usual habits some differences, mainly in front of more than 0b, which is the meaning of binary.

>>> Bin (5) ' 0b101 ' >>> len (Bin (5)) 5

Non-integer case: Must contain the __index__ () method to cut the type of the return value to Integer

>>> class Mytype:def __index__ (self): return    >>> myvar = MyType () >>> bin (myvar) 
  
    ' 0b1000001001 '
  

  

Python's 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.