Updated April 3, 2023
Introduction to Binary number in Python
Python provides the binary number system to the user, in which we convert binary numbers to decimal numbers, vice versa, and binary to octal number system, which means as per our requirement, we can convert the binary numbers into the other number system. Normally a binary number system we can represent by using the 0 or 1 digit in the system. The base of the binary number system, or we can also call radix, is 2. Possible digits in the binary number system are 0 and 1, which means we can only use 0 and 1 in the binary number system.
Syntax
There is different syntax available in python as follows.
Function (specified_variable_name for decimal number):
If specified_variable_name >= 1:
then (specified_variable_name /2)
print (specified_variable_name % 2, end = ‘ ‘)
Explanation
In the above syntax, we use a user-defined function; after that, we check the number is greater than or equal to 1. If the condition is true, then it executes the following code as shown syntax.
Another type of syntax is as follows.
A = 0b binary number
print (A)
Explanation
In the above syntax, we first declared the variable and assigned a binary number; note here binary number should start with the 0b keywords as shown in the above syntax.
How Binary number works in Python?
Now let’s see how the binary number system works in python as follows.
In Python, we can basically utilize the bin() function to change over from a decimal value to its corresponding value. Also, comparatively, the int() function is used to convert binary numbers into decimal numbers. The int() function takes as second contention the foundation of the number to be changed over, which is 2 if there should be an occurrence of binary numbers.
Binary numbers are the number addressed with base two. This implies in the binary number system; there are just two digits used to address binary numbers that are 0 and 1. At the point when you include up from zero in parallel, you run out of digits all the more rapidly: 0, 1?
Besides, there are no more digits left. You don’t go to the digit 2 since 2 doesn’t exist in the binary number system. All things being equal, you utilize an exceptional blend of 1s and 0s. In a double framework, 1010 is equivalent to 10 in decimal. In the binary number system, you use forces of two, which implies 10 is fundamentally: (1(2^3)) + (0(2^2)) + (1(2^1)) + (0(2^0))= 10. The position of the 1 and 0 characterizes the capacity to which 2 is to be raised.
Examples of Binary number in Python
Now let’s see different examples of binary number systems as follows.
def DeciToBinaryNumber(number):
if number >= 1:
DeciToBinaryNumber(number // 2)
print(number % 2, end='')
if __name__ == '__main__':
dec_number = 10
DeciToBinaryNumber(dec_number)
Explanation
In the above example, we try to implement the recursive solution. In this example, we first create the new function name as DeciToBinaryNumber after that, we check the number is greater than or equal to 1. If the condition is true, then it executes the below part of the program, dividing the decimal number by 2 and takes the reminder as a binary number from bottom to top direction. The bottom part of the program contains the driver code, in which we pass a decimal number that we need to convert into a binary number. In this example, we try to convert 10 into a binary number. The final output of the above statement we illustrate by using the following snapshot.
Now let’s see another example of a binary number system by using the inbuilt function as follows.
def deciToBinaryNumber(number):
return bin(number).replace("0b", "")
if __name__ == '__main__':
print(deciToBinaryNumber(4))
print(deciToBinaryNumber(15))
print(deciToBinaryNumber(9))
Explanation
In this example, the only difference is that here we use the built function of python that is the bin() function. As well as here, we also use 0b to convert the decimal number system into the binary number. The second part of the program contains the driver code, and here we call deciToBinaryNumber with a decimal number as shown in the above program. By using the above code, we convert 4, 15, and 9 decimal numbers into a binary numbers. The final output of the above statement we illustrate by using the following snapshot.
Now let’s see another example of a binary number system without using in – built function as follows.
def deciToBinaryNumber(number):
return "{0:b}".format(int(number))
if __name__ == '__main__':
print(deciToBinaryNumber(4))
print(deciToBinaryNumber(15))
print(deciToBinaryNumber(9))
Explanation
In this example, the only difference is that here we try to implement the above program without the python’s in-built function without the bin () function. As well as here, we also use 0b to convert the decimal number system into the binary number. The second part of the program contains the driver code, and here we call deciToBinaryNumber with a decimal number as shown in the above program. By using the above code, we convert 4, 15, and 9 decimal numbers into a binary numbers. The final output of the above statement we illustrate by using the following snapshot.
Now let’s convert binary numbers into decimal numbers as follows.
def binaryToDecimalNumber(b_number):
binary = b_number
dec_number, j, num = 0, 0, 0
while (b_number != 0):
decimal = b_number % 10
dec_number = dec_number + decimal * pow(2, j)
b_number = b_number // 10
j += 1
print(dec_number)
if __name__ == '__main__':
binaryToDecimalNumber(1010)
binaryToDecimalNumber(1110)
binaryToDecimalNumber(1001)
Explanation
By using the above program, we try to implement a decimal number into the binary number as shown in the above program. The final output of the above statement we illustrate by using the following snapshot.
Now see the same program we can implement by using the int () function as follows.
def binaryToDecimalNumber(b_number):
return int(b_number, 2)
if __name__ == '__main__':
print(binaryToDecimalNumber('1010'))
print(binaryToDecimalNumber('1110'))
print(binaryToDecimalNumber('1001'))
Explanation
In the above example, we use the int() function to convert a decimal number into a binary number, as shown in the above program. The final output of the above statement we illustrate by using the following snapshot.
So in this way, we can convert binary numbers to any number system as per requirement.
Conclusion
We hope from this article you learn the binary number in python. From the above article, we have learned the basic syntax of binary numbers, and we also see different examples of binary numbers. From this article, we learned how and when we use the binary number in python.
Recommended Articles
This is a guide to Binary numbers in Python. Here we discuss the basic syntax of binary numbers, and we also see different examples. You may also look at the following articles to learn more –