Lesson 6 Dealing with Sequences of Objects Chapter 1: Introduction Chapter 2: Lists Chapter 3: More on lists Chapter 4: for loops [4.1] for loops [4.2] for loops in action [4.3] continue [4.4] continue the action [4.5] range [4.6] Practise your range! [4.7] More patterns [4.8] for or while? Chapter 5: Applied problem solving Chapter 6: An army of robots Chapter 7: Tuples Chapter 8: Strings as a sequence Chapter 9: Testing and debugging Chapter 10: Sequences style guide Chapter 11: Summary Chapter 4: for loops Practise your range! face Josiah Wang Surprise! We’ll skip the quiz this time. Instead, let’s get you to practise writing for loops with the range object! 1 2 3 4 5 6 7 ❮ ❯ Question 1 Write a piece of code that uses a for loop to print out even numbers from 0 up to (and including) 10. Sample output: 0 2 4 6 8 10 Answer Toggle sample solutions Sample solution: Two possible solutions: maximum = 10 for number in range(0, maximum+1, 2): print(number) maximum = 10 for number in range(maximum+1): if number % 2 == 0: print(number) Question 2 Write a piece of code that uses a for loop to print out even numbers from 10 down to (and including) 0. Sample output: 10 8 6 4 2 0 Answer Toggle sample solutions Sample solution: Three possible solutions: maximum = 10 for number in range(maximum, -1, -2): print(number) maximum = 10 for number in range(maximum, -1, -1): if number % 2 == 0: print(number) This example uses the list reverse idiom that we covered in a quiz earlier. maximum = 10 for number in range(0, maximum+1, 2)[::-1]: print(number) Question 3 Remember the triangle of stars from Lesson 4? Write a version that uses a for loop to produce the same output! Enter an integer: 5 * ** *** **** ***** Answer Toggle sample solutions Sample solution: Possible solutions: rows = int(input("Enter an integer: ")) for current_row in range(rows): print("*" * (current_row+1)) rows = int(input("Enter an integer: ")) for current_row in range(1, rows+1): print("*" * current_row) Question 4 Here is a fun one. Use for loops to print out the following pattern. 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 You can use print("whatever", end=" ") to force Python to add a space after printing a value, rather than a new line which is the default. So the following piece of code will print 1 2 3. print(1, end=" ") print(2, end=" ") print(3, end=" ") Answer Toggle sample solutions Sample solution: One possible solution: for row in range(1, 10): for col in range(1, 10): print(col, end=" ") print() # prints a new line Question 5 Another pattern! Use for loops to print out the following pattern. It should just be a small modification of your previous code. 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 Answer Toggle sample solutions Sample solution: One possible solution: for row in range(1, 10): for col in range(1, row+1): print(col, end=" ") print() # prints a new line Question 6 Was that fun? Try this pattern - it should just be a teeny weeny modification of your previous code. 1 * 1 2 * 1 2 3 * 1 2 3 4 * 1 2 3 4 5 * 1 2 3 4 5 6 * 1 2 3 4 5 6 7 * 1 2 3 4 5 6 7 8 * 1 2 3 4 5 6 7 8 9 * Answer Toggle sample solutions Sample solution: One possible solution: for row in range(1, 10): for col in range(1, row+1): print(col, end=" ") print("*") Question 7 One last pattern! Again, this only needs just one teeny weeny tweak from the previous one! * 1 * 1 2 * 1 2 3 * 1 2 3 4 * 1 2 3 4 5 * 1 2 3 4 5 6 * 1 2 3 4 5 6 7 * 1 2 3 4 5 6 7 8 * Answer Toggle sample solutions Sample solution: One possible solution: for row in range(1, 10): for col in range(1, row): print(col, end=" ") print("*")