Python bin Method in Simple Words with Example

Python programming language has tons of built-in functions and one of them is the bin() method.

This tutorial will give you the in-depths of the Python bin() method with examples. It will guide you through the function and application of the bin() method.

First things first,

What is a bin in Python?

A bin is a method in Python and serves as an abbreviation of the word “binary“. This method takes an integer and returns its binary value in string format.

In short, the bin() method converts an integer into its binary equivalents (decimal, hexadecimal, or octal).

However, if an input is not an integer, the programmer will need to implement the _index_() method to obtain an integer value, or else, the program will raise the “TypeError” exception.

Python bin()

To understand how the Python bin() method works, let’s take a look at the following:

Example Program

integer = 5

a = bin(integer)
print('The binary equivalent of 5 is', a)

This example program shows the basic function of bin() where it returns the binary equivalent of integer 5.

Output

The binary equivalent of 5 is 0b101

Syntax

Python bin() syntax is used in programs to invoke the bin() method.

bin(integer)

Parameter

The bin() method only takes an integer (number) as the function’s parameter. Unless the _index_() method is used, the program throws the “ValueError” exception for anything other than integers. 

Return Value

This bin() method returns the binary equivalent of an integer.

For other types of objects such as strings, the method will return an error exception.

bin() in Python example program

In this section, we will try using the bin() method with an integer. Then we will discuss how each line of code process the program as a whole afterward.

Example Program

integer = 14

a = bin(integer)
print('The binary equivalent of 14 is', a)

Output

The binary equivalent of 14 is 0b1110

Program Explanation

The first line integer = 14 is the declaration of the integer 14.

To return the corresponding binary equivalent of the integer, we apply the bin() method through a = bin(integer). The a here is the variable containing the equivalent binary value of the integer.

The print() function then enables the program to display the value of a (the binary equivalent of 14) which is 1110. The output includes the prefix 0b to indicate that the output is a binary value.

Also read: Python Dotenv (Explanation with Examples)

bin() example program with a non-integer class

In the next example, the program will show how to apply the Python bin() to non-integer classes or objects.

Example Program

class Quantity:
    a = 5
    b = 5
    c = 5
    
    def func():
        return a + b + c
        
print('The binary equivalent of quantity is:', bin(Quantity()))

Output:

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    print('The binary equivalent of quantity is:', bin(Quantity()))
TypeError: 'Quantity' object cannot be interpreted as an integer

Program Explanation

In this example, the quantity class holds the value of variables a, b, and c. To combine the quantity of the three variables, we use the def func(): and will return the sum of quantity through return a + b + c.

However, the Python bin() method only operates for integer values and as for the example given, the output raised the TypError explaining that the ‘Quantity’ object cannot be interpreted as an integer.

This example supports the fact that the bin() method does not take non-integer values, only integers or numbers.

In contrast to this situation, programmers should implement the _index_() method to enable the bin() method for non-integer values.

bin() with __index__() for Non-Integer Class

Now, let us have an example program where we apply the Python bin() method in a non-integer class but also apply the _index_() function.

Example Program

class Quantity:
    a = 5
    b = 5
    c = 5
    
    def __index__(x):
        return x.a + x.b + x.c
        
print('The binary equivalent of quantity is:', bin(Quantity()))

Output:

The binary equivalent of quantity is: 0b1111

Program Explanation

Noticeably, this example program is identical to the example above except that we apply the _index_() function. The _index_() function enables the program to identify the quantity values of variables a, b, and c through x.

The program also includes adding all of the quantity values before displaying its equivalent binary value in the output.

Therefore, the equivalent binary representation of 15 (5 + 5 + 5) is 1111.

Using Python bin() with float

Another useful example below will help you with understanding how the Python bin() method works with float objects or decimals.

Example Program

sample_float = 12.14
print(bin(sample_float))

Output:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    print(bin(sample_float))
TypeError: 'float' object cannot be interpreted as an integer

Program Explanation

The simple example tries to apply the bin() method with a float or decimal values.

However, the output clarifies that Python bin() does not take float values. This statement is concluded based on the Typeerror messages which say “'float' object cannot be interpreted as an integer“.

Summary

In summary, this tutorial covers the topics and discussions to explain what is a Python bin(0 method. This also clarifies how the method works with different kinds of objects or inputs that programmers may use and encounter.

Therefore, the Python bin() method is useful for returning the binary representation of any integer.

Take note that this method is only working for integer values and strings with the _index_() function.

The bottom line is the bin method is only for integers and or numbers.

Leave a Comment