Arun Sangal Arun Sangal - 4 months ago 16
Python Question

Python printing binary value and its decimal form

Python 2.6/2.7

I have the following program.

print 0b10

def p_m(x):
print x

p_m(bin(2))


Output:

2
0b10


What I wanted to print in p_m() is 2 (the decimal value) of "0b10" which is what I'm passing to p_m() as first argument and getting this value "0b10" in variable x and just printing that x.

Why
print x
inside the function is NOT working like the first line in the program?

What should I do in p_m()'s print statement to print the value of "0b10" as 2.

Answer

0b10 is an int. bin(2) is '0b10' which is a string. You'd have to print int('0b10', 2) to get it to print 2