How do you express binary literals in Python?

前端 未结 7 1776
囚心锁ツ
囚心锁ツ 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条回答
  • 2020-11-27 10:47

    0 in the start here specifies that the base is 8 (not 10), which is pretty easy to see:

    >>> int('010101', 0)
    4161
    

    If you don't start with a 0, then python assumes the number is base 10.

    >>> int('10101', 0)
    10101
    
    0 讨论(0)
提交回复
热议问题