How do you express binary literals in Python?

前端 未结 7 1845
囚心锁ツ
囚心锁ツ 2020-11-27 10:15

How do you express an integer as a binary number with Python literals?

I was easily able to find the answer for hex:

>>> 0x12AF
4783
>>         


        
7条回答
  •  猫巷女王i
    2020-11-27 10:40

    For reference—future Python possibilities:
    Starting with Python 2.6 you can express binary literals using the prefix 0b or 0B:

    >>> 0b101111
    47
    

    You can also use the new bin function to get the binary representation of a number:

    >>> bin(173)
    '0b10101101'
    

    Development version of the documentation: What's New in Python 2.6

提交回复
热议问题