+ 2

The "for" loop: how does it work?

So, I have not understood the usage of the "for" loop. You know, the "for _ in _:" loop? Yeah, that. Sure I have just finished the part of the course that explains how a computer holds data in a variable and what and how works a boolean, but still. It's important to learn that loop. So, how does the "for" loop work? Plz someone answer me :(

22nd Sep 2024, 11:48 PM
Enzo Henrique De Oliveira Freitas (Bricky)
Enzo Henrique De Oliveira Freitas (Bricky) - avatar
6 ответов
+ 9
Enzo Henrique De Oliveira Freitas (Bricky) remember index begins at 0 not 1 for i in range(3): Simply means that the for loop will run 3 times So let's say you have an array b = [0,1,2] for i in range(3): b[i] # is 0 or b[0] b[i] # is 1 or b[1] b[i] # is 2 or b[2] In a string s = "this" for i in range(4): s[i] # is t or s[0] s[i] # is h or s[1] s[i] # is i or s[2] s[i] # is s or s[3]
23rd Sep 2024, 12:32 AM
BroFar
BroFar - avatar
+ 6
For loops are actually a beautiful way to control loops. Looping is essential in programming. There are three types of loops. WHILE loops will loop until a condition is met. For example: while x < 10. If x is already above 10, nothing happens. But if x is less than 10, the loop will run until x reaches 10. It's a great loop. DO WHILE loops are similar, but they just do the work before checking the condition. Therefore the loop will run at least one time. These are less common than WHILE loops, but still quite useful sometimes. But then there is the FOR loop. FOR loops are best for when you want it to run across an entire list of properties. Let's say you have a basket and it's filled with fruits. For each fruit in the basket, you want to chop it. basket = ["apples", "grapes", "cherries"] for fruit in basket: wash(fruit) The loop will assign the "next" fruit on each iteration. First time through, fruit will be "apples". Second time through it will be "grapes". When it finishes the list, it exits. It's very simple to write it that way. You don't have to worry about how many there are. If you tried to do this with a WHILE loop, you might have to perform a couple other operations to handle the loop. So when you want to work through a list, FOR loops are the easiest. Of course there are other ways to use FOR loops, but this is the most common. One of the trickier parts of FOR loops is that they can have a few different syntax options depending on the data you are working with. So it can take a little time to understand all your options.
23rd Sep 2024, 12:31 AM
Jerry Hobby
Jerry Hobby - avatar
+ 4
Enzo Henrique De Oliveira Freitas (Bricky) , your profile shows that you have joined sololearn just 3 days ago. and you have started liearning the tutorial `python developer`. do you have any notable prior knowledge of python? it seems that you want to rush into python, so may be you are missing some of the basics like `for loop`. may be you also miss solving exercises / challenges, thisxwill gain your experience experience. learning takes it's time, so be patient and go one step after the other.
23rd Sep 2024, 6:52 PM
Lothar
Lothar - avatar
+ 3
Thanks y'all. I have now learned it with the app. But, thanks y'all for trying to help. Hope y'all have a blessed evening <3
8th Oct 2024, 6:11 PM
Enzo Henrique De Oliveira Freitas (Bricky)
Enzo Henrique De Oliveira Freitas (Bricky) - avatar
+ 1
Jerry Hobby Slight correction: There are no do while loops in Python
11th Oct 2024, 2:52 AM
Rrestoring faith
Rrestoring faith - avatar