270 likes | 289 Views
142-253 Computer Programming. 6. Lists. BSc in Digital Media, PSUIC Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th. How to store data in lists. A look at tuples and dictionaries. What are mutable and immutable data types?. 1. Storing Lots of Data Together.
E N D
142-253 Computer Programming 6. Lists BSc in Digital Media, PSUIC Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th How to store data in lists. A look at tuples and dictionaries. What are mutable and immutable data types?
1. Storing Lots of Data Together • 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
2. The List • A list: • family = ["Mum", "Dad", "Brother", "Sister", "Baby"] • This stores 5 pieces of data in a list called family. "Mum" "Dad" "Brother" "Sister" "Baby" family 0 1 4 2 3 • The boxes in a list are numbered • the numbers are called indexes (or indicies) • New boxes can be added; boxes can be deleted.
Adding Things to a List An empty list A list can store anything "hans solo" "like" 3.141592 2.71828 friends 0 1 2 3
Printing Parts of a List The ":" is called a slice. "hans solo" "like" 3.141592 2.71828 friends 0 1 2 3
Using Slice to Copy a List friends "hans solo" "like" 3.141592 2.71828 0 1 2 3 copy a slice numberFriends 3.141592 2.71828 0 1
Other Ways to Add to a List • append() adds 1 item to the end of the list • seen already • extend() adds many items to the end of the list
insert() adds one item somewhere in the list • uses an index number • other boxes move to the right to make room letters "a" "b" "z" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 letters "a" "b" "y" "z" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 8
Deleting from a List • Use: remove(), del(), or pop() letters "a" "b" "y" "z" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 8 letters "a" "b" "y" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 The boxes move left to fill the empty space.
letters "a" "b" "y" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 letters "a" "b" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 The boxes move left to fill the empty space.
letters "a" "b" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 letters "a" "b" "d" "f" "g" "h" 0 1 2 3 4 5 "e" x letters "a" "b" "d" "f" "g" 0 1 2 3 4 "h" x
Searching a List Useful as a test in if's and while's
Lists and Loops • The counting loop can use a list:
Sorting Lists The words list is changed.
The words list is not changed. A new list is created.
3. Tuple: the Unchangeable List Use (...) not [...] Changes not allowed. A tuple is immutable.
This is allowed since a new tuple is being created. tup is not changed.
list A Grid is a List of Lists list 1 list 2 list 3 : classMarks Read across 97 0 90 92 87 1 92 79 85 88 2 a row 6 13 7 -1 0 1 2 3 a column
Accessing a Grid • Use row and column position rows columns
Accessing a Grid (2D Array) row classMarks 97 0 90 92 87 1 92 79 85 88 2 6 13 7 -1 0 1 2 3 row column
5. Dictionaries • A dictionary is a list where the index numbers are replaced by strings • the strings are called keys; data are called values "444-4321" "Bob" "John" "555-1234" keys "555-6789" "Mary" values "Jenny" "867-5309"
Building a Dictionary "444-4321" "Bob" "John" "555-1234" "555-6789" "Mary" "Jenny" "867-5309" A dictionary can store key-value pairsin any order it wants.
Accessing all the Keys / Values Use the keys list inside a for loop Use a sorted keys list to get values in key order
Deletion and Checking Big "J" != "j" Can only check for keys