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 (2015) Logic in Python Loop

I didnt quite understand for for loops section, if anyone could help explain it a little better.

What really stumped me was the challenge: hellos = [ "Hello", "Tungjatjeta", "Grüßgott", "Вiтаю", "dobrý den", "hyvää päivää", "你好", "早上好"

] for words in list: print(words + " World")

while i did end up getting the answer correct, I didnt quite understand why. Where i put in the word "words" I dont get what that is a place holder for, or why any name works in that spot. The last piece i dont understand is why printing "words" (without quotations) works, is words now taking the place of hellos, sorry if im making no sense. All help is fully appreciated!!!

4 Answers

Van Sanders
Van Sanders
16,087 Points

When creating a for loop, the iterator is just a placeholder that is used within the loop to refer to the current item being passed through it. So for example:

for item in this_list:
    print(item)

here, item could have been called 'i', or 'thing', 'element' or really anything... The important thing is that you use that same temporary variable name to refer to the part of the list that is currently passing through the loop, in the rest of the loop block. Its just a placeholder name that works like any other variable.

If the item is a string, it will act like a string when the loop reads that item variable, no quotes needed. This is because the value stored within the variable is what is actually being passed through the for loop, not the name of the variable itself. so, in the following:

item = "crazyWord"

the loop works with the value (in this case the string "crazyWord") that is the stored in the variable name. We just toss around the variable name to save time and to keep from repeating ourselves and writing way more code!

I think I may have rambled a bit there, but I hope it was a helpful ramble. If not, I'll try again!

Thank you so much!!! It makes perfect sense now and i feel confident to continue forward with my python journey, if there is a +rep feature I would defiantly give it to you, thanks so much!

Van Sanders
Van Sanders
16,087 Points

Glad I could help! Actually there should be a way to select my answer as a "best" answer, if you think it was.

I would much appreciate the vote!

Take care,

Van

The only person I'm allowed to give best answer to is myself...

Van Sanders
Van Sanders
16,087 Points

Hah! Maybe its because I commented on your post rather than submitting an actual answer.

No worries! What matters is that you figured it out :)

thats the reasoning, and yea if youd like copy and paste it as an answer i will be more than glad to upvote it!

I really do appreciate you going out of your way to explain something!