Just how do you share binary literals in Python?

Just how do you share an integer as a binary number with Python literals?

I was conveniently able to locate the solution for hex:

>>> 0x12AF
4783
>>> 0x100
256

and also octal:

>>> 01267
695
>>> 0100
64

Just how do you make use of literals to share binary in Python?


Recap of Answers

  • Python 2.5 and also earlier: can share binary making use of int('01010101111',2) yet not with an actual.
  • Python 2.5 and also earlier: there is no other way to share binary literals.
  • Python 2.6 beta: You can do such as so: 0b1100111 or 0B1100111.
  • Python 2.6 beta: will certainly additionally permit 0o27 or 0O27 (2nd personality is the letter O) to stand for an octal.
  • Python 3.0 beta: Like 2.6, yet will certainly no more permit the older 027 syntax for octals.
0
2019-05-06 21:19:42
Source Share
Answers: 4

For reference bdsh future Python opportunities :
Starting with Python 2.6 you can share binary literals making use of the prefix 0b or 0B :

>>> 0b101111
47

You can additionally make use of the new container function to get the binary depiction of a number :

>>> bin(173)
'0b10101101'

Development variation of the documents : What's New in Python 2.6

0
2019-12-03 02:04:01
Source

As much as I can inform Python, up via 2.5, just sustains hexadecimal & octal literals. I did locate some conversations concerning including binary to future variations yet absolutely nothing precise.

0
2019-05-08 18:43:34
Source

I am rather sure this is just one of things as a result of transform in Python 3.0 with probably container () to select hex () and also oct ().

EDIT : lbrandy's solution is proper in all instances.

0
2019-05-08 18:39:40
Source
>>> print int('01010101111',2)
687
>>> print int('11111111',2)
255

Another means.

0
2019-05-08 18:31:50
Source