The popular number system types named βBinary Numbersβ are represented by β1β or β0β and are expressed in base 2. In Python, the β0bβ is the string format representation of the binary. Sometimes, while dealing with a number system in Python, we need to convert the different number systems into a binary system. To do this the βbin()β method is utilized in Python.
This Python tutorial will offer you a comprehensive tutorial on the Python βbin()β function via the below contents:
- What is the βbin()β Function in Python?
- Retrieving the Binary Representation of an Integer
- Retrieving the Binary Representation of Non-Integer
- Retrieving the Binary Representation of Integer Using User-Defined Function
- Retrieving the Binary Representation of Custom Object Using the βbin()β Function and βindex()β Method
- Retrieving the Binary Representation of Hexadecimal and Octal Value
What is the βbin()β Function in Python?
The βbin()β function in Python is a built-in function that converts an integer or a string to its binary representation. The prefix β0bβ will always precede the retrieved value.
Syntax
Parameters
In this syntax, the βnumberβ parameter represents an integer number that needs to be converted into its binary representation.
Return Value
If an integer is given, the βbin()β method returns a string that represents its binary equivalent. Otherwise, it raises a TypeError for a non-integer argument
Example 1: Retrieving the Binary Representation of Integer
In this code, the integer βnumβ variable is passed to the βbin()β method to retrieve the binary representation:
print(bin(num))
The above code retrieves the binary representation:
Example 2: Retrieving the Binary Representation of Non-Integer
In the below code, the βnon-integerβ value is passed to the βbin()β method to retrieve the TypeError. Take the following code as an example:
print(bin(num))
According to the following output, the above code retrieves the TypeError:
Example 3: Retrieving the Binary Representation of Integer Using User-Defined Function
We defined a function that accepts an integer as an argument and returns its binary form without the β0bβ prefix that indicates a binary number:
n = bin(num)
return n[2:]
print(bin_func(15))
The above code retrieves the binary representation:
Example 4: Retrieving the Binary Representation of Custom Object Using the βbin()β Function and βindex()β Method
Here, the class named βnumβ is defined with an attribute named βnumβ. Next, the class special method called β__index__β is defined. This method defines how the object can be converted into an integer and retrieve the attribute value. The bin() function is called on the instance of class num:
num = 15
def __index__(self):
return self.num
print(bin(num()))
The above output retrieves the below output:
Example 5: Retrieving the Binary Representation of Hexadecimal and Octal Value
In the below-provided code block, the βbin()β function takes the hexadecimal and octal representation value as well as retrieves the binary representation:
octal = 0o17
print(bin(hexadecimal))
print(bin(octal))
The binary representation of the input code is shown below:
Conclusion
In Python, the βbin()β function is utilized to convert a decimal, octal, or hexadecimal value into its binary representation. This function retrieves the TypeError when the non-integer is passed as an argument. We can also use the βbin()β function along with the βindex()β method to convert the custom object to binary representation. This tutorial demonstrated a comprehensive tutorial on Python’s βbin()β function.