Tutorial Study Image

Python bin()

The bin function takes an integer as input and returns its equivalent binary string.

bin(integer) #where integer is an integer number
 

bin() Parameters:

The bin function takes one and only one mandatory parameter, that is an integer. If any non – integer parameter is passed, the method throws an type error.

Parameter Description Required / Optional
Integer An integer number whose binary equivalent to be calculated Required

bin() Return Value

The bin function returns a binary string. This binary string is equivalent binary of the integer parameter passed

Input output
Integer Binary string

Examples of bin() method in Python

Example 1 : Convert integer to equivalent binary in Python

int_num = 10
print('The binary equivalent of 10 is:', bin(int_num))
 

Output:

The binary equivalent of 10 is: 0b1010 
Here 0b indicates that its a binary number

Example 2 : Passing non integer parameter

int_num = 10.5
print('The binary equivalent of 10.5 is:', bin(int_num))

Output:

TypeError: 'float' object cannot be interpreted as an integer