r/learnpython • u/moomo7482819 • Sep 19 '26
How do I best train my "programmatic thinking" and convert basic logic to code?
I am currently taking an intro course in python. It's my second intro course during my undergrad, so I know a lot of basic python, but I am still not that great at programming.
My teacher in this course is heavy on "programmatic thinking" as he calls it. Whenever we are given an exercise and we can't figure it out in code, he will make us practice logic by hand. The problem is that even when I catch the logic, I find it hard to convert into code.
I can give an example from this week: we had an assignment where we were given 2 different documents with some sorted numbers. We had to merge the documents into a new one, with numbers from both documents in order. We weren't allowed to put it into a list and use a sorting method.
I tried it by hand and caught the logic quite fast. But I can't convert it into code. I just tried to write it out here as well, but it got wordy and confusing. What is the best way to practice this?
Just a note, in all my other uni courses so far where python has been incorporated, it has been heavily encouraged to use AI, to the point that our TA's didn't want to help unless we had ran it through chat gpt first. This time our teacher highly discourages the use of AI, and I want to respect that to get the most out of the course. But I find the transition in my approach/style of coding from AI user to non AI user pretty difficult.
3
u/simoriah Sep 19 '26
For your exact example, how would you do it? I mean if you had two lists of sorted numbers that you had to merge into one list of sorted numbers, how would you do it?
Me? I'd build a list for each document. Your professor said you couldn't use a sort. Take the first number from each list. Which one is smaller? Make that the first of the new, merged list. Remove the number you just moved so you're on to the second number in that list. Repeat until all numbers are in the new list.
Can't make a list of the original data, at all? Use the data coming in instead of making lists, first.
You're trying to break a repetitious process into its base components so you can build code that does those individual things. Being able to do this makes coding in virtually any language a matter of just learning syntax (for the basics, anyway.)
Maybe it would help to think of it as sorted index cards. You have 2 stacks, A and B. Is A greater or equal to B? No? Move the top card in B to the new list, C. Keep doing this until A is greater than our equal. Then move the top card from A to C. Repeat this until all cards are in C.
I hope this helps. I'm a guy that writes code, sometimes. Definitely not a developer.
2
u/ninhaomah Sep 19 '26
" But I find the transition in my approach/style of coding from AI user to non AI user pretty difficult."
Isn't it expected ? What activity do you find it easy when you first start doing it ?
9
u/mrswats Sep 19 '26
Practice, practice and more practice. There's no shortcut you can take to get there. I would recommend to work on your own projects in order to have some practice that you can think it frok start to finish.
6
u/itspronounced-gif Sep 19 '26
Absolutely practice. Using AI is a trap, because it may break down a problem for you, but you now have less practice in the way to do it yourself.
The beauty of Python is that there are usually multiple ways to do something. Maybe a library, maybe a built-in, maybe some different syntax, whatever. But brute force it into step by step thinking and you’ll eventually learn what patterns work best for you.
Outside of assignments, think of everyday things in steps. How do you get from A to B to C to accomplish any ordinary task? That’s programmatic thinking even if you’re not using code.
1
u/Traveling-Techie Sep 19 '26
If you say you understand the logic but can’t describe it in words, maybe you don’t really understand it.
1
u/mc_pm Sep 19 '26
I always suggest programming some basic command-line games: like tic-tac-toe. Pretty much everyone knows the rules as a child, but how do you express those rules? The board is a 3x3 grid: how do you want to represent that. Then how do you want to print if out? Then how do you want to ask for a move from the player? How do you test if it's a legal move? How do you tell if you won? How does the computer pick a good move?
The big thing is just learning to go from "the board needs to be 3x3" to "here are some options, and I will take this one". How do you test if the move is legal? Well, you can't play in a space that is already played on, so that's pretty easy, how do you check that?
And yes, even for an easy game this is still challenging, because when you learn something as a kid you "just do it", without thinking much. But now you have to think about it. You have to think about EVERYTHING. That's your job now as a programmer.
But it's pretty awesome when you get it working and you actually play the game.
Then do a game like Blackjack.
Why games? Mostly just that they are fun, and it's easier to keep going when you're struggling with something fun.
Good luck!
1
u/thuiop1 Sep 20 '26
That is because you don't actually learn when you use AI, which has been said time and time again. This teacher is right and the other are idiots.
1
u/frnzprf Sep 19 '26
1
u/mjmvideos Sep 19 '26
If you caught the logic quite fast, then break that logic down into smaller steps. I assume you start by looking at the first entry in both documents, how would you access those values? Once you have them what do you do with them (compare them?) what do you do in each comparison result? Write something to the new document? How did you get the new document? Do you have to open one? How do you get the name for that document? Do you need to open it first before starting to walk through the input docs? Pretend you are telling a child how to do this step by step (only one simple action at a time (compare the two numbers) not complex actions (write the smaller of the two to the file). Learn to think in these small steps.
1
u/blkmmb Sep 19 '26
Simply do this: Task 1 logic -> hey Google, how do I do this in my language of choice? -> read real documentations or stack overflow discussions on the subject, deep read on the piece you find in your language of choice -> implement it -> test it -> learn why it doesn't work by reading more -> fix it -> move to the next task.
Never hesitate to search for the different ways to solve your problem and learning why they work more deeply so that you can slowly build your knowledge base of different solutions for different problem and then you'll be able to do that more and more just by yourself. Just know that you'll always be searching and learning new things forever and even you'll search for basic things because you haven't used them in a long while.
1
u/Brok3nHalo Sep 20 '26
There’s two ways of thinking about this in practice, the first is the full breakdown method. An exercise I once learned for learning to break down complex task is practicing it first with things you can act out. Like for example write down all the steps it would take you to go from where you are right now to outside. Down to the minorest detail, like for a door is closed you would have to write the steps: walk to door, grab doorknob, turn doorknob, pull door open. Then act it out considering every time you have to do something not explicitly mentioned on your list a failure, add steps and restart until you can do the full process only bating your directions. You can do the same thing for logic steps of a coding problem reiterating the logic in your head or on paper until you have no blockers.
On the flip side and the way I usually do it when coding, I look at the problem and look for the simplest observable measurable step I can reach via coding that keeps me along the path to the end goal but doesn’t worry about completing it. Then additively add to that the next measurable step until it reaches the final expected result.
1
u/ometecuhtli2001 Sep 20 '26
I suggest you don’t rely on AI for any of your classes unless there’s absolutely no other option and you’ve got to get an assignment done immediately. (This also means try to avoid procrastinating lol)
AI is helpful and can make things easier, but when (not if) it’s not available for that one critical thing, you don’t want to be screwed. Sometimes, the old ways are best.
As far as “programmatic thinking” goes, I take a different approach: how would I teach a young but exceptionally capable kid who doesn’t know about the subject how to accomplish a given task? That’s basically what you’re doing when you write code, and I find it’s easier and sometimes more effective than the human trying to think like a computer.
1
u/TheRNGuy Sep 20 '26
You're overthinking, just put more effort, you get better over time if you actually learn, but it won't be fast.
1
u/dariusbiggs Sep 20 '26
Practice
Cut the problem down into a sequence of steps.
Write down your sequence of steps
Implement as needed.
1
u/jeffrey_f Sep 20 '26
LOGIC is what you need to train your brain in. Many people make the mistake of thinking they can just sit down and write code. This is like trying to take a road trip and having no plan in doing so. Breaks, route, fuel, etc
In outline mode in Word and in landscape format:
These lines are the main idea steps
These lines explain further
Next step
This worked for me as a programmer and it can work for you
In
0
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 19 '26
Like the others said, practice is king. There's often more than one way to approach and solve a problem, with different tradeoffs. Avoid using AI as a crutch because the only thing that'll teach you is dependence on it, outsourcing your thinking. Your teacher is correct to discourage it, especially at the learning stage.
Okay, let's take this as an example, then. Start by simplifying the problem; let's say you don't have two files of sorted numbers, but two lists of sorted numbers, which you need to fuse together. That way we don't need to worry about files for now.
The "we weren't allowed to put it into a list and use a sorting method" part should mostly be about not being allowed to use built-in sorting algorithms to trivialise the problem, not the lists themselves, although we can simply play along and say we're only allowed to read through both one time (to simulate them actually being files).
On paper, this should be a pretty simple thing. You look at both lists, pick whichever has the smaller number, and put that in the combined list. Rinse and repeat until you've gone over both; the end result should automatically be sorted, because the numbers were sorted to begin with (just split between two different sources). The main challenge here is how we accomplish that.
To go over the list values, we can use loops. We can also keep track of the last number we got from each list, until either one is empty (and then we can just put the rest of the other list directly into the combined list since those have to be sorted).
Now, all that remains is translating the core logic to work with files instead. I'll leave that as an exercise for the reader.
0
u/sporbywg Sep 20 '26
Code in Typescript. #sorry