The bin function takes an integer as input and returns its equivalent binary string.
bin(integer) #where integer is an integer number
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 |
The bin function returns a binary string. This binary string is equivalent binary of the integer parameter passed
Input | output |
---|---|
Integer | Binary string |
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
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