How to convert a binary number to decimal in Python

Binary numbers are numbers in the base-2 system; this means they are only expressed using two numbers, 00 and 11. In binary, all the digits in a number represent a power of 22, with the right-most digit starting with 20(1)2^0 (1). Computers usually operate using the binary number system.

On the other hand, decimal numbers are the standard numbers used globally and belong to the base-10 system. They include 1010 numbers, starting from 090–9.

Let’s see how we can convert binary numbers into decimal numbers in Python.

Algorithm to convert binary numbers into decimal

A simple algorithm to convert binary numbers into decimal is given below:

  1. Create a variable to store the decimal number. Initiate this variable to 00.

  2. Start iterating over each digit of the binary number from the right-most digit.

  3. For each digit multiply the value of the digit with 22 raised to the power of the position of the digit.

  4. Add the result to the variable created earlier to store the decimal.

  5. Repeat steps 343–4 till all the digits in the number are processed.

Binary to decimal conversion algorithm
Binary to decimal conversion algorithm

Implementing the algorithm

Let’s implement the aforementioned algorithm in the code widget given below:

def binary_to_decimal_converter(binary):
decimal = 0
power = 0
# Iterate over each digit of the binary number from right to left
for digit in reversed(binary):
# Convert the digit to an integer
digit_value = int(digit)
# Add the value of the digit multiplied by 2 raised to the power of its position
decimal += digit_value * (2 ** power)
# Move to the next position
power += 1
return decimal
binary_number = "10101"
print("Binary:", binary_number)
print("Decimal:", binary_to_decimal_converter(binary_number))

In the function given above,

  • Lines 6–12: We use a for loop to iterate over the binary number and convert it into decimal using the algorithm mentioned above.

    • Line 8: We convert the binary digit (00 or 11) into an integer.

    • Line 10: We multiply the binary digit with 22 raised to the power of the position of the digit, and add the result to decimal.

  • Line 14: Return the converted number.

In-built binary to decimal converter

Python provides us with a built-in function to convert binary numbers into decimals in case we don't want to get into the hassle of creating our function. Let’s see how this function works in the code given below:

binary_number = "10101"
decimal_number = int(binary_number, 2)
print("Binary number:", binary_number)
print("Decimal number:", decimal_number)

In the function given above, we use the function int() to convert our binary number into decimal. In this function, we provide 2 as the input, which specifies the base for our input function.

Conclusion

Binary-to-decimal conversion is a fundamental and common algorithm used in computer science and digital electronics. We can convert a binary number into a decimal by multiplying each digit in the binary number with 22 raised to the power of the position of the digit. We can also use the built-in function, int(), available in Python to convert binary numbers into decimal.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved