Python code to convert Integer into Binary

14,747

You can just use the bin function:

>>> bin(100)
'0b1100100'

Ignore the 0b infront of the string. You can always get the raw binary numbers using using bin(your_numer)[2:].

Also, you can get this using the format function:

>>> format(100, 'b')
'1100100'
Share:
14,747
Admin
Author by

Admin

Updated on June 25, 2022

Comments

  • Admin
    Admin almost 2 years

    I need a code in Python 3.3 to convert an integer into binary. This is my first try:

    a = input(str("Please Enter a Number")
    if a == float:
        print (1)
    else print(0)
    b = a/2
    while True:
        if b == float:
            print(1)
        else print(0)
    

    I don't know why I keep getting errors with the if a == float:. And I know that the rest of the code is wrong too, but this : makes me crazy.