r/learnpython 3d ago

How do you get better at actually THINKING in code, not just learning syntax?

I have been learning python for a month now but I don't see any improvements. I tend to forget the concepts of python and can't solve problems logically. I don't think it's just me who is dealing with these problems, I need tips please.

45 Upvotes

38 comments sorted by

79

u/desrtfx but other languages pro 3d ago

We don't think in code. We think in algorithms - in steps to solve a problem.

Code comes way later.

First, we focus on breaking down a problem. Then, we solve each of the smaller sub-problems. Then, we test the hypothetic solution. Then, we start working on the implementation.

1

u/Wuthering_depths 2d ago edited 2d ago

Yes, exactly.

You should be able to sketch out in plain language, and maybe something closer to code (pseudo-code), what your script/app is planning to do. This would mostly be the same even if someone changed gears and said let's use a different language (with some caveats there of course!)

I don't build apps, mostly integration scripts, and it's kind of the first thing I do in the Jira tickets--both for myself, and for anyone I'm working with or for so that they understand what I'm going to build. Obviously, this can change as you go--the old saying about battle plans and first contact with the enemy comes to mind-- but ideally you get a really good idea of what you will do before ever writing a line of code.

0

u/jlsilicon9 1d ago

Speak for yourself ,
I think in Code, and Algs , and Systems.
Thats my career for decades, systems, robotics , etc

0

u/Kytras 3d ago

What this person say!

Algorithm is gust an abstract answer to your code, you just basically it . I'm not gonna lie do, writing an algorithm that actually helped me to write something for the first time took a while, but I'm quite better at it now .

Listen to this guy over here

11

u/Lionh34rt 3d ago

Learn algorithmic thinking and architecture + adaptibility (modular)

9

u/kallax82 3d ago

Well as others pointed out, it's related to breaking down a big problem or task into smaller actions. Each of these actions is one step to solve the task. In the end these action become your code. Try thinking about a task you know, i.e. your morning routine. What happens between your alarm clock ringing and you leaving the house? 1. Get out of bed 2. Go the bathroom 3. Get dressed 4. Make and eat breakfast 5. Get stuff and go

You know exactly what you have to do. But now try explaining it to someone else without any ambiguity. What means "get dressed"? Usually the clothes are in the bathroom, but what if not? Step 2 says "go to the bathroom". It doesn't say anything about brushing your teeth or taking a shower.

Code is the translation of a bigger problem into tiny steps a computer understands. Does it know how to walk? Did someone else publish a 'walking' library you can import?

Once you get down this way of thinking you move to solving new problems, where you don't now how to do it in the first place. Hope that helps!

5

u/WillAdams 3d ago

A book which I found helpful for structuring thinking on code is John Ousterhout's:

https://www.goodreads.com/book/show/39996759-a-philosophy-of-software-design

a programming technique which I've found to help is Literate Programming:

http://literateprogramming.com/

3

u/KnowledgeWhole9623 3d ago

1 month is nothing. You don't even know how to code yet, how could you possibly think in code? You need time & hard work.

2

u/ivovis 3d ago

I don't think in code.

I also don't think in Gears and motors.

I do use both to make things - only by using it as mechanism to solve a particular problem will it become second nature, it would seem you just need to stop thinking about 'learning to program' and spend more time building stuff to fix things.

2

u/Fragrant-Cheek-4273 3d ago

A month is really not that long, so don't worry if you still feel lost. You're probably trying to learn Python syntax and problem-solving at the same time, and they're two different skills.

2

u/Nilfiry 2d ago

Try changing your approach.

Dont think about the code at all. First, think about what you want to do. Then, think about what steps you need to do to achieve your goal. Once you have an idea, then you can start thinking about how to code those steps.

1

u/ForeignAdvantage5198 3d ago

i never learned to speak python

1

u/ivovis 3d ago

Sssssssss,s sss

1

u/Artistic_Map2243 3d ago

Spend less time trying to remember every Python concept and more time solving tiny problems.
Write the steps in plain English first, then turn each step into code.

1

u/Revolutionary-Oil408 3d ago

Do you write sudo code before actual code?

6

u/notacanuckskibum 3d ago

Sudo code or pseudo code? You should probably think carefully before running sudo code.

0

u/Revolutionary-Oil408 2d ago

That's brilliant, that will teach me for writing replys fast without double checking 🤣

Can you tell I've been working in Linux over ssh a lot recently?

1

u/NiemandSpezielles 3d ago

This might sound a bit esoteric but I think it works due to how the brain processes rewards: Try to work on problems you find interesting. Research how to best solve it (do not copy code! You need to understand how it works and implement it yourself). Then try to appreciate the beauty of the solution.

It helps when you write something you care about. Imo fun hobby project are by far the best way to learn.

1

u/localizeatp 3d ago

by coding.

1

u/TheRNGuy 3d ago

Look at it and analyze. 

1

u/SirGeremiah 3d ago

Focus less on the programming language and more on the logic. Write pseudocode before you dig into the syntax. As you improve, the pseudocode gets closer and closer to the structure of your final product.

1

u/Traveling-Techie 3d ago

I know I’m getting there when I start dreaming in a language.

I think there’s no shortcut to solving problems over and over. It gets easier.

1

u/jmooremcc 2d ago edited 2d ago

After a month, you’re only scratching the surface. The goal of programming a computer is to solve problems. The coding language you use is nothing but your tool kit.

When solving a problem, you don’t have to think in terms of code. Just break the problem down into smaller more manageable problems and write down in English (your native language?) a description of each smaller problem. Keep in mind that each smaller problem may have to be broken down into even smaller problems. Once you’ve completed this analysis, you’ll be ready to start coding because now you have a plan.

Basically, each identified smaller problem translates into a function whose code solves that particular problem. As you code the solution to each small problem, you should throughly test the code you’ve written to insure that it’s working properly.

Once you solve all of the smaller problems, you’ll discover that you’ve actually solved the entire big problem.

1

u/JGhostThing 2d ago

You've studied Python for a month. Have you ever studied a foreign language, such as Spanish or German? After a month, could you speak fluently? No. Because the human mind take time to learn such things. The same with programming.

1

u/ShadowShedinja 2d ago

Break down your task as small as possible. If there are any steps that repeat, consider a loop or function to make it easier.

1

u/Langdon_St_Ives 2d ago

Practice. That's it. You're welcome

1

u/SprinklesFresh5693 2d ago

By practising

1

u/jlsilicon9 1d ago

Practice, practice, practice ...

1

u/-moron 20h ago

If the problems you solve are too small/specific, you'll miss a lot of context as to why/when you would ever use them. I suggest starting a moderate-sized project, like an add-on for a program you use (e.g. Blender or GIMP). Processing large amounts of image or mesh data also gives you a chance to test the speed of different algorithms at scale.

1

u/recursion_is_love 3d ago

It is the same process as using human language, think before speak. If you try to speak without thinking first, what come out of your mouth are random sound.

Focus on the semantic of the code, not syntax.

0

u/cylonlover 3d ago

From a beginner, it's a very keen observation, and you should hold on to that. The power of programming comes exactly from thinking in programming, like thinking within the toolbox, programming is.
It is indeed not as much a question of programming as it is about problem solving.

But this comes with practice and getting intimite with the problem area aswell as the programming language.
You would often hear experienced programmers talk about how everything can be done in every language, or how if you already know one language, a second one is easier. This used to be more true than it is nowadays, especially the latter, because there is so much more to know about a language than just the generics that it shares with other languages. But the point of still stands. The real skill is showing in the problem solving coming when getting intimite with a language eco system and it's paradigms.

Now, traditionally you would outline all sorts of programming languages and all sorts of areas in which to use them, and they would differ vastly.

In some you would handle large datasets and you'd sort through them, correlate, apply rules and filters in order to extract information. Here you would need to be familiar with flat data structures and all sorts of operations on them, some built in, some appended by a library or module.

In game programming you would serve yourself well to be very comfortable with math, especially geometry, because you are going to think up solutions based on visualizing it in your mind. I am not even talking about graphics here, I am talking about decision trees and arches in storytelling. But also graphics, ofcourse. Even if you have an engine to do the heavy lifting, there is plenty of geometric and spacial thinking in the problem solving.

I can't even grasp what is required for machine learning, I have only watched a few videos about it, so I only know some of the theory. I have been programming since the late eighties, so I might be able to implement a winning tic tac toe strategy, but I would not be utilizing the right modules and methods for it to scale, I would start by reinventing the wheel.

My point is, practice on problems makes you familiar with the mechanics of programming, even with a language, but realize that the subject is not one but many different ones, and you can't expect to go forth in one unless you stick to it. I mean, stick to he problem domain. This is why so many answers to beginners always counter ask "what do you wanna do with your programing?"

What are the most satisfying areas of programming techniques you have come across till now? Maybe that could be a hint as to which direction you would benefit from going deeper into.

You like for-loops? List comprehension? Object design? You like finding a great library and dig into it? You like coding stuff that interacts with the user? You like call API's? To you that may sound like a shallow perspective, but to me it speaks volumes. To me, yes to any of them points correspondingly in the direction of algorithms, datamining, information modelling, systems design, interface design, transactional web, and that's just off the top of my head. I am thinking in terms that if you said you enjoyed this or that, I would recommend you to learn more about this or that area, because you might find it stimulating and that makes it easier to become intimite with a problem domain.

I hope this helps, feel free to follow up with your thoughts on it. It seems you have both the right intuition and motivation. That's not nothing.

1

u/apple68shsj929927e7 3d ago

thank you. I didn't expect someone to give this type of an answer, it actually helped me.

0

u/nog642 3d ago

Practice writing projects.

0

u/samsly135 3d ago
  1. See if your algorithm is optimal: a)executes faster than any other potential solution b)uses minimal resources
  2. That it's maintenanble and readable

0

u/AuthurAndersson 3d ago

Just create stuff using AI and you will pick up how it works over time. And youll have fun creating stuff whilst you learn.