0
For range and list 😓
Could any1 please explain this to me ? list = [1, 2, 3, 4, 5] for i in list print(i) 1 2 3 4 5 I find it really confusing and don’t quite understand what is happening here
7 odpowiedzi
+ 3
Yes, but not at the same time.
i becomes every element of the list, one after another.
Imagine it as if you do something with every item in a box.
for book in books:
read(book)
for fruit in fruitbox:
eat(fruit)
for pen in penbag:
sharpen(pen)
+ 3
awsome thank you so very much i understand v clearly now ! thank you HonFu and Bhavya Bhav
+ 2
Read it like:
For every element in list that shall be called i...
print(i)
+ 1
In every loop iteration you assign to i one of the elements: First list[0], then list[1], then list[2]...
0
similar to assigning a variable a number, now we assign the entire list to a variable, and every element in the list is now i..
am i right ? HonFu Bhavya Bhav
0
so all the elements in the list are i now ?