eHow launches Android app: Get the best of eHow on the go.

How To

How to Create a for Statement in Python

Contributor
By eHow Contributing Writer
(2 Ratings)

Oftentimes, a need arises to go through all the elements in a list or perform an operation over a series of numbers. The Python for statement is a powerful and flexible tool that gives you the ability to easily go through a variety of different types of lists and ranges.

Difficulty: Moderately Easy
Instructions

    Create a For Statement With a List

  1. Step 1

    Create a Python list that contains the data you wish to work with:
    mylist = ['passion fruit', 'kava kava', 'acai', 'pomegranate']

  2. Step 2

    Determine what operations you wish to do as you go through each element in the list. For this example, you will take each element in the list you just declared and print out the word "Fruit:" following the list element.

  3. Step 3

    Create a for statement using the list:
    for myitem in mylist:
    1
      print 'Fruit: ' + myitem

    This will print the following:
    Fruit: passion fruit
    Fruit: kava kava
    Fruit: acai
    Fruit: pomegranate

  4. Create a For Statement With a Range

  5. Step 1

    Determine a range of numbers you wish to loop over. For this example, you will choose the range starting at 5 and ending at 10.

  6. Step 2

    Determine the events you wish to occur while going through the elements in the range. For this example, you will add 10 to each element in the range and print them out.

  7. Step 3

    Create a for statement using the range() method. You will store each element in the range in your counter variable k. The range method will generate a list starting at its first element and ending just before the last element. The range extends to the second parameter. However, it includes the second number. Thus, in this example, you will call range(5,11):
    for k in range(5,10):
      print k+10

    This will produce the result:
    15
    16
    17
    18
    19
    20

Tips & Warnings
  • You can use the for statement with any number of methods that result in a list. Common uses for the for statement include looping through a Python dictionary element, lines in a file or incoming data through a network socket.
  • Guido Van Rossum's Python documentation offers extensive documentation on all the wonderful tricks you can perform with Python's for statement (see Resources below).
  • The for statement in Python is quite unlike the for statement in C-like languages. Unlike C, Perl or Java, the for loop contains no conditionals for stopping and does not have the ability to have an arbitrary increment condition within the for statement itself.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

Demand Media
eHow_eHow Technology and Electronics