-
Step 1
Create a Python list that contains the data you wish to work with:
mylist = ['passion fruit', 'kava kava', 'acai', 'pomegranate'] -
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.
-
Step 3
Create a for statement using the list:
for myitem in mylist:1
print 'Fruit: ' + myitem
This will print the following:Fruit: passion fruitFruit: kava kavaFruit: acaiFruit: pomegranate -
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.
-
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.
-
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











