Introduction to Computer Programming Chapter 5: Lists and Dictionaries PowerPoint PPT Presentation

presentation player overlay
1 / 36
About This Presentation
Transcript and Presenter's Notes

Title: Introduction to Computer Programming Chapter 5: Lists and Dictionaries


1
Introduction to Computer Programming Chapter 5
Lists and Dictionaries
  • Michael Scherger
  • Department of Computer Science
  • Kent State University

2
Contents
  • Using Lists
  • List Methods
  • Tuples vs Lists
  • Nested Sequences
  • Shared References
  • Using Dictionaries
  • Hangman Game

3
Hangman
  • Example Hangman

4
Introduction
  • Data structures
  • Structures that hold and organize information
  • Sequences
  • Also know as arrays in other languages
  • Store related data types
  • 3 types
  • Strings
  • Lists
  • Tuples
  • Mappings
  • In most languages called hashes or associative
    arrays
  • Dictionaries are the only python mapping container

5
Heros Inventory 3.0 Program
  • Example Heros Inventory 3.0

6
Creating Sequences (Review)
  • Strings
  • Use quotes
  • string1 ""
  • Tuples
  • Use parenthesis
  • Separate multiple items with a comma
  • tuple ()
  • singleton 1, - this is a one element tuple or a
    singleton
  • Lists
  • Use brackets
  • Separate multiple items with a comma
  • list1

7
Using Lists
  • Creating a List
  • anylist
  • chevycars Corvette, Aveo, Malibu,
    Cobalt, Impala, Silverado, Trailblazer,
    Tahoe, Colorado, Suburban
  • Length Function
  • num_cars len( chevycars )
  • in Operator
  • if Corvette in chevycars
  • print Thats a nice car!!!!

8
Using Lists
  • Indexing
  • print chevycars4
  • Slicing
  • print chevycars04
  • Concatenation
  • chevycars SSR, Monte Carlo, Avalanche,
    Equinox, Blazer, Astro, Uplander,
    Venture

9
Using Lists
  • Lists are mutable
  • The elements in a list can change value
  • Change by assignment
  • xmaslist0 Black Corvette with optional
    blonde
  • xmaslist1 Trip to Las Vegas
  • Change by slicing
  • inventory46 Orb of Future Telling

10
Using Lists
  • Deleting a list element
  • del inventory2
  • Deleting a list slice
  • del inventory46

11
List Methods
  • append(value)
  • Adds value to the end of the list
  • sort()
  • Sorts the elements, smallest value first
  • reverse()
  • Reverses the order of a list
  • count(value)
  • Returns the number of occurences of value in the
    list

12
List Methods
  • index(value)
  • Returns the first position number of where value
    occurs
  • insert(i, value)
  • inserts value at position i
  • pop(i)
  • Returns value at position i and removes value
    from the list. Providing the position number is
    optional. Without it, the last element in the
    list is removed.
  • remove(value)
  • Removes the first occurrence of value from the
    list.

13
List Methods
  • Example High Scores Program

14
Tuples vs. Lists
  • Tuples are faster than lists. Because the
    computer knows they will not change, tuples can
    be stored in a way that makes using them faster
    that using lists. For simple programs, this
    speed difference will not matter, but in more
    complex applications, with very large sequences
    of information, it could.
  • Tuples immutability makes them perfect for
    creating constants since they cannot change.
    Using tuples can add a level of safety and
    clarity to your code.
  • Sometimes tuples are required. In some cases
    Python requires immutable values. (Example shown
    later when Dictionaries are discussed.)

15
Nested Sequences
  • Tuples and Lists can be sequences of anything
  • Including other tuples and lists
  • Nested Sequences
  • Sequences inside other sequences
  • A great method to organize complex collections of
    information
  • Common use is a table
  • By convention it is organized by row and column

16
Nested Sequences
17
Nested Sequences
  • Creating nested sequences (examples)
  • nested_1 first, (second, third),
    fourth, fifth, sixth
  • scores (Moe, 1000),(Larry, 1500),(Curly,
    2000)
  • nested_2 (deep,(deeper,(deepest,still
    deepest)))

18
Nested Sequences
  • Accessing Nested Sequences (example)
  • gtgtgt scores (Moe, 1000),(Larry,
    1500),(Curly, 2000)
  • gtgtgt print scores0
  • (Moe, 1000)
  • gtgtgt print scores1
  • (Larry, 1500)
  • gtgtgt print scores2
  • (Curly, 2000)
  • gtgtgt a_score scores2
  • gtgtgt print a_score
  • (Curly, 2000)
  • gtgtgt print a_score0
  • Curly
  • gtgtgt print scores20
  • Curly
  • gtgtgt print scores204

19
Nested Sequences
  • table1 1, 2, 3 , 4, 5, 6
  • table2 ( ( 1, 2 ), ( 3, ), ( 4, 5, 6 ) )
  • print "Values in table1 by row are"
  • for row in table1
  • for item in row
  • print item,
  • print
  • print "\nValues in table2 by row are"
  • for row in table2
  • for item in row
  • print item,
  • Values in table1 by row are
  • 1 2 3
  • 4 5 6
  •  
  • Values in table2 by row are
  • 1 2
  • 3
  • 4 5 6

20
Nested Sequences Example
  • Example High Scores 2.0 Program

21
Shared References
  • Consider the following example
  • s Python
  • The variable s refers to a place in memory where
    the string value Python is stored.
  • A variable refers to a value the same way a
    persons name refers to a person. It would be
    wrong to say that a persons name stores the
    person. Using a persons name you can get to the
    person. Using a variables name you can get to a
    value.
  • For lists, when several variables refer to the
    same mutable value, they share the same reference!

s
Python
22
Shared References
  • gtgtgt mike 'khakis', 'dress shirt', 'jacket'
  • gtgtgt mr_scherger mike
  • gtgtgt honey mike
  • gtgtgt print mike
  • 'khakis', 'dress shirt', 'jacket'
  • gtgtgt print mr_scherger
  • 'khakis', 'dress shirt', 'jacket'
  • gtgtgt print honey
  • 'khakis', 'dress shirt', 'jacket'
  • gtgtgt

23
Shared References
mike
khakis
dress shirt
jacket
mr_scherger
honey
24
Shared References
  • gtgtgt mike 'khakis', 'dress shirt', 'jacket'
  • gtgtgt mr_scherger mike
  • gtgtgt honey mike
  • gtgtgt print mike
  • 'khakis', 'dress shirt', 'jacket'
  • gtgtgt print mr_scherger
  • 'khakis', 'dress shirt', 'jacket'
  • gtgtgt print honey
  • 'khakis', 'dress shirt', 'jacket'
  • gtgtgt
  • gtgtgt
  • gtgtgt honey.append('brown shoes')
  • gtgtgt print mike
  • 'khakis', 'dress shirt', 'jacket', 'brown
    shoes'
  • gtgtgt

25
Dictionaries
  • Programmers love to organize information
  • Lists and tuples organize things into sequences
  • Dictionaries stores information in pairs
  • Similar to an actual dictionary
  • Word and definition
  • Python uses key and value

26
Dictionaries
  • Example Geek Translator

27
Dictionaries
  • Dictionaries
  • Mapping constructs consisting of key-value pairs
  • Referred to as hashes in other languages
  • Unordered collection of references
  • Each value is referenced though key in the pair

28
Dictionaries
  • Curley braces () are used to create a
    dictionary
  • When entering values
  • Use key1value1,
  • Keys must be immutable values such as strings,
    numbers and tuples
  • Values can be of any Python data type

29
Dictionaries
  • create and print an empty dictionary
  • emptyDictionary
  • print "The value of emptyDictionary is",
    emptyDictionary
  • create and print a dictionary with initial
    values
  • grades "John" 87, "Steve" 76, "Laura" 92,
    "Edwin" 89
  • print "\nAll grades", grades
  • access and modify an existing dictionary
  • print "\nSteve's current grade", grades "Steve"
  • grades "Steve" 90
  • print "Steve's new grade", grades "Steve"
  • add to an existing dictionary
  • grades "Michael" 93
  • print "\nDictionary grades after modification"
  • print grades
  • delete entry from dictionary

30
Dictionaries
  • The value of emptyDictionary is
  •  
  • All grades 'Edwin' 89, 'John' 87, 'Steve'
    76, 'Laura' 92
  •  
  • Steve's current grade 76
  • Steve's new grade 90
  •  
  • Dictionary grades after modification
  • 'Edwin' 89, 'Michael' 93, 'John' 87, 'Steve'
    90, 'Laura' 92
  •  
  • Dictionary grades after deletion
  • 'Edwin' 89, 'Michael' 93, 'Steve' 90,
    'Laura' 92

31
Dictionaries
32
Dictionaries
33
Dictionaries
  • gtgtgt dictionary "listKey" 1, 2, 3
  • gtgtgt shallowCopy dictionary.copy() make
    a shallow copy
  • gtgtgt dictionary "listKey" .append( 4 )
  • gtgtgt print dictionary
  • 'listKey' 1, 2, 3, 4
  • gtgtgt print shallowCopy
  • 'listKey' 1, 2, 3, 4
  •  
  • gtgtgt from copy import deepcopy
  • gtgtgt deepCopy deepcopy( dictionary ) make
    a deep copy
  • gtgtgt dictionary "listKey" .append( 5 )
  • gtgtgt print dictionary
  • 'listKey' 1, 2, 3, 4, 5
  • gtgtgt print shallowCopy
  • 'listKey' 1, 2, 3, 4, 5
  • gtgtgt print deepCopy
  • 'listKey' 1, 2, 3, 4

34
Dictionaries
  • monthsDictionary 1 "January", 2
    "February", 3 "March",
  • 4 "April", 5 "May", 6
    "June", 7 "July",
  • 8 "August", 9
    "September", 10 "October",
  • 11 "November", 12
    "December"
  • print "The dictionary items are"
  • print monthsDictionary.items()
  • print "\nThe dictionary keys are"
  • print monthsDictionary.keys()
  • print "\nThe dictionary values are"
  • print monthsDictionary.values()
  • print "\nUsing a for loop to get dictionary
    items"
  • for key in monthsDictionary.keys()
  • print "monthsDictionary", key, " ",
    monthsDictionary key

35
Dictionaries
  • The dictionary items are
  • (1, 'January'), (2, 'February'), (3, 'March'),
    (4, 'April'), (5, 'May'), (6, 'June'), (7,
    'July'), (8, 'August'), (9, 'September'), (10,
    'October'), (11, 'November'), (12, 'December')
  •  
  • The dictionary keys are
  • 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
  •  
  • The dictionary values are
  • 'January', 'February', 'March', 'April', 'May',
    'June', 'July', 'August', 'September', 'October',
    'November', 'December'
  •  
  • Using a for loop to get dictionary items
  • monthsDictionary 1 January
  • monthsDictionary 2 February
  • monthsDictionary 3 March
  • monthsDictionary 4 April
  • monthsDictionary 5 May
  • monthsDictionary 6 June
  • monthsDictionary 7 July
  • monthsDictionary 8 August
  • monthsDictionary 9 September

36
Hangman Program Again
  • Example Hangman
Write a Comment
User Comments (0)
About PowerShow.com