Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

python lists

chapter six
Background List Functions
Python provides us with structures that enable us to store related data Sample functions are listed below, there are
together. Imagine a situation where you want to store the names of many more. Far too many to document here.
several people: Refer to ‘Byte of Python’ or the Python
• Previously we would have used variables: documentation on the Internet for a more
complete list.
Name1  =  ‘Laura’
Name2  =  ‘Tom’ MyList.append()
Name3  =  ‘Ryan’ Adds an item to the end of the list.
Name4  =  ‘Alice’
MyList.reverse()
Name5  =  ‘Callum’
Reverses the order of the items in the list.
• Lists allow us to simply this to: MyList.pop(0)
Name  =  [‘Laura’,‘Tom’,‘Ryan’,‘Alice’,‘Callum’] Removes and returns the value stored at the
given list position.
• We can access a particular item in the list as follows: len(MyList)
NextName  =  Name[3] Returns the length of a given list.
• This would return ‘Alice’

One of the major benefits of Lists over individual variables as they enable
us to iterate over the list so we can look at each value in turn: Formatting
for  each  in  range(0,6):
Python provides powerful formatting options
     print(Name[each])
that enable us to format text and other data
types effectively:
List of Lists ‘{0:<5}’.format(‘Bob’)
Would format the text string ‘Bob’ so that it was
Because a list can contain values of any data type we can create a list in left-aligned and padded with two spaces, e.g.
which each item of that list is another list. This is very useful as it allows
us to model a grid structure in a program: ‘Bob    ‘
You can also center and right-align:
1 2 3 4
‘{0:^5}’.format(‘Bob’)  gives
5 6 7 8 ‘  Bob  ‘
‘{0:>5}’.format(‘Bob’)  gives
9 10 11 12
‘    Bob‘
13 14 15 16 You can format multiple items together:
‘{0:<5}  and  {1:<5}’.format
This would be represented as list of lists:
(‘Bob’,‘Jane’)
Grid  =  [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
This would give:
• We can access a particular item in the list of lists as follows: ‘Bob    and  Jane    ‘
NextNum  =  Grid[1][3] This formatting is especially useful if you want to
• This would return ‘8’ create a table structure....
It can also be used to replace traditional string
Let’s try some exercises.... concatenation methods:
MyText  =Name1  +  ‘  and  ‘  +  Name2

Would become:
MyText  =  ‘{0}  and  {1}’.format(Name1,  
Name2)
python chapter six
Exercises
1. Write a program that reads 6 names into a list. The program must display the names in the same order that they
were entered and then in reverse order.
2. We want to simulate throwing a die 30 times and record the scores. If we did this ‘manually’ we would end up with
a tally chart:

Number of 1s ||||
Number of 2s |||
Number of 3s |||| |||
Number of 4s |||| |
Number of 5s |||| ||
Number of 6s ||
If we use a computer to keep count of how many times each number was thrown, we could use a list instead of
the tally chart.
Write a program to simulate the throwing of a die 30 times. The results of the simulation should be printed as a
table of scores (1 to 6) and frequencies.
3. We wish to select six random numbers between 1 and 49 with the condition that all the numbers are different.
One possible strategy, or algorithm, is:
Initialise a list by using a for loop to store the values 1 to 49
Repeatedly select a random element from the list until a non-zero value is selected
Display this value
Set that element to zero
Repeat the above three steps until six numbers have been selected
4. We can declare two lists, Student and DoB, to store the name of Students and their dates of birth. For example, if
Fred is born on 22/12/84 then we could store ‘Fred’ in Student[0] and ’22/12/84 in DoB[0].
Write a program that stores 5 students’ names and dates of birth and then searches for a particular student and
displays that student’s date of birth and current age. Display a suitable message if the student’s details cannot be
found.
5. Write a program which asks the user for the subjects done in each period for each day and then prints out the
timetable with suitable headings.
6. Write a program that stores the names of ten countries in column1 and their capitals in column2. The program
should then pick a random country and ask the user for the capital. Display an appropriate message to the user to
show whether they are right or wrong.
7. Expand the above to ask the user 5 questions and give a score of how many they got right out of 5.
8. Store in a list a set of 5 place names, and in a list of lists the distances between the places. Ensure that the order of
the places is the same in both lists. When the names of 2 places are input, the distance between them is displayed.
If they are not both in the table, a suitable message should be displayed.
9. A Latin Square of order n is an n x n grid which contains the numbers 1,2,3,...,n such that each row and column
contains each number exactly once. For example the following diagram shows a Latin Square of order 4. You can
see that each row can be obtained from the previous one by shifting the elements one place to the left.

1 2 3 4

2 3 4 1

3 4 1 2
4 1 2 3
Design an write a program to store such a Latin Square of a size given by the user. The program should also
display the Latin Square.

You might also like