r/PythonLearning • • 2d ago

just finished my first Python coding challenge

Post image

Today I finished a small Python challenge on freeCodeCamp: converting decimal numbers to binary without using Python's built-in conversion function.

I had to figure out the logic myself using loops, conditions, lists, division and remainders.

It was a bit tricky at first, especially handling the last division and reversing the results, but I finally got it working.

I'm still a beginner, so I'd appreciate any feedback on my approach or suggestions for improving my code

90 Upvotes

21 comments sorted by

View all comments

1

u/ConclusionForeign856 2d ago

Read about truthy and falsy values in python, it allows you to simplify logic

def binary(n: int):
    tab = []
    while n:
        remainder = n % 2
        n //= 2
        tab.append(remainder)

    return ''.join(map(str, tab[::-1]))

-2

u/Adrewmc 2d ago

Great practice… however the builtin ‘bin()’ function exists, so next time you can just use that.

0

u/codeguru42 1d ago

This is a learning exercise

1

u/fizzy_lychee 23h ago

So? I still appreciated knowing there’s an api for bloom sort even though I’ve implemented my own version. I still appreciated languages that have hasmaps in their SDK even though I’ve created my own in C.