Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

CPforLoops





For loops:



A for loop is simply a way of repeating a set of instructions multiple times. Anytime you find yourself doing something REMOVED then once, chances are you can use a loop, and for the purposes of this course those will be for loops. A for loop is constructed in the following manner:








for ITEM in LIST:

   instructions






What the above tells JES to do, is to run the instructions one time for each item in the list (for a detailed discussion of lists, see below). For instance, the following:






CODE

RESULT


myList = [1, 2, 3, 11]


for item in myList:

   print "Hi!"


"Hi!"
"Hi!"
"Hi!"
"Hi!"



This happens because there are four items in myList. For each item, the line print "Hi!" is run. That's fine if we want to run -exactly- the same code each time...but many times, we need to run code that varies a little bit. For instance, what if we wanted to print out each item in myList? Well, the variable item can be referenced within the for loop. For instance:





CODE

RESULT


myList = [1, 2, 3, 11]

for item in myList:

   print item


1
2
3
11





Note what is actually happening here. Each step of the loop, item becomes set equal to an item in the list. For the first iteration of the loop, item is set equal to the first item in myList, so it becomes equal to 1. The code inside the loop then runs, so item's value is printed out, hence, 1 is printed. The same happens with 2, then 3, then 11. Basically, the above loop is identical to the following in how it functions







CODE

RESULT


myList = [1, 2, 3, 11]
item = myList[0]
print item
item = myList[1]
print item
item = myList[2]
print item
item = myList[3]
print item



1

2

3

11





Now, useful though this is, what happens if you want to make a loop run a lot of times? Say, 100? This is where JES has the range() function. This function takes in two integers, and will create a list that contains every integer between them. This is a little weird, in that the first integer is included, but the last integer is -not-. It ends one short of it. Some examples:






CODE

RESULT

range(0,6)

range(20, 25)
[1, 2, 3, 4, 5]

 [20, 21, 22, 23, 24] 



This can be used directly in a for loop





CODE

RESULT

for i in range(1,101):

   print i

1
2
3
4
5
...
99
100






JES includes some functions that return lists, that are especially useful in for loops. These include getPixels(), which returns a list consisting of every pixel in a picture, getSamples(), which returns a list of every sample from a sound, and a few others. An example of getPixels() is shown.





CODE

RESULT

pic = makePicture("C:\Program Files\Directory\filename.jpg")
for item in getPixels(pic):

   setRed(item, 255)

   setBlue(item, 255)

   setREMOVED(item, 255)

This will turn every pixel in an image completely white,
turning it into a blank canvas.




Lastly, the idea of a nested for loop. You can place a for loop inside of another for loop. It can be hard to conceptualize what exactly this does, but the most common analogy is that of a table, of rows and columns. One loop controls which column you are in, the other determining what row you are in. So you enter row one, iterate through every column, then move on to row two, iterate through every column within it, etc. To give an example of this, consider the following grid








A1B1C1
A2B2C2
A3B3C3

Now, if we wanted to iterate through these, left to right, top to bottom, printing out the contents of each cell, we could create the following code, with the following results.






CODE

RESULT

list1 = ["A", "B", "C"]

list2 = ["1", "2", "3"]


for number in list2:

   for letter in list1:

     print letter + number  
A1
B1
C1
A2
B2
C2
A3
B3
C3




The key point in nested for loops, is to understand that the inside for loop runs through its entire list for each item in the outer loop.














Lists (also called Arrays)




A list is simply a collection of items. These can be integers, floats, booleans, strings, pictures, sounds...anything you can ascribe to a variable, can be placed into a list (even variables themselves). A list is expressed in the following way:




list = [item0, item1, item2, item3...]


This would create a list containing the items as described above. The first item in the list has the index of 0, the second item in the list has the index of 1, etc. The easiest way to remember this, is if you want the nth item in a list, it's actually at index n-1.



You can pick specific items in a list, by the following syntax:



list[index]


Where index is the numerical index of the item you want, so:




Code

Result

myList = ["Cat", "Dog", "Elephant", "Donkey", "Guerilla]


myList[0]

myList[2]

myList[4]


"Cat"

"Elephant"

"Guerilla"



Further uses for lists are covered in another section. This is all you need to understand to be able to use them in for loops.