Wednesday, January 22, 2020

Abhijeet Pal: Python Program to Convert Binary Number to Decimal and Vice-Versa

A binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols 0 and 1. The decimal numeral system is the standard system for denoting integer and non-integer numbers. All decimal numbers can be converted to equivalent binary values and vice versa for example, the binary equivalent of “2” is “10” to explore more visit binary to decimal converter. In this article, we will create python programs for converting a binary number into decimal and vice versa Convert Binary to Decimal # Taking binary input binary = input("Enter a binary number:") # Calling the function BinaryToDecimal(binary) def BinaryToDecimal(binary): decimal = 0 for digit in binary: decimal = decimal*2 + int(digit) print("The decimal value is:", decimal) Output Enter a binary number:10 The decimal value is: 2 Pythonic way to convert binary into decimal We can use the built-in int() function to convert binary numbers into decimals. The int() function converts the binary string in base 2 number. binary_string = input("Enter a binary number :") try: decimal = int(binary_string,2) print("The decimal value is …

The post Python Program to Convert Binary Number to Decimal and Vice-Versa appeared first on Django Central.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...