Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loops in python
#1
Hey guys! So, I am a new person to these forums, and I am learning python right now. Currently, I'm learning about for loops, but I'm not super sure on how exactly they work. Here's an example of some code from the course I'm taking:

list = [5, 2, 1, 4, 9, 10]
min_value = 0
for item in list:
    if item < min_value:
        min_value = item
print(min_value)


What I'm unsure about is how exactly they got "item" in line 3. Can someone help me out and explain it a bit clearly for me please? Thanks!
Reply
#2
Yeah, item is simply decided by the programmer as the name to use for an element in the list. If you change it to "x" the code will behave exactly the same, as long as you update it everywhere. It's similar to a function parameter - you pick the name.
Reply
#3
(Apr-17-2020, 11:33 PM)micseydel Wrote: Yeah, item is simply decided by the programmer as the name to use for an element in the list. If you change it to "x" the code will behave exactly the same, as long as you update it everywhere. It's similar to a function parameter - you pick the name.

Oh ok, that makes sense now. Thanks for the help!
Reply
#4
Also note that list is built-in function. It shouldn't be used as name, because this way it shadows the built-in function you cannot use it. I understand this is simple example code, but anyway keep it in mind
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why both loops don't work in python? nau 3 1,091 Sep-21-2022, 02:17 PM
Last Post: rob101
  Why does Python not use parenthesis to contain loops? davidorlow 3 3,470 Jun-09-2021, 06:33 AM
Last Post: Gribouillis
  Python for loops Kristenl2784 3 47,642 Jun-16-2020, 06:01 PM
Last Post: Yoriz
  For loops help, using python turtle SemiBeginnerPY 2 3,945 Mar-10-2020, 10:46 AM
Last Post: SemiBeginnerPY

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020