Python bin() Function: Convert Decimal to Binary

In the Python programming language, the bin() function is a built-in function that converts a decimal number to a binary number prefixed with 0b.

A binary number is a number that comprises only two digits: 0 and 1. It is a number system that we normally use in computing and digital electronics.

In contrast, a decimal number is an integer number that comprises ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

The bin() function in Python takes an integer number as its argument and returns its binary prefixed with 0b equivalent as a string.

Python adds the prefix “0b” to all binary numbers to indicate that the number is in binary format.

Syntax of bin() function in Python


The general syntax of the bin() function in Python programming language is as:

bin(number)

In the above syntax, the parameter “number” is a decimal or an integer number we want to convert to binary. This function returns a string that represents the binary equivalent of the decimal number. Since the bin() function is a built-in function, meaning that we do not need to import any modules to use it.

Example of using bin() function


Let’s take an example in which we will convert integers into binary equivalents using bin() function.


Example 1:

# Python program to convert decimal numbers into binary equivalents.
num1 = 5
num2 = 10
num3 = -5

result1 = bin(num1)
result2 = bin(num2)
result3 = bin(num3)

print("Binary equivalent of 5 is: ", result1)
print("Binary equivalent of 10 is: ", result2)
print("Binary equivalent of -5 is: ", result3)
Output:
      Binary equivalent of 5 is:  0b101
      Binary equivalent of 10 is:  0b1010
      Binary equivalent of -5 is:  -0b101

In this example, we have assigned three values 5, 10, and -5 to variables num1, num2, and num3, respectively. Then, we have called the bin() function and passed the values of num1, num2, and num3, respectively.

The bin() function has returned the binary equivalents “0b101”, “0b1010”, and “-0b101” that we have stored into variables result1, result2, and result3. We then have printed the returned results on the console.

As you see in the output, the prefix 0b denotes that the number is in binary format. Notice that the Python bin() function can also convert negative numbers to binary.

However, the binary representation of negative numbers is more complicated than that of positive numbers because it involves the use of two’s complement representation.

Converting binary numbers to decimal numbers


We can also convert binary numbers to decimal numbers using the int() function. The int() function takes two arguments: the first argument is the binary number, and the second argument is the base of the number system. In the case of binary number system, the base is 2.

Let’s take an example of how to convert a binary number to a decimal number using int() function.

Example 2:

# Python program to convert a binary number to decimal number.
b_num = "0b1010"

result = int(b_num, 2)
print("Decimal equivalent of 0b101 is: ", result)
Output:
      Decimal equivalent of 0b101 is:  10

In this example, we first have assigned the value ‘0b1010’ to the variable b_num. We then passed the value of b_num and the base 2 to the int() function, which returned the decimal equivalent of ‘0b1010’, which is 10.

Limitations of bin() function in Python


Python bin() function has some limitations you should be aware of. First, the function only works with integer numbers. If we pass a floating-point number to the function, it will raise a TypeError.

Example 3:

# Python program to convert float number into binary.
num = 2.5
result = bin(num)
print("Binary equivalent of 2.5 is: ", result)
Output:
       TypeError: 'float' object cannot be interpreted as an integer

As you can see, if we pass a number of any other data type as an argument, Python interpreter returns a TypeError exception. Hence, if you want to convert a number of any other data type to a binary number, you will need to use a different function or method.

Second, the bin() function in Python adds the prefix 0b to the binary number, which may not be desirable in all cases. Finally, the function returns the binary number as a string, which may not be convenient if we want to perform arithmetic operations on the binary number.


In this tutorial, you have learned about how to use bin() function in Python to convert a decimal number to a binary equivalent. Hope that you will have understood the basic points of bin() function and practiced all example programs.
Thanks for reading!!!

Please share your love