Complete beginner, any help appreciated :) - For Loops

Discussion in 'Python' started by Mark, Dec 1, 2011.

  1. Mark

    Mark Guest

    Hi there,

    I'm a complete beginner to Python and, aside from HTML and CSS, to coding in general. I've spent a few hours on it and think I understand most of the syntax.

    However, I'm wondering a bit about For Loops. I know that the basic syntax for them is to define a list, and then to use something like:

    for x in y

    However, what does "for" and "in" mean in this context? Can anyone help me to understand this? I know it's a really basic question, but hopefully it will see me on my way to coding properly :)

    Thanks a lot.
     
    Mark, Dec 1, 2011
    #1
    1. Advertisements

  2. Laurent Claessens, Dec 1, 2011
    #2
    1. Advertisements

  3. That means (in a free translation)

    "For each one of 'x' in 'y', do this"

    'y' is a list, for example, then it means: "For each one of the elements
    of the list 'y' (the element on the current iteration is named 'x'), do
    this"

    Good Luck!

    Att;
    Pedro Henrique G. Souto
    <>
    â•”â•â•â•â•â•â•â•â•â•â•â•â•â•â•—
    ║ ²²²d○_○b²²² ║
    â•šâ•â•â•â•â•â•â•â•â•â•â•â•â•â•
     
    Pedro Henrique G. Souto, Dec 1, 2011
    #3
  4. Mark

    Dave Angel Guest

    And if y is a string, it means

    for each character in y, do this
    x = the character
    (then do the indented part)

    And if y is an arbitrary iterable, it means

    for each thing the iterable produces
    x = the thing
    (then do the indented part)

    Each time you go through the loop, x will be equal to the next item in
    the sequence. So you can systematically process the items in the list
    (or characters in the string, or values from an iterable), one at a time.

    The only real caveat is to not do something that would change the list
    (etc.) while you're looping through it.

    What are some other examples of iterables?

    infile = open("myfile.txt", "r") #infile is an iterable
    for line in infile:
    print "**", line, "**"

    for val in (1, 4, 9, 2007)

    for x in xrange(50000000):
    #this does the same as range(), but doesn't actually build the list.
    #this way, we don't run out of memory

    You can also code your own generator function, which is an iterable, and
    may be used like the above.

    Some things in this message assume Python 2.x. in Python 3, range works
    differently, as does print.
     
    Dave Angel, Dec 1, 2011
    #4
  5. Mark

    Mark Guest

    Thanks a lot for the answers everyone, I really appreciate you getting back to me so quickly. I think that I understand where I am with this now :)
     
    Mark, Dec 1, 2011
    #5
  6. y is some list ["help","beginner","question"]

    for x in y:
    if x=="question": print "FOUND"


    Clear?

    Cheers.
     
    Kyle T. Jones, Dec 2, 2011
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.