Wednesday 1 July 2015

12. Data structures: lists (arrays), using Python docs






It also allows you to choose how to format the output.
Lesson 11 activities
Activity 11.1
Lists (arrays)
Complete this table to summarise the list commands.
Things to do with lists
Commands
Create a list

Reference an item in a list

Delete an item in a list

Append an item to the end of a list

Activity 11.2
Lists (arrays)
A list is a data structure that stores a set of elements. Lists are assigned to a name using square brackets.

>>>mylist=["apple","oranges","lemon","pear","lime"]

apples
oranges
lemon
pear
lime
0
1
2
3
4

Each element in a list has an index location. The first element of the list is in position zero (0).

Elements of a list are referenced using their index location (an integer number).
List name[index]

A range of elements can be displayed using
[start index: end index]

Start index is the position to start at (remember that indexing starts at zero)End position is the index AFTER the index required.

Make this list and experiment with the list commands.
>>>mylist=["apple","orange","lemon","pear","lime"]

What does mylist[1] display?
What does mylist[1:3] display?
What does mylist[-1] display?
What command will display just apple?
What command will display lemon and pear?

Make a new list called myfood containing your five favourite foods.
Display the whole list.
Display the food item at index position 3.
Display the food item at index position 0.
Display the food items at index position 1 to 4.
Activity 11.3
Using lists
Make the list that contains the class marks for Amy Jones.

Marks = ['Amy', 'Jones', 'English', 67, 'Maths', 76, 'Computer Science', 96]

The English teacher has entered Amy’s mark incorrectly; it should be 72 not 67. Alter this item in the list.
Add the mark for Physics to the end of the list.  “Physics”, 65
Remove “Maths” and the score 76 from the list.
Write a program to find the average score for the three subjects (English, Computer Science and Physics).
Activity 11.4
Using Python docs help
Select help/python docs then select the Python tutorial and go to 3.1.4 Lists.
Read through the discussion of lists and try out the examples. Make a note of three more facts about lists to share in the next lesson.
Python is a very powerful programming language which is used in universities and commercial organisations. You do not need to know all the details provided in the Python docs but, with practice, you should be able to find information about Python that can be very useful.Lesson 12 activities

No comments:

Post a Comment