#------------------------------------------------------------ # Construction of Lists # Copyright (C) February 8, 2017 -- Dr. William T. Verts #------------------------------------------------------------ #------------------------------------------------------------ # Construction of a list L by running through a while-loop. # Note that the list starts off empty, then each pass through # the loop one new item [I] is added to the end of the list. #------------------------------------------------------------ def Main1(): L = [] I = 0 while (I < 10): L = L + [I] I = I + 1 print L return #------------------------------------------------------------ # This is the same as above, but uses a for-loop to replace # the while-loop. The for-loop initializes I to the first # value from the range (which is 0), and runs the body of the # loop for each successive member of that range. Note that # both Main1 and Main2 end up with L set to the list: # [0,1,2,3,4,5,6,7,8,9], which could have been done much more # easily by simply setting L = range(10). #------------------------------------------------------------ def Main2(): L = [] for I in range(10): L = L + [I] print L return #------------------------------------------------------------ # In this version, L is set to a list that CANNOT be # generated by a simple range function. For each value of # I, half that value (as a float) is appended to the list. # The result is [0.0, 0.5, 1.0, 1.5, ... , 4.0, 4.5] #------------------------------------------------------------ def Main3(): L = [] for I in range(10): L = L + [I * 0.5] print L return #------------------------------------------------------------ # The range function can only return lists of integers. # However, a list comprehension can generate many different # types of lists. In this example, the list constructor [], # the for-loop, and the computed expression are all folded # into the list comprehension. This generates the same list # as in Main3: [0.0, 0.5, 1.0, ..., 4.5] #------------------------------------------------------------ def Main4(): L = [I * 0.5 for I in range(10)] print L return #------------------------------------------------------------ # Here is a case where there is an if-statement inside the # fop-loop. The expression I % 2 gives the remainder of # dividing I by 2, and that is tested for equality with 1, # so therefore the expression (I % 2 == 1) is asking if I is # and odd number. For each value of I taken from the range # 0 to 9, only those values that are odd have half their # value appended to the list. That is, the list ends up # as: [0.5, 1.5, 2.5, 3.5, 4.5] (or half of the odd integers # 1, 3, 5, 7, and 9). #------------------------------------------------------------ def Main5(): L = [] for I in range(10): if (I % 2 == 1): L = L + [I * 0.5] print L return #------------------------------------------------------------ # This returns the same value as Main5, but embeds both the # for-loop and the if-statement into the list comprehension. #------------------------------------------------------------ def Main6(): L = [I * 0.5 for I in range(10) if (I % 2 == 1)] print L return #------------------------------------------------------------ # Try these expressions in the interactive area on your own: #------------------------------------------------------------ [0 for I in range(10)] [I for I in range(10)] # Is this dumb, or what? [I*I for I in range(10)] [sqrt(I) for I in range(10)] [sqrt(I) for I in range(10) if I % 2 == 0] [I for I in "FROG"] ["*" + I + "*" for I in "FROG"] #------------------------------------------------------------ # Simple Image Processing: Brighten Every Pixel in an Image. # Every line except the def and the return contain functions # that are SPECIFIC to JES, and are not part of standard # Python. This is where we will pick up next class. #------------------------------------------------------------ def Main7(): Filename = pickAFile() Canvas = makePicture(Filename) show(Canvas) for PX in getPixels(Canvas): setRed(PX, getRed(PX) + 20) setGreen(PX, getGreen(PX) + 20) setBlue(PX, getBlue(PX) + 20) repaint(Canvas) return