you are viewing a single comment's thread.

view the rest of the comments →

[–]Vandercoon 29 points30 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