bin() in Python
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course