Converting an decimal to binary is always an important utility that is required in python dev projects and competitive programming as well. Having a shorthand function to achieve this can always be handy in the situations which require the swift conversion of it without writing a long code for same, this is provided by “bin()“. This has been discussed in this article.
1. Using recursion
1000 10010 111
2. Using loop
The binary representation of 100 (using loops) is : 001100100
Using bin() reduces the time required to code and also removes the hassle that you may see in the above two mentioned methods.
Syntax : bin(a) Parameters : a : an integer to convert Return Value : A binary string of an integer or int object. Exceptions : Raises TypeError when a float value is sent in arguments.
The binary representation of 100 (using bin()) is : 1100100
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Merge Python key values to list
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- gcd() in Python
- SHA in Python
- pow() in Python
- Python vs PHP
- Any & All in Python
- SQL using Python | Set 1
- chr() in Python