r/learnpython • • Jan 05 '24

I can't seem to code properly.

Hey everyone.

I started learning Python from yesterday itself, and it's very intriguing. I've pulled all nighters to watch MIT lectures and Corey Schnafer. As of now, I can safely say I understand how to work with strings, numbers, tuples, and lists. That to a slightly basic extent. Today, I decided to go a bit adventurous and tried learning to code a Tic-Tac-Toe. Didn't work. Looked up code online - I barely understand any of it.

I know you may think, "This guy is a laugh! He's running before he could walk!" Thing is, things seemingly come natural to me, especially Mathematics. And, programming languages such as these are very mathematically-oriented. So, I felt a bit disheartened.

I scrolled on this subreddit and found that many people watch lectures and read books, but when it comes to the coding aspect of it, they frazzle. They don't know what to do. I want to prevent that. But how? I don't want to code print("Hello World!") forever. Any advice? Any tips? If you could share your personal anecdotes, please do. Apparently, there is some imitation culture where you can copy other people's codes and tweak it a bit. Is that ethical ?

Also, once I finish learning the basics and experimenting and coding up basic projects, I plan on learning pandas and mathplotlib (I am interested in Data Science) and some Django.

2 Upvotes

12 comments sorted by

42

u/hornetjockey Jan 05 '24

Congrats. You are a gifted person that has come up against an actual challenge. I think if you try to skip ahead just assuming you’ll get it, you’re going to have a bad time. Crawl, walk, run. A good first game is “pick a number between 1 and 10”.

8

u/[deleted] Jan 06 '24

I'm one of the so-called "gifted" people. I wish I wasn't. Yeah, it's great when you can do no homework or studying and ace your tests because of the fact that they're multiple choice. Throw anything on a multiple choice test; if you've got logic and some common sense you'll probably ace it easily.

The problem is that life isn't a multiple choice test. The options aren't given to you in a list where you choose one. Life is one giant long answer essay where you don't even know what the question you're answering is.

Same with coding. There are tons of different ways to implement whatever project you're working on. When you're new, you're also at the first level of competence: unconscious incompetence. Essentially, you're bad at it, and you don't know that you're bad at it or why. You don't know what you don't know.

The only way to get better is obvious. Code more. Ask LLMs questions about how to do different things, and have them expand on every term or function they use that you don't understand. Test their code (they write buggy code often). Write your own code that does the same thing in a different way. Start small.

Tackling a whole game is a large task. Break it down into steps. For example, write a function that takes a one dimensional array of 3 elements in length, and returns true if all the elements are the same, or false if they are all not the same. Remember that all the elements being empty does not constitute a win for anyone, so you have to handle that.

Then later as you're building your tic-tac-toe game, you can use this function over and over as you're checking the board for a win state. Slice the 2D array representing the board into 1D arrays representing the rows/columns/diagonals (of length 3), and feed each one into that function you just wrote. Depending on whoever just moved, you'll know who wins when this function comes back with "true".

^ That's just one possible way to implement a check for a win state.

Happy coding.

22

u/pythonwiz Jan 05 '24

Maybe you should try to sleep, all nighters are not conducive to learning.

16

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Jan 05 '24

I started learning Python from yesterday itself

The short answer is, no matter how gifted you are with mathematics, programming (and especially the wider scope of software development) just isn't something you can pick up overnight. That'd be unheard of.

Thing is, things seemingly come natural to me, especially Mathematics. And, programming languages such as these are very mathematically-oriented. So, I felt a bit disheartened.

The TL;DR of it is, while in theory you are correct (particularly with something like functional programming), in practice it's not so simple. The roots are in mathematics, but programming languages have evolved in unrelated ways to better match how humans think about problems.

...Okay, I realise that's probably not making much sense, but I've never had to explain things from this point of view before. I'm not entirely sure how best to respond here.

I scrolled on this subreddit and found that many people watch lectures and read books, but when it comes to the coding aspect of it, they frazzle. They don't know what to do. I want to prevent that. But how?

Getting stuck on problems is probably unavoidable to all but the handful of savants who may or may not even exist. It's one thing to read theory, and another thing to apply that acquired knowledge in a practical way. Hence, baby steps - start from the simplest program you can think of, then gradually start adding new things.

Experiment, run headfirst into problems, learn from those mistakes, and have another go at it. Fail fast, as they say.

Apparently, there is some imitation culture where you can copy other people's codes and tweak it a bit. Is that ethical ?

Depends on how that code was licensed, but in general, totally. Nearly all of my non-work code is under an MIT license, meaning anyone can do whatever they want with it. We're all ultimately standing on the shoulders of giants; If I was forced to start by building my own operating system in x86 assembly without any library code and only Intel and AMD's documentation, I'd have zero clue how to proceed.

Also, once I finish learning the basics and experimenting and coding up basic projects, I plan on learning pandas and mathplotlib (I am interested in Data Science) and some Django.

That's a fine interim goal, just know that you won't get to that point by tomorrow, let alone next week. Programming is a skill that'll easily take months, usually years, before things are truly starting to "click", regardless of your background.

1

u/[deleted] Jan 06 '24

I feel a lot better now

6

u/RedditSlayer2020 Jan 05 '24

What had always driven me was a purpose, an idea, a concept that I wanted to accomplish, a very specific small project.

Later I found out that proper project management skills are crucial for successful coding.

Concepts like time boxing helped me alot to become structured and highly efficient.

Coding itself is just syntactical implementation of what is laid out already in front of you.

Trying to properly understand a problem is your very first step.

Learn to solve problems first.

2

u/Kriss3d Jan 05 '24

It takes time. Don't worry.

Relax and learn how to do each thing one at a time.

Learn to print out a variable. Both strings and how to convert an int to the screen.

Learn to have it take an input for say a name and age and have it print out a string that contains both your name and age.

Learn to make a small calculator with the addition subtraction division and multiplication and have it return a result.

Learn to go through loops and how they work.

That kind of thing. You'll get the hang of it. Don't worry. We all were at the point where it made no sense. And then suddenly it'll click and you'll feel like you can fly and that the sky is the limit.

2

u/Wasabi9495 Jan 05 '24

I initially started with the Python Crashcourse, which really helped me get into the language. But what really helped me become more and more fluent in Python was setting a goal and trying to achieve it. I used to work for company where I often had to manage multiple IoT devices over a clunky to use API with tedious and lengthy code. I sat down a few days and wrote myself an application, complete with GUI that would allow me to save already accessed devices in a list and send API requests to them with the push of a button. That saved me a lot of work, got me some recognition from my boss and other coworkers and soon we were all using it. (We were just a tiny company of 9 people). I also wrote barcode generators for almost all kinds of barcodes, as well as datamatrix, aztec and so on.

You grow with challenges. Is there something you want to code? Specifically for yourself? Something that would be fun to use or would ease your everyday life?

2

u/[deleted] Jan 05 '24

I always told people that watching lectures and writing code is different. There's too many little things that won't be explicitly said in a lecture. It's like trying to learn a new sport by reading a book, you can't possibly expect to be able to do well just by reading.

Watch a chapter of lectures, then write code. If you have any problems, google. Move on to the next chapter when you're ready. If you watched hours of lectures in ome sitting, you've probably forgotten the content from the earlier chapters or had gaps in your knowledge you didn't even know you have.

You're not at the stage to need to worry about copyrights yet. Basic code sometimes only has a single way of writing it, you can claim to be plagiarising basic template code.

1

u/feldomatic Jan 06 '24

Speaking of running before walking...

If you're interested in DS, just jump to that, doing things in DS will give you a better reason to learn a lot of other things.

I struggled for a while in a similar situation before diving into DS and finding my niche.

1

u/throwaway8u3sH0 Jan 06 '24

Hi, also gifted here. Two STEM degrees and a math minor. I got rich building cutting-edge satellites.

I've been coding since 1988 and I still learn something new every week.

Relax. Get some sleep. Vas-y mollo. It's a marathon, not a sprint. You'll be fine, I'm sure of it. Just don't burn out.

1

u/[deleted] Jan 06 '24

Slow down. You won't build a sufficient understanding if you don't slow down. Either this is a bait post or you're serious, and I sincerely hope you're not serious.

If you are, you are not following a structured learning path and you need to acknowledge Dunning-Kruger at play here. You don't know what you don't know and think you see the full scope, but the scope is much larger than you are realizing.

If this is a bait post, congrats, you got your replies. Now go study.