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
or0B1100111
. - Python 2.6 beta: will certainly additionally permit
0o27
or0O27
(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
Justin Standard 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
Andreas Thomas 2019-12-03 02:04:01
Source
Share
>>> print int('01010101111',2)
687
>>> print int('11111111',2)
255
Another means.
0
Louis Brandy 2019-05-08 18:31:50
Source
Share
Related questions