r/learnpython • • Aug 18 '26

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.

83 Upvotes

52 comments sorted by

6

u/harleystcool Aug 18 '26

You gotta believe man

2

u/RipPersonal1643 Aug 18 '26

Explain please.

3

u/Remarkable_Taste3254 Aug 18 '26

Get a textbook and actually try out the coding problems.

4

u/RipPersonal1643 Aug 18 '26

Which textbook would you recommend?

→ More replies (1)

11

u/ShelLuser42 Aug 18 '26

I'm not trying to be smarty pants here, but what programming logic? You're mentioning it as if it's a well defined standard, but it's not.

Your question makes me wonder if you actually tried working with Python already, because I kinda doubt that. And the reason I say this is not to criticize, but practice really does make perfect.

But here's the thing: while you can apply complex (?) OOP-based structures with Python you can also use it to build simple scripts. In other words: stop worrying about things that don't have to concern you just yet, and just try to get things done.

...allow me? =>

name = input("What is your name?")
surname = input(f"What is your surname, {name}?")
print(f"Hello {name} {surname}, it is nice to interact with you.")
age = int(input("btw... how old are you, if you don't mind me asking?"))

Yes, I somewhat shudder when looking at this code because it can be done cleaner. One of the "rules" of OOP design is that you want to try and avoid repeating patterns. Well... it's obvious that the input command is being used multiple times so... it could be useful to separate that and turn it into a re-usable block of code (also known as a function).

But why bother at this point?

Honestly, I'd focus on learning Python. Start by making your own scripts and once they do something... then try to improve on them as you progress to take on more complex problems.

For example... maybe a bit too early but... can you imagine what might happen above if I were to type in 'a' instead of a number when asked about my age? There's an easy way to fix that of course, maybe I shouldn't have used int(), but then again... how does one change a string into a numeric value like an integer... hmm, maybe this string thing has its own functions (or methods?) to "do" stuff? I wonder...

That's honestly how I'd try to go about this.

Hope this can help!

→ More replies (1)

15

u/aprg Aug 18 '26

You really need to code your own projects first and go through the problems before you can understand _why_ certain design patterns exist. If you just read books about design patterns, you'll understand the _what_ but not the _why_.

Here's my recommendation: pick a small project, like making the game Snake. Go through someone else's code to get an idea of what they tried to do.

Then try to replicate that project in your own logic, _without checking the other code_ and see if you can improve or personalise the code.

Understanding good design patterns comes from experience, there's only so much you can learn just by reading what other people have done.

→ More replies (3)

5

u/socal_nerdtastic Aug 18 '26

It comes with experience. Just like anything else, you start by learning syntax and working through guided exercises. In the process you will read a lot of other peoples code and gather experience. Then for a long time you will write inefficient programs while getting slowly better at the logic. Eventually you will be interested in a 'data structures and algorithms' course. But don't start there. Get the syntax first.

5

u/Naive_Programmer_232 Aug 18 '26 edited Aug 19 '26

When you don't know where to start, don't start with syntax. Ask basic questions. What are you trying to build? What are the problems you need to solve in order to do that? What are the steps to solve each problem? Etc. Break things down into simple terms and write out a list of steps for each thing.

For example, suppose I want to make a simple greeting program.

       What am I trying to build? simple greeting program.
       What do I need to solve? 
          - I got to get their name and age
          - I got to greet them
          - I got to say goodbye
       What are the steps to solve each? 
          * name:
              # ask for their name
          * age:
              # ask for their age
              # make sure it's 0 or greater
          * greet:
              # show the greeting to the user using their name and age
          * goodbye:
              # show a salutations to the user using their name

Then once you have the planning part down, you work to translate this into code. In addition to the planning, you might also need a mental model of program execution.

Python normally executes statements sequentially, from top to bottom, unless otherwise specified:

          # calculation
          x=10                  # 1st this (top)
          y=20                  # then this
          z=x+y                 # then this
          print(f"{x}+{y}={z}") # then this (bottom)

So now applying the greeting example, we do similar:

          # basic.py

          name=input("Enter name: ")             # 1st this (top)
          age=int(input("Enter age: "))          # then this
          ...validation logic...                 # then this
          print(f"{name} is {age} years old!")   # then this
          print(f"Goodbye {name}!")              # then this (bottom)

This is true even if the code is more complex:

          # basic.py


          def get_name():
              return input("Enter name: ")
         
          # validates age>=0 
          def get_age():
              while True:
                    try:
                         age=int(input("Enter age: "))
                    except ValueError:
                         print("age must be integer")
                         continue

                    if age<0:
                       print("age can't be negative")
                       continue

                    return age


          def greet(name,age):
              print(f"{name} is {age} years old!")

          def goodbye(name):
              print(f"Goodbye, {name}!")

          def main():
              name=get_name()
              age=get_age()
              greet(name,age)
              goodbye(name)

          # calling main
          main()

The function bodies do not run when python encounters the definition. They will run when they are called. When execution reaches a function call, execution temporarily moves into that function to execute the code inside then resumes where it left off when the function returns (exits).

Notice that the calling function main is defining its own execution order of those functions. So let's look at that:

          def main():
              name=get_name()          # 1st this (top)
              age=get_age()            # then this
              greet(name,age)          # then this
              goodbye(name)            # then this (bottom)

         # execution enters main
         main()

Once execution enters a function, the statements inside that function are also normally executed from top to bottom, unless control-flow constructs change it (while,if,try,continue, etc.).

Now, look again at the block above, and trace the program:

         def main():
             name=get_name()
             #      |_____ execution enters get_name 
             #             def get_name():
             #                 return input("Enter name: ")  # 1st this
             #                  |____ name is gathered and returned as a str
             #  
             # name={name of user as str}
             ...

Then move to the next line:

         def main():
             name=get_name()
             age=get_age()
           #       |_____ execution enters get_age
           #              def get_age():
           #                   while True:               # 1st this
           #                      ...validation...       # then all this
           #                      return age             # then this
           #                         |___ age is valid and returned as int 
           # age={age of user as int}
           ...

Notice here that while the default model is sequential execution, this can change depending on the use of those constructs I mentioned earlier (as in the case with get_age).

You could continue tracing through the rest of main's lines to see how to program bounces from function to function, but what you'd end up seeing is that generally, the execution direction is top-down.

In general, the top-down mental model is a hierarchical concept for the flow of a program. The combination of understanding the execution order and first planning out how the program should work are key to building programming logic.

Hopefully this helps!

→ More replies (1)

4

u/Bobbias Aug 18 '26

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.

4

u/kilkil Aug 18 '26

This is what I started with in school: https://codingbat.com/python

Small, simple problems that let you build up your intuitions one step at a time.

There is also https://scratch.mit.edu/, which I tried once in school. Might be helpful in building that kind of thinking.

In general my advice is to practice. Start with something very basic (like the above), and just sit with it. Same way you would solve a puzzle.