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

Python Python Basics (Retired) Shopping List Shopping List Project

for/in

what do for and in do i know they have something to do with naming lists but i am confused on what there purpose is since you are not printing out the word "item" that was given in the video what is the point in using it.

1 Answer

'for' is setting up a loop. If you have ever done programming before, the concept is the same. Say you have a python list:

fab_four = ['george', 'paul', 'john', 'ringo']

now you can set up a loop with your 'for/in' statement like:

for beatle in fab_four

which will then loop through all the members in that list and effectively assign the variable name 'beatle' to whatever is in the list for each iteration. iteration 1 will yield, beatle = 'george', iteration 2 will be beatle = 'paul', etc... until all members of the list are exhausted at which point the loop will end and any statements that appear after the for loop will be implemented.

If you, for example, wrote

fab_four = ['george', 'paul', 'john', 'ringo']
for beatle in fab_four:
    print(beatle)

the result would be

'george'
'paul'
'john'
'ringo'

because the print statement is using the argument 'beatle' every time the for loop iterates.

does that make sense?