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

John Faires
John Faires
1,361 Points

A feel like this should be a no brainer... but need an answer.

Does Python automatically understand that "item" is part of "items"?

It appears in a couple of examples Kenneth does the variable "words" is able to be used by simply writing "word" in reference to the contents of "words".

I would have assumed if you had two things in your code named "items" and "item" they would simply be understood as two separate variables with no relation. The same as "apples" and "oranges".

For a more concrete example, in the code below (the shopping list code) You have a variable "new_item" and I can follow throughout how this is handled until you call the print function to print the individual items out using the variable "item". No where is "item" declared as the contents of shopping_list, and I guess I'm trying to figure out how Python was able to take "new_item" and relate it to "item".

Can anyone confirm how this would behave, or help me understand this? Sorry if this is a dumb question.

Thanks.

make a list to hold onto our items

shopping_list = []

print out instructions on how to use the app

print("What should we get from the grocery store?") print("Enter 'DONE' to stop adding items.")

while True: # ask for new items new_item = input("> ")

# be able to quit the app
if new_item == 'DONE':
    break

# add new items to our list
shopping_list.append(new_item)
print("Item added to your list")   

print out the list

print("Here's your list")

for item in shopping_list: print(item)

2 Answers

Idan Melamed
Idan Melamed
16,285 Points

The for loop is the critical thing to understand here. I know I had a hard time grasping it.

For loops in Python usually loop through a list.

When Python reads this line:

for item in shopping_list:
 print(item)

What you are basically telling Python is this: Hey Python, I got a list called shopping_list. I want you to go through every index in that list, assign it to a variable called 'item' then print it.

So python goes something like:

item = shopping_list[0]
print item
item = shopping_list[1]
print item
item = shopping_list[2]
print item
...

Until it finishes going through the list.

And you were right! Python does not automatically understands that item is a part of items.

We can just as easily write:

for doctors in doctor :
  print(doctors)

Then Python will go through the list 'doctor', assign every value to the variable 'doctors' and print it. It's just common practice to be so clear about the variable names that you can sort of instinctively understand them.

I hope my explanation actually made it better and not worse. :-)

John Faires
John Faires
1,361 Points

Thank you so much for explaining this. That one line essentially creating a temporary variable (for variable in variable) is what I was missing, or failing to understand. Realizing that line actually created a variable was the unlock.

Thanks again.

In this line:

for item in shopping_list: print(item)

Item is a temporary variable created specifically for the for loop, once the loop ends the variable expires.