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

26th May 2019, 3:00 PM
Akshay Ramchandra
Akshay Ramchandra - avatar
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)
26th May 2019, 4:25 PM
HonFu
HonFu - avatar
+ 3
awsome thank you so very much i understand v clearly now ! thank you HonFu and Bhavya Bhav
26th May 2019, 4:52 PM
Akshay Ramchandra
Akshay Ramchandra - avatar
+ 2
Read it like: For every element in list that shall be called i... print(i)
26th May 2019, 4:05 PM
HonFu
HonFu - avatar
+ 1
In every loop iteration you assign to i one of the elements: First list[0], then list[1], then list[2]...
26th May 2019, 4:21 PM
HonFu
HonFu - avatar
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
26th May 2019, 4:20 PM
Akshay Ramchandra
Akshay Ramchandra - avatar
0
so all the elements in the list are i now ?
26th May 2019, 4:23 PM
Akshay Ramchandra
Akshay Ramchandra - avatar