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 email@example.com. 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.
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Important differences between Python 2.x and Python 3.x with examples
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Visualizing O(n) using Python
- Any & All in Python
- SHA in Python
- abs() in Python
- pow() in Python