Python bin() Function
The Python bin() function is used to convert a specified number (positive or negative integers only) into binary numeral system. It returns a binary string which always starts with 0b followed by binary value of the specified number.
Syntax
bin(number)
Parameters
number |
Required. specify a positive or negative integer. |
The table below shows decimal numbers converted into binary numbers.
Decimal | Binary | Decimal | Binary |
---|---|---|---|
0 | 0 | 15 | 1111 |
1 | 1 | 16 | 10000 |
2 | 10 | 31 | 11111 |
3 | 11 | 32 | 100000 |
4 | 100 | 63 | 111111 |
5 | 101 | 64 | 1000000 |
6 | 110 | 127 | 1111111 |
7 | 111 | 128 | 10000000 |
8 | 1000 | 500 | 111110100 |
Example:
The example below shows how to convert decimal number into binary number in Python.
MyNumber = bin(5) print(MyNumber) MyNumber = bin(64) print(MyNumber)
The output of the above code will be:
0b101 0b1000000
❮ Python Built-in Functions