Where is 'breed' defined?

Can someone please explain why the below "breed’ term yields all the names when nothing in the code is identified or saved as a variable called “breed”?

dog_breeds = ['french_bulldog', 'dalmatian', 'shihtzu', 'poodle', 'collie']
for breed in dog_breeds:
  print(`breed`)

you define the breed variable here:

for breed in dog_breeds:

python will now assign the items of the list to the breed variable (one at a time). Python does a lot of heavy lifting for you

65 Likes

In a for-in expression, for item in collection, item is the iteration variable. It is successively assigned the value of each element in the iterable collection.

(What is an iterable? Easy. Something that you can do for-in with! :slight_smile: )

44 Likes

Thank you all,

This made a lot sense now, it was confusing at first but your explanations helped.

Thanks :slight_smile:

9 Likes

How does python know that what “breed” as it was not specifically defined…“def dog_breed”

dog_breeds = [‘french_bulldog’, ‘dalmation’, ‘shihtzu’, ‘poodle’, ‘collie’]
for breed in dog_breeds:
print(breed)

“dog_breed” v “breed”

14 Likes

you define breed here:

for breed in dog_breeds:

python will assign the values from the list to breed (or whatever variable name you decide to give this variable)

24 Likes

I was going to ask just this question. I have been coming across loop on the side and this has been a major headache for me. Reading the response from mtf make me feel good

You won’t know how much relief you caused with this response. I have struggled with this for looooonnngggg (even as simple as it seems

2 Likes

But if I use anything other than “breed” (just to chek this out I tried “bred” and “breeds”) I get this error:

Traceback (most recent call last):
File “script.py”, line 3, in
print(breed)
NameError: name ‘breed’ is not defined

So the explanation given by everyone does not hold up. Python doesn’t assign those values and throws back an error.

1 Like

If you use your code editor’s search/highlight functionality and type in “breed” then presumably you are likely mentioning it in two places. One place where you are defining it, one place where you are using it.

in other words, this won’t work:

for bred in dog_breeds:
print(breed)

but this will:

for bred in dog_breeds:
print(bred)

in the first example, i “forgot” to change ‘breed’ into ‘bred’ when i print it…

1 Like

I am asking myself the same question?

If I understand it correctly the following code:

dog_breeds = [‘french_bulldog’, ‘dalmatian’, ‘shihtzu’, ‘poodle’, ‘collie’]
for breed in dog_breeds:
print(breed)

the ‘breed’ portion in “for breed in dog_breeds:” is a temporary variable that is defined by each iteration of the list. Meaning, at first pass, the variable ‘breed’ takes the value of the first index in the list -french_bulldog in this case. Then the “print(breed)” instruction prints the value of the temporary variable, french_bulldog. Then the loop proceeds to each other item in the list until it reaches the last index.

I’m new to programming too, and struggling with loops as well; the above is my very newby understanding of a simple loop.

1 Like

I’d be a little careful about referring to it a temporary variable since after the loop breed is still referencing the last object from whatever iterable the loop was stepping through. It’s not in a different scope and can still be accessed.

dog_breeds = ["french_bulldog", "dalmatian", "shihtzu", "poodle", "collie"]
for breed in dog_breeds:
    print(breed)
print(breed)
Out:
french_bulldog
dalmatian
shihtzu
poodle
collie
collie

Otherwise I think you’ve got the right idea.

3 Likes

Here, breed is temporary variable assigned the value of an item(which means it teporarily holds a copy of the list item value) each time the for loop iterates.
One thing to notice is even if the value of breed changes (which is in fact of copy value of the list item), there’s no affect in the original list

The iterable can be literally any name you choose. The word “breed” is used in the example because it makes the most sense in this case. However, you could use the standard “i” for the iterable instead:

for i in dog_breeds:
     print(i)
 
french_bulldog
dalmatian
shihtzu
poodle
collie

2 Likes

i is generally used as abbreviation for index, so not sure that is the best fit here. Giving a descriptive name for what the values are going to be is better

2 Likes

Think of ‘breed’ as a container with no intrinsic value. ‘For’ loop assigns the values from the list ‘dog_breeds’ to the container and the print command prints out whatever is stored in that container.

3 Likes

Hope no-one gets offended, but I was thinking about the same, checking out the replies here I came out with:

dog_breeds = ['french_bulldog', 'dalmatian', 'shihtzu', 'poodle', 'collie']
for gents_sausage in dog_breeds:
    print(gents_sausage)

still works ;)… stay calm :slight_smile:

1st edit: oh hey it gets censored :smiley:
2nd edit: now you get it:)))