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.
  1. class NumberSystemConversions:
  2. def binary_to_decimal(self, binary):
  3. self.binary = list(str(binary))
  4. self.dummy = list()
  5. self.power = 0
  6. for i in (self.binary[::-1]):
  7. self.dummy.append(int(i)*pow(2, self.power))
  8. self.power += 1
  9. return sum(self.dummy)
  10.  
  11. def decimal_to_binary(self, decimal):
  12. self.dummy = list()
  13. while(decimal):
  14. if decimal % 2 == 0:
  15. self.dummy.append(0)
  16. decimal = int(decimal/2)
  17. else:
  18. self.dummy.append(1)
  19. decimal = int((decimal - 1)/2)
  20.  
  21. return "".join(map(str, self.dummy[::-1]))
  22.  
  23.  
  24. x = NumberSystemConversions()
  25. choice = ""
  26. while(choice != "q"):
  27. print("\n=============== Binary to Decimal Converter ===============")
  28. print("1. Decimal to Binary\n2. Binary to Decimal\n3.Press 'q' to Quit")
  29. choice = input("Enter your Choice : ")
  30. if choice == '1':
  31. try:
  32. dec = int(input("Enter a decimal number : "))
  33. print("-"*50)
  34. print(f"{dec} in Binary is {x.decimal_to_binary(dec)}")
  35. except :
  36. print("Enter a valid choice")
  37. elif choice == '2':
  38. try:
  39. binary = int(input("Enter a binary number: "))
  40. print("-"*50)
  41. print(f"{binary} in Decimal is {x.binary_to_decimal(binary)}")
  42. except :
  43. 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

Add new comment