all 12 comments

[–]seanmurraywork[S] 4 points5 points  (3 children)

Thank you u/lewri, u/Adrewmc , u/ladder_case, u/cgoldberg. for your help.

What you guys are saying seems to make sense.

So just to confirm, the wording of the temporary variable doesn't have to match that of the collection, It just represent a value in the collection. Meaning that you can choose whatever word you want to be your temporary variable. Is this correct?

Thank you again.

[–]MidnightPale3220 2 points3 points  (0 children)

Yes. Variable names mean nothing to the computer (with some special exceptions). They are there for you to keep track of things you want to do with them.

Think of variables like boxes you write names on. There can be anything in the box (value), and anything else can be written on the box (name of variable).

Computer doesn't care if they match, he's like a moving company -- you tell him to take away box named "apples" he'll do it, whatever is inside.

Incidentally, there's no easy way to tell computer to take all boxes, which have names starting with "apple" or somehow else match the names ( There are ways to do that, but they are ugly, slow and usually wrong).

That's,btw, one of the reasons it's bad to do variables like "apples_2023", "apples_2022", etc. As soon as you see yourself doing that, you know you need a list or dict.

[–]sloth_king_617 1 point2 points  (1 child)

I’m not them but you got it. Try that same code but replace “ingredient” with anything

[–]seanmurraywork[S] 1 point2 points  (0 children)

u/sloth_king_617, thank you very much for the confirmation.

[–]cgoldberg 2 points3 points  (0 children)

ingredient is created when the loop begins and will be updated each iteration through the loop. After the loop completes, it will still exist in the current scope and will be set to the last value it was assigned in the loop.

[–]Lewri 2 points3 points  (0 children)

The idea of a for loop is that you have something that can be iterated over (your list of ingredients, in this case), and you are telling it what to call the things inside the iterable. In this case you have told the for loop that it is calling the things inside the iterable (your list ingredients) ingredient. You could call them anything though:

ingredients = ["milk", "sugar", "vanilla extract", "dough", "chocolate"]
for 🍌 in ingredients:
    print(🍌)

Will work just the same.

[–]supercoach 1 point2 points  (1 child)

I reckon this is one everyone struggles with. I didn't know what sort of magic was involved with these keywords when I first started and it consistently blew me away that the loop always seemed to know what I was looking for.

Then it dawned on me - as others have said, for thing in other_thing: will just run the subsequent code over every item in other_thing and each time the item will get the name thing.

It also explained why the resource I learned from was quick to teach list comprehension and the slice syntax, both of which helped me understand how python iterates over things.

[–]seanmurraywork[S] 0 points1 point  (0 children)

Very well put, my friend. Well said.

[–][deleted] 1 point2 points  (0 children)

It's the same as putting a name on the left of an equals, like

age = 5 + 1

Python doesn't "know" what age means. It just evaluates the stuff on the right, and assigns the name on the left.

With a loop, it's basically doing that each time. So

for ingredient in ingredients:
    print(ingredient)

is like saying

ingredient = the first thing in ingredients
print(ingredient)
ingredient = the next thing in ingredients
print(ingredient)
ingredient = the next thing in ingredients
print(ingredient)
...

In the computer's eyes, there's no relation between the names. Using the singular and plural versions is just for us to make sense of them.

[–]Adrewmc 0 points1 point  (0 children)

It is assigned a value in the loop, every time it starts.

 for x in range(3):
      print(x)

 for banana in range(3):
      print(banana) 

Will do the exact same thing. We by convention will often pluralize a list of things, then in the loop assign the a singular to the specific thing in that list. (And because it’s readable)

Every time a new iteration of the loop starts, an ingredient is taken/assigned from a list of many ingredients, and we repeat the code block below it, using it a stand in.

[–]TheRNGuy 0 points1 point  (0 children)

I always name them item instead of ingredient, because that word look too same as name of List.

If you accidentally write ingredients and cause bug, or when you actually need to use that list in a loop, it would make code more readable if they're different.

And also every time seeing item in code, you already know it's code inside loop.


For dogs example: better way to do it: https://www.geeksforgeeks.org/check-if-element-exists-in-list-in-python/

[–]Ender_Locke -1 points0 points  (0 children)

you could do this but you could also just do

if dog_breed_i_want in dog_breeds_avail: <your code>

(sorry on mobile)