# How to Convert a Number from Binary to Decimal in Python

--

Would you like to learn how to convert a number from binary to decimal in Python? You are in the right place.

**In Python, you represent binary numbers using 0b followed by the number. To convert a binary number to a decimal number Python provides the int() function. To convert the other way around, from decimal to binary, Python provides the bin() built-in function.**

In this Python tutorial, we will cover a few examples to show you how to work with binary numbers in Python and how to convert numbers from the binary system to the decimal number system and vice-versa.

Let’s begin the conversion!

# How to Convert Binary to Decimal Number in Python

As you know, the only two numbers you have in a binary number system are 0 and 1.

So, how can you convert a binary number into decimal using Python?

Let’s take, for example, the binary number 1010…

**To specify a binary number in Python you have to use the prefix 0b. For example, you can write the binary number 1010 as 0b1010.**

Let’s see what this looks like in the Python shell:

`>>> number = 0b1010`

And here is something interesting…

This is what you get back if you print this number.

`>>> number`

10

Even if we have specified a binary value, Python has stored this number as a decimal value.

Also…

**To convert binary numbers to their decimal representation Python has a built-in function called int().**

`>>> int(0b1010)`

10

# How to Convert Decimal to Binary in Python

So, if you have a decimal number, how can you print it in using its binary format?

**To print a number in binary format Python provides the built-in function bin().**

Let’s use the bin() function to print the previous number in binary format:

`>>> number = 10`

>>> bin(number)

'0b1010'

Et voilà!

This means that…

**To convert a decimal number into the corresponding binary number in Python you can use the bin() built-in function.**

# How to Calculate the Sum of Two Binary Numbers in Python

**To calculate the sum of two binary numbers in Python we first specify the two numbers starting with 0b. Then we can use the + operator as usual to calculate the sum of the two numbers. Finally, we return the sum of the two numbers in binary format using the bin() built-in function.**

Here is an example:

`>>> number1 = 0b1010`

>>> number2 = 0b1010

>>> sum = number1 + number2

>>> sum

20

>>> bin(sum)

'0b10100'

As explained previously, the sum is stored as a decimal number and to get it back as a binary number we use the bin() function.

Let’s confirm that the result is correct:

`10100 = 2^2 + 2^4 = 4 + 16 = 20`

The binary number 10100 is equal to 2 to the power of 2 plus 2 to the power of 4.

# How to Convert a Binary String into a Decimal Number

Previously we have seen that when you specify a binary number Python automatically stores it as a decimal.

You might also find Python programs in which binary numbers are represented using a string like in the example below:

`>>> number = "0b1010"`

>>> number

'0b1010'

How do you convert a binary string into the decimal equivalent?

Let’s try to use the int() function:

`>>> int(number)`

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ValueError: invalid literal for int() with base 10: '0b1010'

Hmmm…what’s causing this error?

Python is not able to understand the value “0b1010” because it assumes it’s a decimal number (see “base 10” in the error).

The int() function allows passing, as the second argument, the base of the number. In this case, the base is 2 considering that we are working with a binary number.

`>>> int(number, 2)`

10

That’s better, we have converted a binary string into a decimal number.

# Function to Convert Binary to Decimal Number

Now that we know the basic concepts of binary numbers in Python, it’s time to write a Python program to convert a number from binary to decimal.

Let’s create a Python function that takes a binary number as a string and returns its decimal value. We will use what you have learned in the previous section.

`def convert_binary_to_decimal(bin_num):`

return int(bin_num, 2)

This function takes a binary number as input and returns its decimal value using the int() function.

Here is how you can call this function:

`number = "0b1011"`

print(convert_binary_to_decimal(number))

[output]

11

You can improve this Python program using the input() function and give the result back to the user with the string format() method.

`number = input("Please insert a binary number: ")`

print("The decimal value for binary number {} is {}".format(number, convert_binary_to_decimal(number)))

Execute the program and provide a binary input:

`Please insert a binary number: 0b1100110`

The decimal value for binary number 0b1100110 is 102

It works!

# Conclusion

In this tutorial, we have seen how to work with binary numbers in Python and how to convert a binary number to decimal and a decimal number to the binary equivalent using Python built-in functions.

We have also created a custom function that performs this conversion in Python so we can include it in your Python code.

Now it’s your time to use what you have learned in your Python programs!

**Related article**: if you want to learn more about using Python for mathematics, have a look at this tutorial about calculating the square root of a number in Python.

*Originally published at **https://codefather.tech** on March 3, 2023.*