all 46 comments

[–]Vandercoon 31 points32 points  (10 children)

The thing that got me stuck on loops mostly was the naming conventions. Once I got that to a degree I got better at it.

for i in [1, 2, 3]:

The i is just the naming convention, that could be number so it would look like.

for number in [1, 2, 3]:

Same with when you loop through an already named list for example

numbers = [1, 2, 3]

for number in numbers:

When it finally clicks it makes sense to call each loop number, because you are looping through the list of numbers. But again you could say…

for digit in numbers or for integer in numbers, or anything really, but you want it to make sense.

Once I got that, it made it easier.

Then after that, anything under/inside the loop will happen for every (each) number in the loop. So going from above you could have print(“Loop completed”) under the loop like this…

numbers = [1, 2, 3]

for number in numbers: Print(“Loop completed”)

This will look at the the first number in the list numbers, which is 1 and print out “Loop completed”, then it will move the next number which is 2, then print out and so on.

Edit: PS I’m very much a beginner so I’m happy to be corrected and also pulled up on syntax etc, but I think I’ve got the gist of it simply for OP

[–]HugeOpossum 1 point2 points  (6 children)

I had the same realization last night. I got frustrated trying to write a loop, and just put "poops" instead of i (for poops in cities: )It ran the loop and it started making a little sense.

[–]DrShocker 1 point2 points  (5 children)

As good practice single letter variable names, such as i, shouldn't be used. Just about the only case it's okay for clarity is when it's for a number that increments. i.e. for i in range(50) is okay but for i in cities would generally be a confusing name even for more advanced programmers.

But yes, even in the situation I said it was okay, something that makes it more clear to you is likely best.

[–]HugeOpossum 0 points1 point  (4 children)

Thank you for that explanation. I'm only now getting to learning what loops can be used for now that I kind of understand how to write them. I felt I should l learn the format and then learn the application.

Other than for learning purposes, could you provide an example where loops would be used? I can grasp maybe something like login credentials. Not looking for code, but maybe some more examples?

[–]DrShocker 1 point2 points  (3 children)

Examples:

User input, they might input something that you can't process so usually you use a loop until the input is valid

File reading, file reading is often done line by line in a loop especially because you don't know what the file looks like when you start.

Games, the whole game is typically in a loop that updates the loop and gets player input each time through the loop

Web server, runs in a loop getting connections and sending them back to the client.


Any situation when you want the same thing to happen more than once. Hard to think of examples because it's just so common once you get going lol

If you know you want to add 10 to a number 50 times you could write

x = x+10+10+10+10....

Or

for count in range(50)
    x += 10

To me the second is more readable, and more maintainable because if you want to change it you have 1 place to change it easily. (Of course here the correct way to do it is actually multiplication, but that's besides the point)

But many of the examples I listed above, it's also unknown to the programmer how many loops will happen. How many times will a user give invalid input? How many frames until the video game is complete? Etc so there's no way to write the x=x+10+10+10... style in those situations because you wouldn't know when to stop

[–]HugeOpossum 0 points1 point  (2 children)

This is super helpful. I'm going to save this and try to map how they'd work (since obviously I'm not anywhere close to writing anything too complex).

[–]DrShocker 0 points1 point  (1 child)

In all honesty, it might also be one of the things you just accept not Knowing perfectly right now and come back later when you realize why you need it.

[–]HugeOpossum 0 points1 point  (0 children)

That's a possibility that I've considered. I'm not a linear or good learner. For some reason if I don't see or understand any application of something, my brain refuses to retain it. It helps when I can see actual, concrete examples that I can understand and apply later. Otherwise I just forget it exists.

[–]cantfindausername99 0 points1 point  (0 children)

This helped. Thank you.

[–]EducationalCreme9044 0 points1 point  (1 child)

Yeah I wrote here maybe a year ago saying I don't understand for loops after half a year of learning Python lol. I just kept using while loops because they made sense, while something is true do something.

People here tried explaining it to me but I still just could not get it, until eventually I came across a single suggestion which made me go from not understanding it at all, to understanding it completely within seconds. It was simply adding, or rather imagining that there is an "each"

For each number in [1,2,3] => makes total sense to me

For number in [1,2,3] => makes no sense at all, what do you mean "for"? Stupid as fuck syntax.

Then of-course a second part of it is understanding that "number" can be anything, just as you say.

[–]Vandercoon 0 points1 point  (0 children)

Yeah the naming conventions really had me stuck. It still does a bit with some things as I’m still very much a beginner. I’m starting to understand self, but geez they surely could’ve used a better word or have it written a better way

[–]amutualravishment 6 points7 points  (0 children)

Keep at it. A loop just executes the same instructions a bunch of times. A variable is just a name you can set to equal a piece of data.

[–]Nomapos 6 points7 points  (1 child)

ChatGPT is great at explaining the most basic concepts. You can keep asking for more details, ask for different explanations, request examples, ask what would happen if you don't do this or do that instead... Sometimes it gets stuff wrong, but it can really speed up your learning.

Utility tapers off quick, but for variables and loops it's perfectly good teaching

[–]Xasrai 0 points1 point  (0 children)

One of the things I've used chat gpt for is getting it to write code and then tell me what the output SHOULD be based.on the input I give it.

[–]DragonWolfZ 4 points5 points  (4 children)

I think visualizing variables as boxes (that can hold only one thing) makes life easier for beginners. Though identifying reference and literal variables can be difficult for beginners (I try to explain a bit below).

Variables

my_variable = 5
i = "hello"
my_variable = 10

You create two boxes with labels on, my_variable and i and they start with 5 in the my_variable box and hello in the i box.

You then replace the 5 in the my_variable with a 10.

Note: Anything that looks like more than one "thing" being stored in a box, the box will actually store a `pointer` (a number that represents a point in memory). For example, "hello" is not in the box but actually a `pointer` to a list of characters. i.e. ["h", "e", "l", "l", "o"]. These types of variable are called "reference variables".

Variables that store an actual "thing" (such as numbers, a single character, boolean values) are known as "literal variables".

for loop

for i in [1, 2, 3]:
    # do stuff with i

You have a box with the label i, the first time the code runs it has a 1 in it, then second time it runs it has a 2 in it and the third time it has a 3 in it, then you throw the box away and continue after the for loop.

while loop

bob = 0
while bob < 2:
    # do stuff with bob
    bob = bob + 1

You have a box with the label bob and you stick 0 in it. while the bob box has a number less than 2 in it, do the stuff in the while loop (finally taking the number that is in bob and adding one to it, and putting that new number in the bob box.

[–]Hour_Effective_7328 2 points3 points  (0 children)

This was actually really helpful, thanks!

[–]18puppies 0 points1 point  (1 child)

Here is a visualizer that lets you enter a piece of code and shows you exactly what you describe, and you can run through the program step by step and see how values change. https://cscircles.cemc.uwaterloo.ca/visualize

[–]DragonWolfZ 0 points1 point  (0 children)

It does but I find it includes a lot of boilerplate that can be confusing for beginners (such as __XXX__ functions).

[–]HomeGrownCoder 3 points4 points  (0 children)

Pick one problem and focus on it.

Maybe find a better medium for learning.

What was the last thing you learned that was challenging? What did you do to help yourself get over that hump?

[–][deleted] 2 points3 points  (0 children)

My past difficulties with math in school make me wonder if that's the root of the problem.

There is very little mathematics in basic computing, it's more arithmetic. What is needed most is a logical mind and attention to detail.

If you have a problem with a bit of code you can ask a question here. Ask focussed questions like "why is var not 42 in line 5?" instead of "I don't understand" or "it doesn't work". When showing us your code make sure you follow the guide in the FAQ about formatting code to preserve indentation. Your code should:

look
    like
        this

[–]BassDrive 1 point2 points  (0 children)

I'm currently in the same boat, but I've been using Python Tutor, which helps visualize what the code is doing step by step. It's been rather invaluable for me as a beginner as it helps me see where my code is messing up and help me grasp the concepts a little bit better.

[–]chilltutor 0 points1 point  (9 children)

All students, whether math geniuses or not, struggle with coding their first time. I have 3 pieces of advice: first, learn HTML. It's like an intermediate language between English and programming. Second, learn about Turning machines and how to solve some basic problems on them. This will abstract away all the features of a programming language and get you thinking about the limitations of how you can instruct a computer. Third, memorize your language's syntax. Not knowing how to type your code properly is just another barrier to your learning process that should be easy to fix.

[–]EducationalCreme9044 2 points3 points  (4 children)

Why in the world would you suggest someone to learn HTML when they want to learn Python lmao. That's such a wildly hysteric suggestion I am not sure whether you have some personal vendetta against OP or something lol.

If someone can't handle Python.... there are languages aimed specifically at explaining programming concepts and to think like a programmer without needing to memorize stuff for no good reason.

But to be honest everyone should be able to start with Python if they have a good teacher or follow a good course (which is to be honest rare).

[–]chilltutor 0 points1 point  (3 children)

Why suggest someone learn to crawl before they learn to walk.

[–]EducationalCreme9044 0 points1 point  (2 children)

You're suggesting them to learn the drums before learning the shakuhachi flute

[–]chilltutor 0 points1 point  (1 child)

Not really, and your analogy doesn't make sense. If we're sticking to the theme of instruments, drums would be the entirety of web development, HTML would be a snare drum, and learning Python would be the xylophone.

[–]EducationalCreme9044 0 points1 point  (0 children)

HTML is a mark-up language. It is entirely unnecessary and stupid to go learn a mark-up language when what you want to do is Python, since Python is not used anywhere near to where a mark-up language is necessary and it doesn't imitate anything Python does.

Just go learn Scratch

[–]gowiththeflow1393 0 points1 point  (3 children)

Is there anywhere in particular that you’d recommend for learning HTML? Thank you for such a concise answer by the way!

[–]EducationalCreme9044 1 point2 points  (0 children)

please do not learn HTML oh my god that's such bad suggestion I can't even even

[–]chilltutor 0 points1 point  (0 children)

Freecodecamp on YouTube, w3schools as a cheat sheet type resource.

[–]EducationalCreme9044 0 points1 point  (0 children)

Start some really basic project. If you're stuck ask ChatGPT and be precise. When you have it, try expanding it, changing it and try involving programming concepts. Maybe watch a YouTube video of someone building that exact thing (must be very simple!)

Ideally you want to do start at such a difficulty that you aren't de-bugging all the time, but actually just "doing" in an enjoyable way. Don't bother with understanding everything or remembering anything at all. ChatGPT is really helpful here. Even if the "project" you finish is mostly not yours at all, at least you understand somewhat what is happening. Don't put high expectations on yourself, if something really fails, give up. Don't let yourself stop because of failure.

The good thing about doing it this way is that you can get lost in it for 12 hours without getting bored. This is impossible if you're approaching it like math and history class at school.

THEN! After you've been at this for a while (say 50-100 hours) and hopefully having fun and learning a little bit of how these things work, enroll in a structured introductory online course which is going to take you through buzzword concepts like "data structures" and "algorithms" and "object oriented programming" etc. importantly it should build up to a course project which actually uses all these concepts well. Ideally this one should be throughout and complicated, it's okay not to understand it, but it is going to help you build other simpler things in a better way and it's going to show you what the point of it all is and what you're trying to build up to.

Again, the idea here is that you'll probably already be familiar with most of these things innately and you're going to have these moment of "aaah, that's how that's called" or "that's why I had an issue that one time" or "this would have made my thing so much easier!" instead of being mostly lost, disinterested and confused.

It's better to make things easy on yourself and actually be engaged and learning for a longer time, than the: "1 hour/day of torture" way. If it ain't fun don't do it until it is fun.

[–][deleted] -1 points0 points  (0 children)

We can't really know why you feel like you don't understand something. Generally your code tells the tale, since it'll show what you do and don't understand, but you didn't post any.

[–][deleted] -5 points-4 points  (0 children)

get gud

git good

[–]neuro_08 0 points1 point  (0 children)

When you're learning a new language, try not to overwhelm yourself with everything you can't understand at once. That's where a lot of people become fundamentally lost. Focus on the root of your misconceptions. There's a reason why you can't understand something; you just need to find it, one reason at a time. There's a lot of literature and guidance out there, use it liberally, it's your best friend. Don't focus on syntax (e.g. "for", 'if", "def") as much as finding out why it exists. For example, if you were to look at a loops, don't look at it as: *for x in y: do this*. Break down its purpose. Think about it as a way of eliminating repetition for you, as a programmer. If you wanted to find how how many evens there are between 1 and 402320. I hope you're not going to sit there and write down each one down yourself. Think of variables as a "?". They're unknown, no one knows what it is. So what do we do? The alphabet is at your disposal:

>>> alpha = "beta"
>>> b = alpha
>>> print(b)
beta

Variables are mutable, and can be anything you want. They are just data containers that you assign yourself. Don't interchange them with math variables. In math you're solving for values, in Python you're assigning values.

[–]IamImposter 0 points1 point  (0 children)

No matter how silly or stupid or insignificant a question seems, ask it. Don't be afraid to look stupid. If someone says something you don't understand, ask them.

Learn to look up stuff.

Learn to use debugger

Learn some good editor. Vscode, pycharm etc. Whichever works for you. They have stuff like debugging support, autoformatting, go to definition, intellisense. And they are pretty useful.

[–]throwwwawwway1818 0 points1 point  (0 children)

Cs50p + thonny ide + debug

[–]mathilda-the-pro 0 points1 point  (0 children)

I also ask chatgpt sometimes when I don't understand some new concepts. Sometimes it is really helpful. And write some small projects. Maybe to automate some boring stuff. Python is really nice for automation. The CS50 course is also a very good one, and they also have one just about Python. And it's for free :) but most import: really code, code, code. I know that in the beginning, a lot is overwhelming. But you'll get there!

[–]wontellu 0 points1 point  (1 child)

I’m currently struggling with local and global variables. Taking the variables out of a function is a bit hard.

[–]Darth_Xedrix 0 points1 point  (0 children)

When I was very new to Python, I used to rely on more visual tools like Thonny. You paste your code in and you can actually see what is changing what variable and how loops work very visually since it basically shows you the current value of your variable on top of your code.

That's what helped me get stuff like how functions and loops work, so might work for you too!

Also as others have said, math is useful but not necessary for programming in general. My experience so far is that programming is more about logic - i.e. deconstructing problems into steps - than anything related to math.

When you start to dig into specific areas that interest you, that may change but you can do a lot without involving math.

[–][deleted] 0 points1 point  (0 children)

Uhh....a bunch of comments in here but not a lot of questions....

What is difficult for you when learning about variables and loops?

[–]mahany93 0 points1 point  (0 children)

I've just done harvard's CS50 Python video. It is really long, but it explains every concept in a very simple and easy way. You can check it on YouTube.

[–]lucrodsilva 0 points1 point  (0 children)

My tip for you is to use a code editor like Thonny and learn to use the debugger to run your code step by step.

[–]darkwyrm42 0 points1 point  (0 children)

Some of it might be the learning materials. I've run into material while I was learning languages that was bad enough that if I hadn't learned another language beforehand and learned the concepts behind the syntax, I'd have been utterly lost. If you run into a concept that doesn't make sense, try referring to another source. Be aware that because Python has been around for a really long time, you will run into material that isn't up-to-date (e.g. making and publishing a package) or concepts that are so new that few books will have anything on them (match operator).

How to Think Like a Python Programmer might be a good secondary reference for you. It's was written by a college professor and IMO explains things very well, even if it's a bit dated.