Why does "int ('1111', 2)" return "15" in Python?

5

Why this output?

Code:

a = int('1111', 2)
print(a)

Output:

15
    
asked by anonymous 21.11.2017 / 13:01

1 answer

11

You have created an integer based 2, that is, in binary, and 1111 is 15 in decimal.

1.2 3 + 1.2 2 + 1.2 1

8 + 4 + 2 + 1 = 15

If it were 1010 would print 10. If it were 10000 would print 16.

Documentation . Note that there is a first parameter that accepts the value that will be converted to integer and a second that says which base you want to use, the default is 10. So if you had used this base 1111 would print 1111 same.     

21.11.2017 / 13:06