Presentation is loading. Please wait.

Presentation is loading. Please wait.

OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists.

Similar presentations


Presentation on theme: "OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists."— Presentation transcript:

1 OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists

2 OCR Computing GCSE © Hodder Education 2013 Slide 2 Python 9: Tuples and lists Tuples and lists are Python objects that can store sequences of data. They are like arrays in other languages in some ways but Arrays contain data items that are all the same data type; Tuples and lists can contain different data types. The main difference between tuples and lists is that Tuples are immutable; Lists are changeable – you can alter their contents during a program. This is how to make a tuple: movie=('Skyfall','PG','26/10/2012',143) This has produced: Index0123 ContentsSkyfallPG26/10/2012143

3 OCR Computing GCSE © Hodder Education 2013 Slide 3 Python 9: Tuples and lists Indexing a tuple In this example: movie=('Skyfall','PG','26/10/2012',143) movie[0] is ‘Skyfall ‘ movie[1] is ‘PG’ etc. Index0123 ContentsSkyfallPG26/10/2012143

4 OCR Computing GCSE © Hodder Education 2013 Slide 4 Python 9: Tuples and lists We can access elements in the same way as any Python sequence: For example, len(movie) gives us how many elements there are in the tuple.

5 OCR Computing GCSE © Hodder Education 2013 Slide 5 Python 9: Tuples and lists To show that tuples are immutable, try changing the contents of one element with lines such as movie[3]='new item' print(movie[3]) You will get an error like this: movie[3]='new item' TypeError: 'tuple' object does not support item assignment

6 OCR Computing GCSE © Hodder Education 2013 Slide 6 Python 9: Tuples and lists Lists are set up in a similar way but use square brackets: movie=['Skyfall','PG','26/10/2012',143] To access one element print(movie[0]) To output a complete list for item in movie: print(item)

7 OCR Computing GCSE © Hodder Education 2013 Slide 7 Python 9: Tuples and lists More list techniques Make a slice start=int(input('Enter the start position of the slice ')) end=int(input('Enter the end position of the slice ')) print('\nThe slice is ',movie[start:end]) Create a new list booking=['New Theatre','1/1/2013','seat D23','price £5'] Concatenate lists movie+=booking Change an element in a list (you can’t do this in a tuple) movie[index]=new_value

8 OCR Computing GCSE © Hodder Education 2013 Slide 8 Python 9: Tuples and lists List methods Lists come with lots of methods, just like so many Python objects. Removing items from a list Here is our list – named ‘movie’: 'Skyfall','PG','26/10/2012',143,'New Theatre','1/1/2013','seat D23','price £5' The method is invoked like this: movie.remove(item to be removed) So we could say: movie.remove(‘New Theatre’) This method does not create a gap. The list shrinks when an item is removed.

9 OCR Computing GCSE © Hodder Education 2013 Slide 9 Python 9: Tuples and lists Sorting a list This is easy to achieve using the sort method. You can sort in either direction. Here is the output:


Download ppt "OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists."

Similar presentations


Ads by Google