Presentation is loading. Please wait.

Presentation is loading. Please wait.

6. Lists Let's Learn Python and Pygame

Similar presentations


Presentation on theme: "6. Lists Let's Learn Python and Pygame"— Presentation transcript:

1 6. Lists Let's Learn Python and Pygame
Aj. Andrew Davison, CoE, PSU Hat Yai Campus 6. Lists How to store data in lists. A look at tuples and dictionaries. What are mutable and immutable data types?

2 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

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

4 Adding Things to a List An empty list A list can store anything
friends "like" "hans solo" 1 2 3

5 Printing Parts of a List
The ":" is called a slice. friends "like" "hans solo" 1 2 3

6 Using Slice to Copy a List
friends "like" "hans solo" 1 2 3 copy a slice numberFriends 1

7 Modifying Box Data

8 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

9 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" 1 2 3 4 5 6 7 letters "a" "b" "y" "z" "d" "e" "f" "g" "h" 1 2 3 4 5 6 7 8

10 Deleting from a List Use: remove(), del(), or pop() letters letters
"b" "y" "z" "d" "e" "f" "g" "h" 1 2 3 4 5 6 7 8 letters "a" "b" "y" "d" "e" "f" "g" "h" 1 2 3 4 5 6 7 The boxes move left to fill the empty space.

11 letters letters The boxes move left to fill the empty space. 1 2 3 4 5
"d" "e" "f" "g" "h" 1 2 3 4 5 6 7 letters "a" "b" "d" "e" "f" "g" "h" 1 2 3 4 5 6 The boxes move left to fill the empty space.

12 letters letters letters 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 "a" "b" "d" "e"
"f" "g" "h" 1 2 3 4 5 6 letters "a" "b" "d" "f" "g" "h" 1 2 3 4 5 "e" x letters "a" "b" "d" "f" "g" 1 2 3 4 "h" x

13 Searching a List Useful as a test in if's and while's

14 Lists and Loops The counting loop can use a list:

15 Sorting Lists The words list is changed.

16 The words list is not changed. A new list is created.

17 3. Tuple: the Unchangeable List
Use (...) not [...] Changes not allowed. A tuple is immutable.

18 This is allowed since a new tuple is being created. tup is not changed.

19 4. Dictionaries A dictionary is a list where the index numbers are replaced by strings the strings are called keys; data are called values "Bob" " " "John" " " keys "Mary" " " values "Jenny" " "

20 Building a Dictionary "Bob" "444-4321" "John" "555-1234" "Mary"
" " "Jenny" " " A dictionary can store key-value pairs in any order it wants.

21 Accessing all the Keys / Values
Use the keys list inside a for loop Use a sorted keys list to get values in key order

22 Deletion and Checking Big "J" != "j" Can only check for keys


Download ppt "6. Lists Let's Learn Python and Pygame"

Similar presentations


Ads by Google