This is an archived version of the course. Please find the latest version of the course on the main webpage.

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

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

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
*
**
***
****
*****
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=" ")
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
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 *
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 *
Sample solution:

One possible solution:

for row in range(1, 10):
    for col in range(1, row):
        print(col, end=" ")
    print("*")