So yeah I'm trying to figure a way on how to change a decimal into a binary in python
2 Answers
How about bin:
>>> bin(42)
'0b101010'
In [1]: def dec2bin(n):
...: if not n:
...: return ''
...: else:
...: return dec2bin(n/2) + str(n%2)
...:
In [2]: dec2bin(11)
Out[2]: '1011'
2 Comments
arshajii
Looks rather convoluted given that there are already standard functions to do just that.
inspectorG4dget
@arshajii: Yeah, but I figured I'd give a DIY answer for completeness, just in case that's what OP was after