This article will discuss how to convert binary to decimal in Python. Python has different methods for converting binary to decimal data. A binary number is represented by 0s and 1s; a decimal number is an integer value.
How to Convert Binary to Decimal in Python
Let’s consider a binary number – 1100
The decimal conversion process is We have to multiply each value with 2 to raise to the power of values start from 0 to n. And add all the values - (1*2^3)+ (1*2^2)+(0*2^1)+ (0*2^0) =(1*8)+(1*4)+(0*2)+(0*0) =8+4 =12
Hence, the decimal value for the binary number 1100 is 12.
Method 1: Convert Binary to Decimal using int()
int() is used to convert binary numbers to decimals.
Syntax:
int(binary_number, 2)
Where binary_number is the input binary number.
Steps:
- Input a Binary number
- Convert binary to a decimal using int()
- Display the decimal number converted from the binary number
Code:
#input a number
binary_number= input("Enter an Binary number ")
print("The binary number is ",binary_number)
#convert to decimal
decimal_number= int(binary_number, 2)
print("The decimal number is ",decimal_number)
Output:
So we are giving an input – 110110
Enter an Binary number 110110 The binary number is 110110 The decimal number is 54
Method 2: Convert Binary to Decimal without using the built-in function
Steps:
- Input a binary number
- Specify the for loop, and inside that,
- Divide the binary number by 10 to get the remainder
- Add the remainder to the decimal variable initialized.
- Divide the binary number by 2 and take only the integer part using the int() function
- End of the for loop
- Display the decimal number
Code:
#get an binary number input from user
binary_number = int(input("Enter the Binary Number: "))
#create a decimal variable and set to 0
decimal_value=0
#initialize a variable i and set to 1
i = 1
#get the length of the binary number
length = len(str(binary_number))
#logic to convert binary to decimal
for k in range(length):
reminder = binary_number % 10
decimal_value = decimal_value + (reminder * i)
i = i * 2
binary_number = int(binary_number/10)
#display the decimal value
print("Decimal number is ", decimal_value)
Output:
So we are giving an input – 110110
Enter the Binary Number: 1100 Decimal number is 12
Next Read: How To Automate Google Search With Python
Method 3: Convert Binary to Decimal using Recursion
Steps:
- Input a binary number
- Define the function, and inside that, check the condition and return 0 if the binary number equals 0; otherwise, go to step 3.
- Divide the binary number by 10 to get the remainder.
- Add the remainder to the decimal variable initialized.
- Divide the binary number by 2 and take only the integer part using the int() function
- Return to function again
- Display the decimal number
Code:
#Binary_To_Decimal function with 2 parameters
def Binary_To_Decimal(binary_number, exponent=1):
if binary_number== 0:
return 0
else:
#actual conversion lofic
k= binary_number % 10
binary_number= int(binary_number / 10)
k= k * exponent
return k + Binary_To_Decimal(binary_number, exponent * 2)
#input
binary_number = int(input('Enter a binary number: '))
#output
print('The decimal value is ', Binary_To_Decimal(binary_number))
Output:
So we are giving an input – 1100
Enter a binary number: 1100 The decimal value is 12
Method 4: Convert Binary List to Decimal
In this case, we are going to convert some binary numbers to decimal through a list in Python
int() is used to convert binary numbers to decimals.
Syntax:
(int(str(i), 2) for i in binary_numbers)
Where binary_number is the input binary number.
Steps:
- Input Binary numbers from a list
- Iterate the list of binary numbers and convert it to a decimal using the int() function. Place str() to convert the binary values from integer to string
- Use a for loop to get the decimal values from binary values
Code:
#binary_numbers
binary_numbers = [11,00,1110,111000,10001,110]
#convert to decimal
decimal_data= (int(str(i), 2) for i in binary_numbers)
#display
for i in decimal_data:
print(i)
Output:
So we are giving 6 binary input values.
3 0 14 56 17 6
Method 5: Convert Binary String List to Decimal
In this method, we are going to convert some binary numbers of string type to decimal through a list in Python
int() is used to convert binary numbers to decimal.
Syntax:
(int(i, 2) for i in binary_numbers)
Where binary_number is the input binary number.
Steps:
- Input Binary numbers from a list
- Iterate the list of binary numbers and convert them to decimals using the int() function.
- Use a for loop to get the decimal values from binary values
Code:
#binary_numbers
binary_numbers = ['11','00','1110','111000','10001','110']
#convert to decimal
decimal_data= (int(i, 2) for i in binary_numbers)
#display
for i in decimal_data:
print(i)
Output:
So, we are giving 6 binary input values.
3 0 14 56 17 6
So, these are the ways to convert Binary values to Decimal values in Python.
