#********************************************************************** # TASK #1: Print the items in a list generated by a range #********************************************************************** X = list(range(50,100,9)) # X will be [50,59,68,77,86,95] #---------------------------------------------------------------------- # Version #1: Use a counter loop. With this version we know where # each item is located (Counter). #---------------------------------------------------------------------- print ("TASK #1 VERSION #1") Counter = 0 while Counter < len(X): print (Counter, X[Counter]) Counter = Counter + 1 #---------------------------------------------------------------------- # Version #2: Use a for-loop with a counter variable. With this # version we also know where each item is located (Counter). #---------------------------------------------------------------------- print ("TASK #1 VERSION #2") for Counter in range(len(X)): print (Counter, X[Counter]) #---------------------------------------------------------------------- # Version #3: Use a for-loop on the master list itself. We DON'T # know where each item is located, but if we don't need to know then # the code is significantly simpler than the earlier versions. #---------------------------------------------------------------------- print ("TASK #1 VERSION #3") for Item in X: print (Item) #********************************************************************** # TASK #2: Print the characters in a string (this is really the same # as task 1 in a lot of ways). #********************************************************************** Z = "This is a long string" # Define the string to step through #---------------------------------------------------------------------- # Version #1: A tried-and-true counter loop. With this version we # know the position of each character in the string. #---------------------------------------------------------------------- print ("TASK #2 VERSION #1") Counter = 0 while Counter < len(Z): print (Counter, Z[Counter]) Counter = Counter + 1 #---------------------------------------------------------------------- # Version #2: Use a for-loop keyed off the length of the string # to step through and print each character. With this version we # also know the position of each character in the string. #---------------------------------------------------------------------- print ("TASK #2 VERSION #2") for Counter in range(len(Z)): print (Counter, Z[Counter]) #---------------------------------------------------------------------- # Version #3: Just step through the characters of the string, printing # each one. We DON'T know where each item is located, but if we don't # need to know then the code is significantly simpler than the earlier # versions. #---------------------------------------------------------------------- print ("TASK #2 VERSION #3") for Item in Z: print (Item) #---------------------------------------------------------------------- # Version #4: We could use a literal string here, too. #---------------------------------------------------------------------- print ("TASK #2 VERSION #4") for Item in "This is a long string": print (Item)