Decimal to Binary Converter in Python


In this python tutorial, you will see how we can create that convert a binary number to a decimal number and do the vice versa in python. It is just simple mathematics that you should know.

You should Also Read: Decimal to Binary Converter in tkinter python

Decimal to the binary converter in Python

Below is the python code that helps us convert decimal to binary and vice versa. Run the following code in your python ide and you will see the result.

decimal to binary converter

    menu = int(input("Choose an option: \n 1. Decimal to binary \n 2. Binary to decimal\n Option: "))
    if menu < 1 or menu > 2:
        raise ValueError
    if menu == 1:
        dec = int(input("Input your decimal number:\nDecimal: "))
        print("Binary: {}".format(bin(dec)[2:]))
    elif menu == 2:
        binary = input("Input your binary number:\n Binary: ")
        print("Decimal: {}".format(int(binary, 2)))
except ValueError:
    print ("please choose a valid option")

Related Python Projects

Summary and Conclusion

In this python tutorial, you have seen how we can create a python project that conversion of numbers system. If you have any questions please let us know in the comment section.

Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !