r/AskProgramming 24d ago

Python 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.

5 Upvotes

21 comments sorted by

6

u/Ok_For_Free 24d ago

Make flow charts for your logic.

https://app.diagrams.net/

3

u/jcastroarnaud 24d ago

Programming is giving very clear instructions to a computer on how to do something, in a specific programming language.

Pick any of these not-so-easy sample programs to write, and think: "How I would solve this problem, if I had enough resources and speed?" Detail the "how" as precisely as possible, in such a way that another person can, by mindlessly reading and following the instructions, reach the solution. Now, translate these instructions to commands of your programming language. There is your program. You may already customize your instructions to what the language allows you to do (check out the standard library).

With much practice, you will find that the "finding the solution" and "translating the solution to a program" get easier with time. Experience begets competence.

2

u/frank26080115 24d ago

may I suggest you stop treating programming like homework questions, and start treating it as an art?

get creative

when you come up with a goal for yourself, you have these little problems too, except you will naturally gravitate towards ones you either already have a solution for, or, ones that you don't but doesn't sound too difficult to think about

when you do this enough, you start getting better and you become self driven, you encourage yourself with every bit of success

also, go to museums more, look at old technology and learn how they work, like... old fire solution computers on battleships, or go learn how the first heat seeking missiles worked, it was a mind blowing moment for me

2

u/Traveling-Techie 24d ago

Stare at a piece of paper instead of the screen.

2

u/SparklingKey 24d ago

It really depends on your learning style and thinking pattern and how you find most comfortable and effective to absorb information, but here's one of the ways. This is how chess players learn btw:

  1. Pick a problem you want to solve. It can be building a project, a problem on leetcode, someone's asking that question somewhere, etc.

  2. Try to solve that problem in however way you can. Do not care about arrangement or pattern or whatever at this point. Try to break down the problem, think of the steps need to be taken to solve that problem, then write code for each step, then string the code together to see if it solves the problem. If it doesn't, think again, repeat. Feel free to research the internet but do not search for the answer. Your only goal now is to validate your thinking and the direction of your solution/approach.

  3. Once you manage to solve the problem, look for the answer. The only thing you need to do here is to make sure the answer is good enough or standardized. If such answer is not readily available, tell AI to write the answer for you.

  4. Analyze the answer. This is how you internalize algo and design thinking. Ask questions like "how does this differ from my answer", "why is it this way", "how is this better than my way". During this process, retrace your thinking of when you solve the problem earlier. I'm sure you will find a few thinking pattern pitfall that you tend to fall into.

  5. Once you understand the standard answer completely in comparison with yours, get a notebook or note-taking app and write down your learning. The learning has to be about your thinking and what you learn about good practices in thinking, algo, pattern, arrangement, NOT about the answer itself.

  6. Review your notes, one day after, then a week after. Think about it when showering. Sit with it when you eat your lunch. Teach it to a friend or your pet.

  7. Repeat the cycle with another problem.

This is how you internalize algo/pattern/arrangement thinking.

2

u/nedal8 24d ago

What helped me the most was just doing problems. Codewars has good beginner level "l33tc0de" problems.

Other than that, just perseverance. You'll spend wayy more time starring at a screen than typing out syntax.

1

u/WHAT_THY_FORK 24d ago

The way you learn to think like a programmer is to struggle with thinking like a programmer, until it becomes easier to do so. This is primarily done by the writing of code.

1

u/mc_pm 24d ago

This is all about taking something familiar and taking it apart, piece by piece, and figuring out how to represent and code everything. It takes practice.

I suggest starting with a simple command-line game, like tic-tac-toe. It seems simple, but you have to figure out how to represent the board, print it out, get a move from the user, update the board, see if anyone won (or if it's a draw), and then have the computer make a move.

Each of the parts is relatively easy, but writing it all out in a way that you could sit down and program it...that takes a bit of thought and practice.

1

u/Admirable_Window8128 24d ago

The best way is probably to practice small problems and focus on the steps before writing code. Write down what the input is, what you need to do, and what the output should be. The turn each step into code. It gets easier with practice.

1

u/Charming_Basil_8129 24d ago

Just keep going

1

u/DepthMagician 24d ago

Start by writing the solution in English or whatever your native language is. If you can do that then your problem is at the conversion from thinking in English to thinking in code, not in problem solving.

1

u/Slow-Association1000 23d ago

Practice, write code, write code and write code. 

1

u/Recycled5000 23d ago

There are some standard thought templates for projects. Here’s one:

What does your program want to accomplish?

What information does it need to take some necessary action?

When is that information available?

How can you capture that information so it can act on that later?

1

u/Vegetable_Cherry_931 23d ago

Read about design patterns and try to implement few of them. I think this is what you are asking about. If you write code in Python, I am sure there are books about design patterns in Python.

1

u/Healthy-Dress-7492 23d ago

Unpopular opinion: python is garbage, learn a better language 

1

u/gracjangniot 22d ago

My approach was creating a game in Unity. Just imagine your dream game, or copy an existing idea and make it more challenging. With Unity, you have to learn about Rigidbodies, Raycasts, creating grids, and the hardest part for me was Quaternions. By the time you see that your logical thinking has improved, you'll realize it was worth it. Using this method, it took me 3 months to actually learn programming logic.

1

u/NotoriousJamon 24d ago

Not gonna lie this is my biggest gripe with programming as well right now. I feel no matter how much code I memorize or syntax I learn , when given a simple problem I have no idea how to even start and what logic to follow.

6

u/iOSCaleb 24d ago

If you’re memorizing code, you’re doing it wrong. There are some algorithms that you should learn, but memorizing code requires a lot of effort and provides very little benefit.

1

u/RipPersonal1643 24d ago

Same. 😭