bin() converts an integer number to its binary representation.
It returns a binary representation of the given number with the prefix 0b.
Example:
n = 5
result = bin(n)
print(result) # Output: 0b101
Syntax
bin(num)
Parameters
bin() takes a single parameter; an integer value.
Return Value
- Binary Equivalent of the given integer.
- TypeError for any non-integer data
Example
Use of bin()
n = 22
print('Binary equivalent of 22 is',bin(n))
# won't work
x = 12.2
print(bin(x))
Output
Binary equivalent of 22 is 0b10110
TypeError: 'float' object cannot be interpreted as an integer