Why it is not converting to decimal to b

a=0b1100
b=hex(a)
print(b)
0xc
print(a)
12

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

a=0b1100

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

b=hex(a)

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
 12
 >>> type(a)
 <class 'int'>
 >>> hex(a)
 '0xc'
 >>> bin(a)
 '0b1100'
 >>> oct(a)
 '0o14'
 >>> 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
above.

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
str).

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.

Cheers,
Cameron Simpson cs@cskk.id.au

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