Presentation is loading. Please wait.

Presentation is loading. Please wait.

And now for something completely different . . .

Similar presentations


Presentation on theme: "And now for something completely different . . ."— Presentation transcript:

1 And now for something completely different . . .
28-Apr-19 AHD c 2010

2 Part 7 Lists 28-Apr-19 AHD c 2010

3 Python Lists 7.1 Introduction to Lists 7.2 List Operations 28-Apr-19
AHD c 2010

4 Python Lists 7.1 Introduction to Lists 7.2 List Operations 28-Apr-19
AHD c 2010

5 Introduction to Lists Most real-world Python programs contain lists.
Lists allow you to collect objects, so that you can treat them as a group. Lists allow you to collect objects, so that you can treat them as a group. Lists have left-to-right positional ordering, with index capability. Lists have left-to-right positional ordering, with index capability. Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. 28-Apr-19 AHD c 2010

6 Introduction to Lists Most real-world Python programs contain lists.
Lists allow you to collect objects, so that you can treat them as a group. Lists have left-to-right positional ordering, with index capability. Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. 28-Apr-19 AHD c 2010

7 Lists are arrays of object references
Technically, Python lists contain zero or more references to other objects. Lists do not contain the actual objects, just a reference to (address of) them. (Aside for C and C++ programmers: Python lists are essentially arrays of pointers. They are implemented as C arrays inside the Python interpreter. Fetching an item from a Python list is about as fast as indexing a C array, i.e. fast.) 28-Apr-19 AHD c 2010

8 A variable (or simple variable) can take a single value.
A list variable is a special kind of object that can take many values - and they don't need to be all of the same data type. 28-Apr-19 AHD c 2010

9 Say you want to store the exam results of 30 students
Say you want to store the exam results of 30 students. You could create 30 variables: result1 = 67 result2 = 95 result3 = and so on . . . 28-Apr-19 AHD c 2010

10 If you only had a few results to score, this method would be acceptable. But what if you had to store the results of each student in the college, not just one class? 28-Apr-19 AHD c 2010

11 Storing 2000 results in this way would be a very tedious and error-prone process. You would have to have 2000 different variable names and type 2000 assignment statements. 28-Apr-19 AHD c 2010

12 Fortunately, by using a list, we get around this problem . . .
28-Apr-19 AHD c 2010

13 What is a list? First consider a simple variable, the type we have used so far: result1 = 67 28-Apr-19 AHD c 2010

14 After this statement Python reserves an area of memory which is referred to by the name result1 which is big enough to hold the address of the integer object, for example the number 67. Imagine this area of memory as a mail box: result1 . 67 28-Apr-19 AHD c 2010

15 A List Variable Consider a list variable as a line of these boxes, each box is referred to by a number: result 28-Apr-19 AHD c 2010

16 result[0], result[1] and so on... 0 1 2 3 4 5 6 7 result
The line of boxes is known as a list. A list variable is given a name, just like the simple variable, (e.g. result), and each box is referred to by its number: result[0], result[1] and so on... result 28-Apr-19 AHD c 2010

17 Important The number of the box is known as the subscript or index of the list element (box). 28-Apr-19 AHD c 2010

18 Note: In Python, the index of a list always starts at 0.
Important Note: In Python, the index of a list always starts at 0. 28-Apr-19 AHD c 2010

19 Elements of a list may be of any data type
The individual elements of a Python list do not need to be all of the same data type. A Python list can contain a mixture of strings, integers and float objects, indeed a mixture of any type of object. 28-Apr-19 AHD c 2010

20 Assigning values to lists
Note: for simplicity, the diagram here shows the integer values in each element, but in reality, the elements of a list store references to the data values. result = [0,0,0,0,0,0,0,0] result[0] = 75 result[1] = 90 result[4] = 72 75 90 72 28-Apr-19 AHD c 2010

21 How big are the list elements?
An element is one of the boxes making up the list. Each element holds the address of the object to which it refers. Hence, each element has size in bytes sufficient to hold an address. 28-Apr-19 AHD c 2010

22 A List Variable Consider an list variable as a line of adjacent memory locations. The elements are all the same size. result 28-Apr-19 AHD c 2010

23 Python Lists 7.1 Introduction to Lists 7.2 List Operations 28-Apr-19
AHD c 2010

24 List operations The best way to understand lists is to see them in action... The first example program creates a list of integers, and accesses each item in the list (each element) by its index. result = [0,0,0,0,0,0,0,0] print result result[0] = 75 result[1] = 90 result[4] = 72 print result[0] print result[1] 07-01.py 28-Apr-19 AHD c 2010

25 An empty list This example program creates an empty list and shows the result of a print on an empty list. list1 = [] print list1 07-02.py 28-Apr-19 AHD c 2010

26 Appending to an empty list
This example program creates an empty list and shows the result of a print on an empty list, appends an item, and prints again. list1 = [] print list1 list1.append(67) print list1[0] list1.append("spam") print list1[1] 07-03.py 28-Apr-19 AHD c 2010

27 A list of lists This example program creates a list of lists and prints out the list and its elements. list1 = [1,2,3] print list1 list2 = [4,5,6] print list2 list3=[list1,list2] print list3 print list3[0] print list3[1] 07-04.py 28-Apr-19 AHD c 2010

28 Accessing the last element in a list
This example program creates a list and prints out the last element by using index number -1. list1 = [1,2,3,6,7,8,9,10] print list1 print list1[0] print list1[1] print list1[-1] print list1[-2] 07-05.py 28-Apr-19 AHD c 2010

29 Deleting items from a list
This example program creates a list and deletes selected elements of the list. list1 = [1,2,3,4,5,6,7,8,9,10] print list1 del list1[0] del list1[-1] 07-06.py 28-Apr-19 AHD c 2010

30 Repeating (multiplying) lists using *
print list1 print list1 * 3 list1 = list1 * 2 07-07.py The output: [1, 2, 3] [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3] 28-Apr-19 AHD c 2010

31 Lists can be joined together using the + symbol
Joining lists together using the + symbol is a process known as concatenation. list1 = [1,2,3] print list1 list2 = [4,5,6] print list2 list1 = list1 + list2 list1 = list1 + list1 [1, 2, 3] [4, 5, 6] [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] 07-08.py 28-Apr-19 AHD c 2010

32 Indexing lists using the [] operator
Any element of a list can be accessed by indexing. list1 = ["Anne", "Dawson", 666] print list1[0], list1[2] prints Anne 666 07-09.py 28-Apr-19 AHD c 2010

33 Slicing lists using the [] operator
Any sublist of a list can be obtained by using the [] operator. list1 = [2,4,6,8,10,12,14,16,18,20] print list1[0:1],list1[5:7] prints [2] [12, 14] 07-10.py 28-Apr-19 AHD c 2010

34 Finding the length of a list using len
The length of any list can be determined using the len method. list1 = ["Anne","was",'here','testing',1,2,3] list2 = [1,2,3,4] list3 = [] print len(list1), print len(list2), print len(list3) prints 07-11.py 28-Apr-19 AHD c 2010

35 List iteration The following example shows that you can iterate over lists in loops using for statements. list = [1,2,3,"Spam",4,5] for i in list: print i, 07-12.py The output: >>> 1 2 3 Spam 4 5 28-Apr-19 AHD c 2010

36 List membership The following example shows that you can test lists for membership with the in expression operator: list = [1,2,3,"Spam",4,5] print "Spam" in list 07-13.py The output: >>> True 28-Apr-19 AHD c 2010

37 List Methods Python has a number of built-in list methods which enable lists to be sorted, reversed, appended, etc. For a full list of available methods, refer to Python's web page: 07-14.py 28-Apr-19 AHD c 2010

38 List Textbook References
28-Apr-19 AHD c 2010

39 This presentation uses the following program files:
07-01.py 07-02.py 07-03.py 07-04.py 07-05.py 07-06.py 07-07.py 07-08.py 07-09.py 07-10.py 07-11.py 07-12.py 07-13.py 07-14.py 28-Apr-19 AHD c 2010

40 End of Python_Lists.ppt
28-Apr-19 AHD c 2010

41 Last updated: Friday 29th May 2009, 14:38 PT, AHD
28-Apr-19 AHD c 2010


Download ppt "And now for something completely different . . ."

Similar presentations


Ads by Google