Lets Learn Python and Pygame Aj Andrew Davison

  • Slides: 22
Download presentation
Let's Learn Python and Pygame Aj. Andrew Davison, Co. E, PSU Hat Yai Campus

Let's Learn Python and Pygame Aj. Andrew Davison, Co. E, PSU Hat Yai Campus E-mail: ad@fivedots. coe. psu. ac. th 6. Lists How to store data in lists. A look at tuples and dictionaries. What are mutable and immutable data types? 1

1. Storing Lots of Data Together §Up to now we've stored 1 piece of

1. Storing Lots of Data Together §Up to now we've stored 1 piece 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

2. The List §A list: § family = ["Mum", "Dad", "Brother", "Sister", "Baby"] §This

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" 0 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. 3

Adding Things to a List An empty list A list can store anything friends

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

Printing Parts of a List The ": " is called a slice. friends "like"

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

Using Slice to Copy a List friends "like" 0 "hans solo" 3. 141592 1

Using Slice to Copy a List friends "like" 0 "hans solo" 3. 141592 1 2 2. 71828 3 copy a slice number. Friends 3. 141592 0 2. 71828 1 6

Modifying Box Data 7

Modifying Box Data 7

Other Ways to Add to a List § append() adds 1 item to the

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 8

§insert() adds one item somewhere in the list § uses an index number §

§insert() adds one item somewhere in the list § uses an index number § other boxes move to the right to make room letters "a" "b" 0 1 "z" "d" "e" "f" "g" "h" 2 3 4 5 6 7 "y" "z" "d" "e" "f" "g" 2 3 4 5 6 7 letters "a" "b" 0 1 "h" 8 9

Deleting from a List §Use: remove(), del(), or pop() letters "a" "b" 0 1

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

letters "a" "b" 0 1 "y" "d" "e" "f" "g" 2 3 4 5

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

letters "a" "b" 0 1 "d" "e" "f" "g" "h" 2 3 4 5

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

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

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

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

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

Sorting Lists The words list is changed. 15

Sorting Lists The words list is changed. 15

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

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

3. Tuple: the Unchangeable List Use (. . . ) not [. . .

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

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

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

4. Dictionaries §A dictionary is a list where the index numbers are replaced by

4. Dictionaries §A dictionary is a list where the index numbers are replaced by strings § the strings are called keys; data are called values keys "Bob" "444 -4321" "John" "555 -1234" "Mary" "555 -6789" "Jenny" "867 -5309" values 19

Building a Dictionary "Bob" "444 -4321" "John" "555 -1234" "Mary" "555 -6789" "Jenny" "867

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

Accessing all the Keys / Values Use the keys list inside a for loop

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

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

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