2

I tried the following code,

a=0b1101
b=int(a,2)
print(b)

And it gave me an error.

TypeError: int() can't convert non-string with explicit base

But when I don't mention the base it just works fine,

a=0b1101
b=int(a)
print(b)

1 Answer 1

3

Having zero in the beginning has a special meaning in python. For eg. 0x means hexadecimal number. While using the int() func, we provide the second argument when the binary number is stored in the form of an integer. For eg,

b=int(a,2)

but when converting an actual binary number to integer we don't provide the second argument. Eg,

b=int(a)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.