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

Asked

Viewed 87 times

5

Why from this exit?

Code:

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

Exit:

15

1 answer

11


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

1.23 + 1.22 + 1.21 + 1.20

8 + 4 + 2 + 1 = 15

If it were 1010 it would print 10. If it were 10000 it 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 even.

  • Now it makes sense! Thank you Maniero!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.