Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python Program to Convert Binary Number to Decimal and Vice-Versa

2 min read

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

def BinaryToDecimal(binary):
    decimal = 0
    for digit in binary:
        decimal = decimal*2 + int(digit)
    print("The decimal value is:", decimal)


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 :")
    decimal = int(binary_string,2)
    print("The decimal value is :", decimal)
except ValueError:
    print("Invalid binary number")


Enter a binary number :10
The decimal value is : 2

Convert Decimal number into Binary number

decimal = input("Enter a decimal number :")
# Calling the function
def DecimalToBinary(decimal):
    if decimal > 1:
        DecimalToBinary(decimal // 2)
    print(decimal % 2, end = '')


Enter a decimal number :2210110

Pythonic way to convert decimal into binary

Python has  bin() method to convert decimal numbers into binary. The bin() method converts and returns the binary equivalent string of a given integer.

decimal = input("Enter a binary number :")
binary = bin(int(decimal)).replace("0b", "")
print("The decimal number is :", binary)


Enter a binary number :10
The decimal number is : 1010


Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read