#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12

    No clue about (for)


    Well hello

    Could you guys teach me how to use the (for) loop because i don't understand.

    I know the while but not for.

    And please give examples and discribe or I won't get it.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Here is an example.
    Code:
    itemlist = ['noob', 'hello', 'test', 'chocobo']
    for item in itemlist:
         if 'e' in item:
              print item
    This will print all of the items in the itemlist that contain 'e'. THis should work for you.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    okay thanks, but where does the item come from
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    The item is automatically created, just try it out in your Interpreter (Python Shell) and see what happens.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Yes, item is just created. Things in a list don't have names normally, so you nead to come up with one to use them.

    It reads in English as "for each thing in the list, do this:"

    or

    Take each thing in the list and temporarily give it a name ('item').
    Run the following code on that name.
    Go to the next thing in the list and do it again.
    When you get to the last thing, move on.


    The name doesn't matter, it's just for the programmer to read. These both do the same thing:
    Code:
    for number in [1, 2, 3]:
        print number
    
    1
    2
    3
    Code:
    for banana in [1, 2, 3]:
        print banana
    
    1
    2
    3

    The term for "going through each item in a list" is iteration, and "for" iterates. I say this because Python uses it a lot, wherever it makes sense.

    You can use "for" to go through each item in a list.
    Code:
    >>> for i in [1, 2, 3]:
    >>>    print i
    1
    2
    3
    each character in a string:
    Code:
    >>> for justsomename in "hi":
    >>>    print justsomename
    'h'
    'i'
    each line in a file:

    Code:
    for lineOfText in file('c:/atextfile.txt'):
        print lineOfText
    and so on.
    Last edited by sfb; December 15th, 2004 at 07:32 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    11
    monkeyman23555, I will persume that you have a good knowledge of variables
    A for statement in python, is simply a branch of code which will be execute for the length of elements in list, and assigns everytime the variable name give, to the item in list
    confused, let's take it one by one
    PHP Code:
    for i in list:
             print 

    for ==> a built in key
    i ==> this is a variable that will be assigned everytime the loop goes on, inside the code e.g print i, i will be assigned with each element of the list
    in ==> in
    list ==> any list or iterable item like a tuple

    PHP Code:
    for i in range(20):
              print 

    to get this short, range is a function, which takes a number a generated a list from 0 to that number - 1
    so in order -for- operates it must takes a list, and you should put a variable name, just any name for using it inside the code of the loop

    Simulating:
    PHP Code:
    for N in [1,4,5]:
          print 

    This code will be executed 3 times, N will move to the first element, e.g N = 1, then execute the code branch (print N), then N = 4, execute (print N) and finally N = 5, and execute (print N)
    so each time, N got the value of the item in list


    also read the python official tutorial, it will help you in understanding how loops truly work
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    11
    p.s string, tuples, list are all iterable keys, which is sorted by numbers
    e.g list[0] will get you first item in list, and "foo"[0] will get you first character
    also, there's other forms of -for- for using it with dictionaries


    PHP Code:
    #Consider Dict as a Variable holding a Dictionary
    #e.g Dict = {'name':'John','title':'Mr.'}....
    for k,v in Dict.items():
            print 
    k,
    for is also valid for dictionaries, but you must assign two variables instead of one, but everything is just same as the earlier for loops
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Okay I get the point Thanks

    I have another question about Boa Constructor. How can I use it how do i insert a script or do I have to make it right there and then how do I make a graphical window?

    I am really anoied I just don't get it.
  16. #9
  17. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Post about that in a new thread so people will see it.
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just to fill in some of the blank spots and add a few examples of my own .

    As of Python 2.x a Dictionary objects keys can be iterated over directly using the same form as any other Python sequence. This is usually tidier than calling the items() method; though there are times when it's easier to use items(), in general you would do something like this:

    Code:
    >>> aDictionary = {'keyOne': 'One', 'keyTwo': 2, 3: [1, 2, 3]}
    >>> for key in aDictionary:
    ...     print key
    ...
    3
    keyOne
    keyTwo
    >>> for key in aDictionary:
    ...     print aDictionary[key]
    ...
    [1, 2, 3]
    One
    2
    >>>
    As you can see, the first for loop simply iterates over the keys in the dictionary and prints them to stdout. The second loop works in basically the same way but this time the key is used to print the associated value.

    You can also access the values on a List/Tuple/Set etc. in a similar way (aSequence[index/subscript]).

    Code:
    >>> aSequence = ('One', 'Two', 'Three')
    >>> for index in range(len(aSequence)):
    ...     print index
    ...
    0
    1
    2
    >>> for index in range(len(aSequence)):
    ...     print aSequence[index]
    ...
    One
    Two
    Three
    >>>
    This does pretty much the same thing as the other examples above. The only real difference here is the way in which the index/subscript is generated -- using the range() function. Anyway time for one more example.

    Code:
    >>> for index, value in enumerate(aSequence):
    ...     print index, value
    ...
    0 One
    1 Two
    2 Three
    >>>
    After the previous examples this one should be pretty self explanatory but put simply it assigns the location within the sequence to the first variable [index] and the value at that location to the second variable [value].

    Note: The enumerate() function only works properly with List, Tuples & Sets and not with Dictionaries.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by monkeyman23555
    I am really anoied I just don't get it.
    It's always hard learning your first programming language, the second is easier and this third is easier still! The only thing i can really suggest is that you sit down with some good introductions and read them though i.e. the "Python 101" series here on Devshed good articles.

    So if you can't stomach Guido van Rossums Python Tutorial thats probably a good place to start. Also, I believe that you already have "Learning Python" by Mark Lutz & David Ascher .

    Here are a few links that you might be interested in:

    http://www.python.org/doc/Intros.html
    http://www.devshed.com/c/a/Python/Getting-Started-With-Python/
    http://www.python.org/moin/BeginnersGuide

    The best way to learn is by doing, so if you see an example that you don't quite understand open the Python shell and try it out! Always helps me .

    Edit: Python 101 seems to have been removed from Devshed along with some of the older articles so you might want to google them and see if anything comes up.

    Hope this help,

    Mark.
    Last edited by netytan; December 19th, 2004 at 02:39 PM.
    programming language development: www.netytan.com Hula

  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Okay thanks

IMN logo majestic logo threadwatch logo seochat tools logo