Binary numbers

I use the python shell to figure out how the print command works in python.
When i type

  

print 01
    1
    print 010
    8
    print 0100
    64
    print 030
    24

  

What's going on here? Is it just base 2? Why is the “one” in the second position printed as 8? Shouldn't it be 2 if it is binary?

+3
source share
8 answers

Python 2. , , 0x . Python 3, 0, , , 0o. 0b, .

>>> 10
10
>>> 0x10
16
>>> 0o10
8
>>> 0b10
2
>>> 010
  File "<stdin>", line 1
    010
      ^
SyntaxError: invalid token

0x, 0o 0b Python 2.6 Python 2.7.

+12

Python.

Python 2.6 0o10 0b10010 .

Python :

>>> x = int("10010", 2)
>>> print x
18
+5

0 .

Python 3 Python 2.6: 0o....

>>> 0b1010 == 012 == 0xA == 10
True
+3

0 , . , 10 8 , 100 64 ..

, .

+1

, Python C , 0, ( 8) .

0

( 8) , .

0

Definitely not base2. It is octal - base 8.

0
source

Numbers starting with 0 are interpreted as octal. For binary numbers, the "initial sequence" is 0b.

>>> print 0b10
2
>>> print 010
8
>>> print 0x10
16
0
source

Source: https://habr.com/ru/post/1781151/


All Articles