Python For GCSE
Practising For Loops

Introduction

The idea behind these exercises is to give you practice in getting your for loops to start and end with the numbers that you intend. You need to make sure that your solutions give the numbers that you expect. If not, make the changes you need until you have the exact output that was expected.

Example 1

for i in range(1,11):
    print(i)

Copy and test the above program code. It prints the numbers from 1 to 10. The part the tells it to do this is range(1,11). This means, count from 1 and stop before you reach 11.

Task 1

Change the example program so that you have a program that counts from 3 to 9, printing out each of the numbers. Just change the range part of the example program, based on what you read in the task.

Task 2

Change the example program to count from 20 to 25. Make sure that all of the correct numbers are included.

Example 2

for i in range(2,21,2):
    print(i)

The above program counts from 2. It stops counting before it reaches 21. It goes up in twos. The third number in the range statement determines what is added to i at the end of each repetition.

Task 3

Change the example program to count from 1 to 21, printing only the odd numbers (go up in twos).

Task 4

Change the example program to count from 10 to 100, going up in 10s. Check that your program actually counts all of the right numbers.

Task 5

Change the example program to count from 100 to 1000, going up in 100s. Check that your program actually counts all of the right numbers.

Task 6

Change the example program to count from 10 to 1. To count backwards, make the third number in the range statement negative. Check that your program actually counts all of the right numbers.

Task 7

Change the example program to count from 100 to 0, going down in 10s.

Example 3

start = int(input("Where should we start counting? "))
for i in range(start, start+10):
    print(i)

When you write your range statements, you can use arithmetic expressions and integer variables to make the number of repetitions depend on the user's input. This program counts 10 numbers, starting with the number that the user enters.

Task 8

Adapt this program to make it so that, instead of 10 repetitions, the loop repeats as many times as the user wants. Add an extra input statement at the start of the program.