Why it is not converting to decimal to b


Like it is converting ‘a’ variable to decimal value why it is not converting to b variable.


This just sets a to 12, you’ve just written the value in base 2
instead of base 10.


Thos converts a into a string which contains the hexadecimal
representation of the value in a (12), which will be 0xc.

Let’s show this in a bit more detail:

 >>> a = 0b1100
 >>> a
 >>> type(a)
 <class 'int'>
 >>> hex(a)
 >>> bin(a)
 >>> oct(a)
 >>> type(hex(a))
 <class 'str'>

So a is just 12, however you produced it. It does have any inherent
“base” for printing out. It’s just an integer, with type int as shown

When you print(a), the print function calls str(a) to get
something to print. str on an integer produces a base 10
representation of the value, because that’s what humans expect to see.
And print the prints that string.

There are various convenience functions presupplied in Python for
getting ints in some common bases: hex() for base 16, bin() for
base 2 and oct() for base 8. But they all return strings (type

When you print a string, print treats it like any other argument: it
calls str() on it to get a string to print. In Python, str() of a
string returns that same string, so strings get printed straight out as
they are.

Cameron Simpson cs@cskk.id.au

Ohh…Got it, wow that’s really detailed and helpful.
Thanks a ton!!