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:

hexadecimal = 0xf
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.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.