Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ayokunle Akachi
281 Pointsfor loop
list = [1, 2, 3] for var in list: print(var)
I understand the for loop is the method used to iterate through the list. However, what is var? how does it become a variable to be printed out. Finally, the 'in' function. I thought the 'in' was used to peek inside a list and pull info out. What is it doing here?
2 Answers
Thomas Duff
3,112 PointsHowever, what is var?
The 'var' part in that for loop would just be what you're calling each element of the list. It could also be 'element', 'piece' or 'x'; anything really.
I thought the 'in' was used to peek inside a list and pull info out. What is it doing here?
In is used to select which list you'll be checking each element of. Basically, you're checking each element in the list called 'list'.
Dustin James
11,364 PointsThink if it like this.
I have a basket of fruit. If you wanted me to tell you what kind of fruit you'd ask me:
Tell me each me kind of fruit in your basket.