Binary to Decimal in Python

We know that the computer only understands binary numbers that are 0 and 1. All data are given as input to the computer converts into a binary number system. But, a binary number is difficult to understand by humans. We have discussed how to convert binary to decimal in python. In the same way, conversion of Decimal to Binary, Decimal to Octal and Octal to Decimal, Octal to Binary, and Binary to Octal also can be done.

We will be given a binary number and the python program to convert the given binary number into an equivalent decimal number.

Example:-

Binary number: 1001
Decimal number: 9

Convert Binary to Decimal in Python using while loop

This python program using a while loop to convert binary to decimal. We can also take the help of a user-defined function. A function is a block of code that performs a specific task. We will take a binary number while declaring the variables. Python program to convert binary to the decimal using while loop and finally, the result will be displayed on the screen.

# Python program to convert Binary to Decimal using while loop

def BinaryDecimal(n):  #user-defined function
    num, dec, base = n, 0, 1
     
    temp = num
    while(temp):
        digit = temp % 10
        temp = int(temp / 10)
        
        dec += digit * base
        base = base * 2
    return dec
 
# take inputs
num = int(input('Enter a binary number: '))

# display result
print('The decimal value is =', BinaryDecimal(num))

Output for the different input values:-

Enter a binary number: 101
The decimal value is = 5

Enter a binary number: 101011
The decimal value is = 43

Note:- This Python program works only with binary numbers in the range of integers. If we want to work with long types of binary numbers like 20bits or 30bits, we can use a string variable to store the binary numbers.

Convert using for loop

In the previous program, convert binary to a decimal using a while loop but in this program, convert binary to a decimal using for loop. This program uses string variables instead of integers to store binary values.

# Python program to convert Binary to Decimal using for loop

def BinaryDecimal(n):  #user-defined function
    num, dec, base = n, 0, 1
     
    len1 = len(num)
    for i in range(len1 - 1, -1, -1):
        if (num[i] == '1'):     
            dec += base
        base = base*2;
    return dec
 
# take inputs
num = input('Enter a binary number: ')

# display result
print('The decimal value is =', BinaryDecimal(num))

Output:-

Enter a binary number: 1110101
The decimal value is = 117

Convert Binary to Decimal Python Program

This is the simplest and easiest program in python because this program is using a pre-defined function. We will take the binary number when declaring the variable and print the decimal value of the number.

# Python program to convert Binary to Decimal

def BinaryDecimal(n):  #user-defined function
    return int(n, 2)
 
# take inputs
num = input('Enter a binary number: ')

# display result
print('The decimal value is =', BinaryDecimal(num))

Output:-

Enter a binary number: 1101
The decimal value is = 13

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *