7. Lists 1 Lets Learn Saenthong School, January February 2016 Teacher: Aj. Andrew Davison, CoE, PSU...

download 7. Lists 1 Lets Learn Saenthong School, January  February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus

If you can't read please download the document

description

 A list:  family = ["Mum", "Dad", "Brother", "Sister", "Baby"]  This stores 5 pieces of data in a list called family. 2. The List 3 "Mum""Dad""Brother""Sister""Baby"  The boxes in a list are numbered  the numbers are called indexes (or indicies)  New boxes can be added; boxes can be deleted. family

Transcript of 7. Lists 1 Lets Learn Saenthong School, January February 2016 Teacher: Aj. Andrew Davison, CoE, PSU...

7. Lists 1 Lets Learn Saenthong School, January February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus How to store data in lists. A look at tuples and dictionaries. What are mutable and immutable data types? Up to now we've stored 1piece of data in 1 variable: x = 3 With bigger programs, it's useful if we can store lots of data inside 1 variable we do this using a Python data structure I'll look at 3 data structures: lists, tuples, dictionaries 1. Storing Lots of Data Together 2 A list: family = ["Mum", "Dad", "Brother", "Sister", "Baby"] This stores 5 pieces of data in a list called family. 2. The List 3 "Mum""Dad""Brother""Sister""Baby" The boxes in a list are numbered the numbers are called indexes (or indicies) New boxes can be added; boxes can be deleted. family Adding Things to a List 4 An empty list A list can store anything "like" "hans solo" friends Printing Parts of a List 5 "like" "hans solo" friends The ":" is called a slice. Using Slice to Copy a List 6 "like" "hans solo" friends copy a slice numberFriends 0 1 Modifying Box Data 7 append() adds 1 item to the end of the list seen already extend() adds many items to the end of the list Other Ways to Add to a List 8 insert() adds one item somewhere in the list uses an index number other boxes move to the right to make room 9 "a" letters "b""y""z""d""e""f""g""h" "a" letters "b""z""d""e""f""g""h" Use: remove(), del(), or pop() Deleting from a List 10 "a" letters "b""y""z""d""e""f""g""h" "a" letters "b""y""d""e""f""g""h" The boxes move left to fill the empty space. 11 "a" letters "b""y""d""e""f""g""h" "a" letters "b""d""e""f""g""h" The boxes move left to fill the empty space. 12 "a" letters "b""d""e""f""g""h" "a" letters "b""d""f""g""h" "e" x "a" letters "b""d""f""g" "h" x Searching a List 13 Useful as a test in if's and while's The counting loop can use a list: Lists and Loops 14 Sorting Lists 15 The words list is changed. 16 The words list is not changed. A new list is created. 3. Tuple: the Unchangeable List 17 Use (...) not [...] Changes not allowed. A tuple is immutable. 18 This is allowed since a new tuple is being created. tup is not changed. 4. Using Lists to Build a Grid 19 A Grid is a List of Lists classMarks Read across a row a column : list list 1 list 2 list 3 Use row and column position Accessing a Grid 21 rows columns Accessing a Grid (2D Array) classMarks row column row A dictionary is a list where the index numbers are replaced by strings the strings are called keys; data are called values 5. Dictionaries 23 "John" "Bob" "Mary" "Jenny" " " " " " " " " values keys Building a Dictionary 24 "John" "Bob" "Mary" "Jenny" " " " " " " " " A dictionary can store key-value pairs in any order it wants. Accessing all the Keys / Values 25 Use the keys list inside a for loop Use a sorted keys list to get values in key order Deletion and Checking 26 Big "J" != "j" Can only check for keys Common Dictionary Operations 27 Mutable == data can change Immutable == data cannot change Data Types == the different kinds of Python data integer, float, string, boolean, list, tuple, dictionary, etc. e.g. x = 1 y = 1.23z = "hello" r = True s = ["dog", "cat"] t = ("dog, "cat") phoneNos from the last few slides 6. Mutable and Immutable Data Types 28 ImmutableMutable integerlist floatdictionary string tuple Which Data Types are Immutable? 29 Immutable/mutable affects the meaning of =: x = 4 y = x x = x + 1 Immutable Examples 30 Immutable means that data is never changed, so what is happening to x and s? = in Pictures 31 x = 4 x 4 x = x the data is copied and 1 added to the copy 1 x the x tag and data are separated 2 4 the x tag is attached to the new data 3 x 5 4 the 4 data is never changed. It is immutable = in Pictures 32 s = "Andrew" s "Andrew" s = s+" Davison" "Andrew Davison" the data is copied and " Davison" added to the copy 1 s the x tag and data are separated 2 the x tag is attached to the new data 3 s "Andrew" "Andrew Davison" the "Andrew" data is never changed. It is immutable Immutable data never changes. New immutable data is a copy of old data with some parts changed. A tag "changes" by being linked to new immutable data. x = "foo" x = x + " bar" 33 "foo" x "foo bar" It matters when there is more than one tag attached to the same data. Why Does this Matter? 34 = in Pictures 35 x = 4 y = x x 4 x = x the data is copied and 1 added to the copy 1 x 2 the x tag is attached to the new data 3 x 5 the 4 data is never changed. So y is still 4 y the x tag and data are separated y 4 Another reason why immutable / mutable matters is for understanding how arguments are passed to and changed by functions see the next set of slides Important for Functions 36 Mutable Example 37 Mutable means that data is changed, which is why both names and names2 are altered = in Pictures 38 "jim" "bob" "jill""jane" names = [... ] names2 = names names names2 names[1] = "andrew" "jim" "andrew" "jill""jane" names names2 the data is changed