DEV Community

Cover image for Need help to understand the following code
Karmabir Brahma
Karmabir Brahma

Posted on

Need help to understand the following code

Please help me to understand the above code

Top comments (1)

Collapse
 
bchhun profile image
Bernard Chhun • Edited

First, you need to figure out the complicated parts:

>>> 5//2
2
>>> 5/2
2.5

I googled "python double slash" and reached this page: pythonpool.com/python-double-slash/

then you need to figure out what the print statement does.

Trying the function out, I'd say it displays the binary version of the number you used as a starting parameter:

>>> def dec(num):
...     if num > 1:
...             dec(num//2)
...     print(num%2,end='')
...
>>> dec(2)
10>>>
>>> dec(3)
11>>>

The recursive part might throw you off as well :)

It really looks like a code golfing challenge if you ask me !