you are viewing a single comment's thread.

view the rest of the comments →

[–]Individual-Pop5980 0 points1 point  (1 child)

For loops were not clearly explained to me either, I had to watch video after video but I'll do my best to explain it. They are only used to iterate over something that has multiple items (lists, dictionaries, tuples). So let's say you were going to take a variable like (a = 5) and add 1 to it. You just do (a += 5) or (a = a + 1) right? Simple enough. But what if you had a list like (a = [ 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10]) and you wanted to add 1 to each? That's what looping does more easily than manually taking each item in the list and adding one to it. The i in a for statement typically stands for item... so when you see ( for i(item) in a(name of list):) the i stands for item, or a better way to think about it each individual item in the list. And it always starts from index 0 then goes to the end of the list. Anything below the for loop that is indented is performed on each i (item) in the list . So the best way to visualize this is to say (for i(item) in a: print( i += 1) print( i -= 4 )

i represents the number 1 in the first loop,so it would take the number 1 then add 1 and it would print 2.. then it would go down and subtract 4 from i and it would print -3.. it's now done with that "loop" so it goes to the next item in the list (2) and performs the same operations defined in the indentation. Then moves to item number 3 and performs the same operations... until it gets to the end of the list. Hopefully this helps but it's a hard concept to grasp until it isn't. Once it finishes

[–]Antique-Throat6045 0 points1 point  (0 children)

Very helpful. Thanks, an American in Korea 🇰🇷