Python

Python bin() Function

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?

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

bin(number)

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:

num = 24
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:

num = 'one'
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:

def bin_func(num):
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:

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(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.