Download presentation
Presentation is loading. Please wait.
Published byClaude Houston Modified over 9 years ago
1
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: 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?
2
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
3
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 0 1 23 4
4
Adding Things to a List 4 An empty list A list can store anything "like" "hans solo" 3.1415922.71828 friends 0 1 23
5
Printing Parts of a List 5 "like" "hans solo" 3.1415922.71828 friends 0 1 23 The ":" is called a slice.
6
Using Slice to Copy a List 6 "like" "hans solo" 3.1415922.71828 friends 0 1 23 copy a slice 3.1415922.71828 numberFriends 0 1
7
Modifying Box Data 7
8
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
9
insert() adds one item somewhere in the list uses an index number other boxes move to the right to make room 9 "a" letters 012345678 "b""y""z""d""e""f""g""h" "a" letters 01234567 "b""z""d""e""f""g""h"
10
Use: remove(), del(), or pop() Deleting from a List 10 "a" letters 012345678 "b""y""z""d""e""f""g""h" "a" letters 01234567 "b""y""d""e""f""g""h" The boxes move left to fill the empty space.
11
11 "a" letters 01234567 "b""y""d""e""f""g""h" "a" letters 0123456 "b""d""e""f""g""h" The boxes move left to fill the empty space.
12
12 "a" letters 0123456 "b""d""e""f""g""h" "a" letters 012345 "b""d""f""g""h" "e" x "a" letters 01234 "b""d""f""g" "h" x
13
Searching a List 13 Useful as a test in if's and while's
14
The counting loop can use a list: Lists and Loops 14
15
Sorting Lists 15 The words list is changed.
16
16 The words list is not changed. A new list is created.
17
3. Tuple: the Unchangeable List 17 Use (...) not [...] Changes not allowed. A tuple is immutable.
18
18 This is allowed since a new tuple is being created. tup is not changed.
19
4. Using Lists to Build a Grid 19
20
A Grid is a List of Lists 20 0123 0 1 2 classMarks 97909287 9279 8588 6137 Read across a row a column : list list 1 list 2 list 3
21
Use row and column position Accessing a Grid 21 rows columns
22
Accessing a Grid (2D Array) 22 0123 0 1 2 classMarks 97909287 92798588 6137 row column row
23
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" "444-4321" "555-1234" "555-6789" "867-5309" values keys
24
Building a Dictionary 24 "John" "Bob" "Mary" "Jenny" "444-4321" "555-1234" "555-6789" "867-5309" A dictionary can store key-value pairs in any order it wants.
25
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
26
Deletion and Checking 26 Big "J" != "j" Can only check for keys
27
Common Dictionary Operations 27
28
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
29
ImmutableMutable integerlist floatdictionary string tuple Which Data Types are Immutable? 29 Immutable/mutable affects the meaning of =: x = 4 y = x x = x + 1
30
Immutable Examples 30 Immutable means that data is never changed, so what is happening to x and s?
31
= in Pictures 31 x = 4 x 4 x = x + 1 5 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
32
= 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
33
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"
34
It matters when there is more than one tag attached to the same data. Why Does this Matter? 34
35
= in Pictures 35 x = 4 y = x x 4 x = x + 1 5 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
36
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
37
Mutable Example 37 Mutable means that data is changed, which is why both names and names2 are altered
38
= in Pictures 38 "jim" "bob" "jill""jane" 0 1 23 names = [... ] names2 = names names names2 names[1] = "andrew" "jim" "andrew" "jill""jane" 0 1 23 names names2 the data is changed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.