cpsc111-Su16-Lab3_.docx - CPSC111 Introduction to Computing...

This preview shows page 1 - 3 out of 7 pages.

CPSC111 - Introduction to ComputingSummer 2016Lab3 – Practice with nested for loops and Lists in PythonLAB MARK: 20 / 25Today's lab exercise provides practice with lists, one of the fundamental programming tools. Most programs that use lists involve loops, so we will be getting more practice with loops at the same time as we explore the power of lists.Almost all programming languages provide a data type similar to Python's lists, but terms such as "array" or "vector" may be used instead of "list". You will work with a partner on this Lab3. Two students should work at a single computer. Occasionally switch the student who is typing. Talk to each other about what you are doing and why so that both of you understand each step. The first thing to do is to create a folder with the name Lab3.Part A: Iteration using for-looprange (start, end, step) where "start" is the starting value of the counter, "end" is the end value and "step" is the amount by which the counter is increased each time.## A first example# Example: the counter-controlled for loopfor digit in range (0, 10, 1): ## same as ‘for digit in range(10):’print (digit)# Note: range (10) is 0 through 9## A second example# Example: the counter-controlled for loopfor some_digits in range (5,10): ## same as ‘for some_digits in range(5, 10, 1):’print (some_digits)# Note: range (5, 10) is 5 through 9## A third examplepg. 1/ am/cpsc111-Su16-Lab3.docx
# How to use a loop within a loop (called a nested for-loop)print ("This is the start of the program")for i in range (1,6):for j in range (1,6):print ("i: " + str(i) + " j: " + str(j) )print() '''Notice that with a loop repeating 5 times,***within*** a loop that repeats 5 timesmeans that you can control 25 processes.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture