We have discussed in the previous tutorials, how to handle different types of number systems in python language. In this tutorials we are going to see how to convert any number into python binary number.
Number to Python Binary Number :
We can convert any number (whether it is octal, hexadecimal or decimal) in to a binary number in python.
To do this python gives us bin method. python bin() method takes the number as a parameter and convert it as corresponding binary number. Lets see how it works with all numbers.
Decimal to Python bin() :
I am going to convert a decimal number to binary number.
>>>x= 1234 >>>bin(x) >>>'0b10011010010'
Octal to Python bin() :
I am going to convert a octal number to binary number.
>>>x=0o127 >>>bin(x) >>>'0b1010111'
Hexadecimal to Python bin () :
I am going to convert a hexadecimal number to python binary number.
>>>x=0x235f >>>bin(x) >>>'0b10001101011111'
Note: we can even pass the binary number to bin() method, but it differ nothing.
Happy Learning 🙂