This will not work as expected, as you have print in two places, you will end up with multiple lines, e.g.:
>>> def binary(n):
... if n < 2:
... print n
... else:
... binary(n / 2)
... print n % 2
...
>>> binary(0)
0
>>> binary(1)
1
>>> binary(3)
1
1
>>> binary(9)
1
0
0
1
>>> binary(10)
1
0
1
0
Other answers use strings, so here's one with lists: :)
>>> def binary(n):
... if n < 2:
... return [n]
... else:
... return binary(n / 2) + [n % 2]
...
>>> binary(0)
[0]
>>> binary(1)
[1]
>>> binary(3)
[1, 1]
>>> binary(9)
[1, 0, 0, 1]
>>> binary(10)
[1, 0, 1, 0]
and if you really want a string, it's as simple as this: :)
>>> ''.join(map(str, binary(10)))
'1010'
Of course, since you've already found out about the function bin, you should probably have done this in the first place:
>>> bin(10)[2:]
'1010'
How this reminds me of this:
Happy coding! :)