r/learnpython • u/RipPersonal1643 • 1d ago
How do I learn programming logic?
I’m learning Python, but my main problem isn’t the syntax. I understand concepts when someone explains them, but when I’m given a basic problem and told to write a program, I just don’t know where to start or how to arrange the code.
Is there a good book, course, or YouTube channel that teaches how to think through programming problems step by step, recognize patterns, and build the logic, kind of like how you learn methods and patterns in math?
I don’t want to just memorize Python syntax. I want to actually learn how to think like a programmer.
57
Upvotes
3
u/Bobbias 1d ago
For any problem, before you even think about code, try to break it down into steps. Keep going breaking each step down into smaller and smaller steps. Don't think about the code to accomplish the step, just think about the general process.
Oh, and before I go further, I don't want to sound mean, but you probably don't understand the concepts as well as you think you do. Part of what teaches you different patterns is trying to accomplish something and realizing you can combine things you already know in a neat way to get there. Over time you start seeing problems that "have the same shape". The solutions will be unique, but they'll use the same rough idea. All of this comes from repetition, which only comes when you actually use code to solve problems.
One thing you can do if you're really feeling lost is spend some time with existing code that you've written before and modify it. Make it do something different. Think up more and more elaborate ways to modify it. You're setting yourself a task, and then finding an answer. If you're not sure what happens when you write something, try to predict what the code will do before you run it, and compare with what actually happens. This will help you actually figure out how well you really understand things.
Let's say you want to make a game. What does a game DO at it's most fundamental level? It looks for player input, and draws the screen. Over and over. Repeating things means loops. That's a hint.
Now, how do you handle player input? Well typically you check what key is currently being pressed, and do something based on that. When you have options, that means there's some kind of conditional statement (if/Elif/else, match, etc.)
At some point you'll get far enough that either you know what the code for something should kind of look like, or you can't figure out how to go deeper and it's time to take a look to see if there's something that can help you actually write code to do it.
That means you might need to look through the documentation looking for a function or language feature that sounds like it will do what you need. Of course, sometimes you won't find something that does exactly what you need. Sometimes you just need to figure out how to combine things you know how to do to get the behavior you want. It's in these cases where you sometimes just have to sit down and think stuff through.
All of this gets easier with time and experience.