# How to Create a Binary to Decimal Number in Python

In this tutorial, we will program 'How to Convert Binary to Decimal Number in Python'. We will learn how to perform the conversion from binary to decimal numbers. The objective here is to make it easier for you to obtain the decimal result. I will provide a sample program to demonstrate the actual coding of this tutorial.

This topic is very easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program I will show you covers the basics for converting the binary to decimal number. I will do my best to provide you with the best way to get an accurate conversion. So, let's start with the coding.

## Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

## Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
`class NumberSystemConversions:    def binary_to_decimal(self, binary):        self.binary = list(str(binary))        self.dummy = list()        self.power = 0        for i in (self.binary[::-1]):            self.dummy.append(int(i)*pow(2, self.power))            self.power += 1        return sum(self.dummy)     def decimal_to_binary(self, decimal):        self.dummy = list()        while(decimal):            if decimal % 2 == 0:                self.dummy.append(0)                decimal = int(decimal/2)            else:                self.dummy.append(1)                decimal = int((decimal - 1)/2)         return "".join(map(str, self.dummy[::-1]))  x = NumberSystemConversions()choice = ""while(choice != "q"):    print("\n=============== Binary to Decimal Converter ===============")    print("1. Decimal to Binary\n2. Binary to Decimal\n3.Press 'q' to Quit")    choice = input("Enter your Choice : ")    if choice == '1':        try:            dec = int(input("Enter a decimal number : "))            print("-"*50)            print(f"{dec} in Binary is {x.decimal_to_binary(dec)}")        except :            print("Enter a valid choice")    elif choice == '2':        try:            binary = int(input("Enter a binary number: "))            print("-"*50)            print(f"{binary} in Decimal is {x.binary_to_decimal(binary)}")        except :            print("Enter a valid choice")`

This Python script defines a class NumberSystemConversions that contains methods for converting between binary and decimal numbers. It allows the user to choose between converting a decimal number to binary (decimal_to_binary method) or a binary number to decimal (binary_to_decimal method).

The script prompts the user to choose an option and input the respective number. If the user inputs an invalid choice or number, it catches the exception and prompts the user to enter a valid choice.

## Output:

The How to Create a Binary to Decimal Number in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Binary to Decimal Number in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials