Python For GCSE
Section D Exercises

Task 1

The following program defines a list and then uses an expression to print "Denby Garbo".

names = ["Albert", "Bennet", "Charleston", "Denby", "Elfin",
         "Fishface", "Garbo", "Hubert", "Indigo", "Jelly"]

print(names[3],names[6])

Create similar expressions to output the following,

a) Albert Fishface
b) Hubert Jelly
c) Hubert Indigo Jelly

Task 2

The following program uses a for loop to print the items from the list in the even-numbered positions.

names = ["Albert", "Bennet", "Charleston", "Denby", "Elfin",
         "Fishface", "Garbo", "Hubert", "Indigo", "Jelly"]

for i in range(0, len(names),2):
    print(names[i])

Add for loops to do the following,

a) The elements of the array with odd-numbered subscripts.
b) All of the elements in the array in order (0 to 9).
c) All of the elements in the array in reverse order.
d) Elements with even-numbered subscripts, in reverse order.

Task 3

The following program chooses and prints a random item from the list.

import random

names = ["Albert", "Bennet", "Charleston", "Denby", "Elfin",
         "Fishface", "Garbo", "Hubert", "Indigo", "Jelly"]

n = random.choice(names)
print(n)

We can use pseudorandom numbers to select elements from the array at random. Run the program several times and make sure that you get the sense that the name you see is randomly chosen. Add code to do the following,

a) Select 2 names at random and output them on one line to make a random first name, second name combination.
b) Use a FOR loop to create 10 of these first name, second name combinations.

Task 4

When we want to swap the positions of elements in an array, we need to use a third variable to temporarily store one of those values.

names = ["Albert", "Bennet", "Charleston", "Denby", "Elfin",
         "Fishface", "Garbo", "Hubert", "Indigo", "Jelly"]

tmp = names[0]
names[0] = names[1]
names[1] = tmp

print(names)

Add code to do the following,
a) Use a random number generator to randomly select the two elements you swap.
b) Place a FOR loop around your swapping code so that you swap two random elements 100 times. Output the array now – it should be shuffled.

Task 5

We often use loops to assign user input to elements in an array.

names = ["Albert", "Bennet", "Charleston", "Denby", "Elfin",
         "Fishface", "Garbo", "Hubert", "Indigo", "Jelly"]

scores = [0] * len(names)
for i in range(len(names)):
    scores[i] = int(input("Enter the mark for " + names[i] + ": "))

The second assignment statement makes a list of scores as long as the list of names. It fills the list with 0s.
Now, add code to do the following,

a) Output all of the names with their respective scores using one line for each name/score pair.
b) Fill the scores array with randomly chosen values and then output the names and scores.

To make a random number, include import random at the top of the code window. Then you make a random number with an expression like random.randint(a,b). The letters a and b should be replaced with numbers (integers) defining the range of the random number.

Task 6

Make a list of integers called rolls – and include 7 items, all 0.

Use a loop and random numbers to simulate the rolling of a single die 100 times.

If a 1 is rolled, add 1 to rolls[1]. If a 2 is rolled, add 1 to rolls[2]. If a 3 is rolled, add 1 to rolls[3] and so on.

You will not need to use any IF statements to do this. The die roll on each iteration will store the index that you need to add 1 to.

When all 100 rolls have happened, output elements 1 to 6 of the rolls array in a format like this,

1: 15
2: 17
3: 16
...

Task 7

The following program shows how to create a list of digits from a number.

a = 123456
b = str(a)
c = []

for digit in b:
    c.append(int(digit))

print(c)

Perform the same task but using a list compehension.

Task 8

This task is similar to Task 6.

Write a program to choose 100 random numbers between 0 and 99. Count and display how many numbers are in the range 0-9, 10-19, etc.

You will need a list of 10 items, positions 0 to 9. You will need to use floor division to work out which position in the list. The operator for this is //.

When you output the results, use a for loop. Multiply the counter by 10 to get the first number in the range. Add 9 to get the second number in the range. This will allow you to output 0-9, 10-19 etc.

Task 9

Here are two lists of 50 digits. Assuming that these list contain the digits of two 50 digit numbers, write a program that adds them together and stores the result in a string.

A = [3,7,1,0,7,2,8,7,5,3,
     3,9,0,2,1,0,2,7,9,8,
     7,9,7,9,9,8,2,2,0,8,
     3,7,5,9,0,2,4,6,5,1,
     0,1,3,5,7,4,0,2,5,0]
B = [4,6,3,7,6,9,3,7,6,7,
     7,4,9,0,0,0,9,7,1,2,
     6,4,8,1,2,4,8,9,6,9,
     7,0,0,7,8,0,5,0,4,1,
     7,0,1,8,2,6,0,5,3,8]

You need to use a loop to read one position from each list at a time, starting at the right end. Add the two numbers at that point. The result MOD 10 is the value for that place in the answer. The result – result MOD 10 is carried over and added to the two numbers from the next column.

You can build your answer in a string. Each time you want to add a digit to the string, you need to convert from integer to string. The programming guide tells you how to do this.

Task 10

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

You can use the code from task 7 to turn a number into a list of its digits.

The trick with this kind of problem is not to try to solve all parts of the problem at once. You need to do the following things, one at a time,

Code to turn a number into a list of digits.
Code to find the sum of the 5th powers of the digits.
Code to check if this sum is equal to the original number.

Then change the code so that the numbers you convert to digits are coming from a for loop. Your for loop starts with 10 and does not need to go above 1000000.

Task 11

The following lines are the beginning of a famous song about a house built by a person called Jack.

This is the house that Jack built.
This is the malt that lay in the house that Jack built.

This is the rat that ate the malt
That lay in the house that Jack built.

This is the cat that killed the rat
That ate the malt that lay in the house that Jack built.

This is the dog that worried the cat
That killed the rat that ate the malt
That lay in the house that Jack built.

There are many more words to this song. The song has a repetitive structure. Using arrays, you could work out a really efficient way to do this. For example,

things = ["malt","rat","cat","dog"]
verbs = ["lay","ate","killed","worried"]

Experiment by trying to write a program to produce these lines from the two lists provided. This is quite a challenge and something you need to do in stages. It will need you to use loops inside loops.

When you have done, go to https://en.wikipedia.org/wiki/This_Is_the_House_That_Jack_Built and read the rest of the lyrics. You should be able to get the whole song by simply extending your lists.

The next 'thing' you will need is "cow with the crumpled horn".

You do not need a lot of code to solve this problem, no special techniques. It isn't easy to get your head around the order of the words though.

This is a part of a Python solution with some of the key words obscured.

python code

The variable jack stores the phrase "Jack built".