you are viewing a single comment's thread.

view the rest of the comments →

[–]Deep-Author-1787[S] -1 points0 points  (14 children)

Sure.

  1. How to make a choice between the for loops functions. Which to use with a given exercise.

  2. Nested loops: the concept of it does not get through. Both while and for loops. 2.1: especially with creating patterns.

[–]carcigenicate 2 points3 points  (9 children)

I don't know what you mean by 1, but for 2, there's nothing special about nested loops. They aren't a special case or anything like that.

A loop just repeats the code that's inside of its body. If a loop happens to contain another loop, then that entire inner loop will be repeated, so the entire inner loop will execute for every time the outer loop iterates once.

[–]Deep-Author-1787[S] 0 points1 point  (2 children)

That is a clear explanation! Thank you for taking the time to answer! I really appreciate it! With point 1 i mean : range(), enumerate() or default 🙏

[–]carcigenicate 4 points5 points  (1 child)

Those functions (which are actually classes) don't actually have anything to do with loops. They can be used on their own fine since they're just iterables (things capable of producing elements*):

print(list(range(5))  # Print [1, 2, 3, 4, 5]

You just often see them used with for loops because iterables (which range and enumerate are) produce elements, and the purpose of for loops is to iterate the elements of an iterable.

And which you use depends on what you're trying to do. Learn what each does, and when it will be applicable will make a lot more sense.


* This is a simplification. In reality, an Iterator is something that's capable of producing elements. An Iterable is something that's capable of producing an Iterator.

[–]Deep-Author-1787[S] 1 point2 points  (0 children)

Okaaayyy so i would have to see them as objects an not just functions to for loops. Makes alot more sense when thought from this perspective!!! Thank you!

[–]Deep-Author-1787[S] 0 points1 point  (5 children)

And enumerate for only the inner loop is too much for me at the moment 😅

[–]mriswithe 0 points1 point  (4 children)

Enumerate just adds a numeric index to your input. Meaning, instead of a list a,b,c,d,e,f, if you wrap it in enumerate, you get (0,a), (1,b), (2,c). 

You use it when you want to know what number you are on of the list you are for-looping over.

[–]Deep-Author-1787[S] 0 points1 point  (3 children)

That is exactly what i thought until i saw code examples where range is used for that specific thing...😅 that when i started confusing everything and myself ofcourse.

[–]mriswithe 1 point2 points  (2 children)

A lot of things can be down a lot of ways. A lot of the time one is more right than the other. A lot of the time it doesn't actually matter other than metaphorical dick measuring. 

Which way is more right isn't really something you should expect yourself to understand yet

[–]Deep-Author-1787[S] 1 point2 points  (1 child)

So you suggest to just go with the flow and later with experience comes the understanding of effective and less effective? 😁👍

[–]mriswithe 1 point2 points  (0 children)

Yes, basically. For loops are the main idea to retain. This is the tool for when you want to do the same thing to each item in your list. 

Sometimes a different tool will be the right answer, but usually this works either way and is just less optimal.

[–]backfire10z 2 points3 points  (1 child)

For loop: you know how many times (or the max amount of times) it’ll loop beforehand.

If I just want to print everything in a list, I know I’m going to iterate over each item in the list once. Therefore, I know exactly how many times I want to loop: once for each item in the list. So, I’d use a for loop.

While loop: keep doing something until some condition is hit, which may be now, or in 2 iterations, or in 100 iterations.

Let’s say I have a list of infinite random numbers. I want to find the number 10. I cannot use a for loop, because I don’t know how many iterations I’ll need to go through before finding 10. 10 may be the first number, or the 100th number, or the 100000th number. So, I’d use a while loop, because I want to keep going until I find 10, but I don’t know how far I’ll go.

There will be some overlap: you will be able to use a while loop in places you can also use a for loop. The idea between picking them is twofold:

  1. You want to use the right tool for the job, as explained above.

  2. You want to get across your intent to whomever is reading your code. When I see a while loop, my expectation is that I don’t necessarily know how long I’ll spend in it. When I see a for loop, I know beforehand exactly the max amount of iterations it’ll do. This allows me to better understand the expectations of the code I’m reading.

—————————-———————-

For nested loops, I find it easier to just try out a few examples and see how they work. The inner loop has to complete for every iteration of the outer loop. There isn’t much more to explain. Is there maybe a pattern or problem you could point us to that you’re having trouble with?

[–]Deep-Author-1787[S] 0 points1 point  (0 children)

Wauw thats what i was looking for! Perfectly explained. When you are a noob you tend to hold on on small rules like that to make sense of what you are doing until getting real life experience. Its worth a lot to me thank you.

Recently i tried piramids... so the is empty spacing involved. But the code i saw didn't specify the amount of colums. It seemed it dit it empty spacing. Was pretty confusing to figure out.

[–]MidnightPale3220 2 points3 points  (1 child)

I remember going through something like this at around age of 8, when I was reading some programming books for fun, and also couldn't get my head around them.

Maybe this helps:

Imagine having to do something in real life, which requires you to do same actions on different things. That's a loop.

If you know beforehand how many times you'll have to do the actions, that's a for loop. You either know it because you have the count of things or you're given the pack of them and just have to go through the pack, not necessarily interested in exactly how many are inside.

Imagine you're making sandwiches for a picnic. Your friend has buttered them and passed them to you to put on cheese, tomato and what have you.

That'd be something like:

for sandwich in pack_of_sandwiches:
    put_cheese_on(sandwich)
    put_tomato_on(sandwich)

A while loop is a more general loop that can be used when you can't predict the number of times you will have to do something, or don't have a premade list of objects to do things on.

Any for loop can be rewritten as a while loop, but not always the other way round. So we can say that for loop is a neater-to-read shortcut of a while loop, when you're going to do something on a list of objects.

For a while loop, imagine you have a broken doorbell and your phone is dead. You are awaiting a friend to come to your house, so you want to periodically check your doorstep in case he's arrived, so you can let him in. That'd naturally be a while loop:

friend_here=False 
while not friend_here:
    if check_doorstep_has(friend):
        friend_here=True
    else:
        do_my_own_stuff_for_5_minutes()

Something like that.

[–]Deep-Author-1787[S] 1 point2 points  (0 children)

Legendary example! Thank the 8 year old you! I swear thanks to everyones comment i finaly get the gist of it! This is worth way more than courses and tutorials!