📢
18
c/coding-for-beginners•lily394lily394•2mo ago

My first Python script went from 50 lines to just 15...

I was trying to make a program that would rename a bunch of image files. My original version had a ton of loops and checks, it was messy. After a month, I learned about list comprehensions and the 'os' module from a free course. I rewrote the whole thing in one afternoon. The new script does the exact same job but is way cleaner. Do you think beginners should try to write compact code like this, or is it better to start with the longer, more obvious way?
3 comments

Log in to join the discussion

Log In
3 Comments
skyler_kim
skyler_kim2mo ago
Longer code first helps you really get why the fancy shortcuts work later.
6
john430
john4302mo ago
Wait, you rewrote the whole thing in one afternoon after just a month? That's a crazy fast jump from messy loops to clean code. Beginners should absolutely start with the long way, how else do you learn what the problems are? But once you see a better tool, why wouldn't you use it? The goal is to get the job done, not to keep writing the same clunky stuff forever.
3
grace419
grace4192mo ago
Oh man, "longer, more obvious way" is my entire coding history. My first scripts look like they were written by someone scared of the delete key. I say start messy, then get fancy when it clicks.
1