Arun Sangal Arun Sangal - 1 year ago 187
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




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.

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download