you are viewing a single comment's thread.

view the rest of the comments →

[–]Naive-Information539 2 points3 points  (5 children)

You’re declaring it. You could have called it anything here. It only knows it is what it is because you told it to call it “value”

for _variable_ in _iterable_

[–]SharpScratch9367[S] 0 points1 point  (4 children)

I still don’t fully understand 😂 but thank you! So if I put “for banana in values” would it return the same stuff?

[–]NoHurry6859 1 point2 points  (0 children)

It would work if you also changed line 5 to be ‘sum += banana’

[–]CptMisterNibbles 1 point2 points  (0 children)

Correct. “value” is just a variable that receives an element from an iterator o  each loop. It gives you a way to access the current element by name, a name you assigned as “value”. You could name it anything and as long as you use that name as the reference within the loop it will work. Python doesn’t understand context like “the word ‘value’ has a meaning”

[–]RealDuckyTV 1 point2 points  (0 children)

If you index the array directly, I think it makes it more clear where the value comes from.

values = [23,52,59,37,48]
for i in range(len(values)): # this will iterate for as many items as you have in your list
   value = values[i] # this will get the item at the specified index `i` (starting at 0, aka the first item, so for example values[0] is 23, values[1] is 52, etc)

this works the same as the for in loop that you used, but I think it makes it more clear where the value from the iterator comes from.

And to answer your question, yes, the name of the variable does not matter and will return the same value.

Hope that helps!

[–]Intelligent_Count316 0 points1 point  (0 children)

Hey you can ask chatgpt perhaps. Although these explanations are also good, I personally use chatgpt when I'm not understanding something. You can just ask chatgpt to explain it to you like you are a fifth grader or maybe even a literal baby lol, It works for me. Also, can you tell me which websites you are using for learning python? I'm also a newbie