Download presentation
Presentation is loading. Please wait.
1
Sample lecture slides
2
Python: basic data types
Numbers (done) Strings Lists Dictionaries
3
Strings
4
Python: basic data types
Numbers Strings Lists Dictionaries
5
Python Lists Recall strings from last time?
Think of those as lists too
6
Python Lists vs. Sets set is faster than list for lookup (hashtable)
in – membership not in – not a member of set(List) - produces a set, no duplicates permitted set is faster than list for lookup (hashtable)
7
Python Lists
8
Python Lists Lists as stacks Lists as queues
Lists as stacks Lists as queues List Comprehensions (advanced topic)
9
Python List as a Queue Method append() to add to the queue
list[0] gives us the head of the queue list = list[1:] deletes the head of the queue from the queue EXAMPLE:
10
Python https://docs.python.org/3/tutorial/introduction.html Numbers
Strings Lists Dictionaries
11
Python Lists Lists as stacks Lists as queues
Lists as stacks Lists as queues List Comprehensions (advanced topic)
12
Python List as a Queue Method append() to add to the queue
EXAMPLE: Method append() to add to the queue list[0] gives us the head of the queue list = list[1:] deletes the head of the queue from the queue Also can use del list[0]
13
Python List as a Queue A faster implementation according to section 5.1.2
14
Python List as a Stack pop() removes from the same end as append()
15
Tuples See use with dictionaries later …
Section 5.3: note instead of square brackets, round brackets are used See use with dictionaries later …
16
Dictionaries (dict) Lists and tuples are indexed by whole numbers (from 0) Dictionaries are indexed by a key (usually a string) and return some value associated with the key Note: use of curly braces Dictionaries are not ordered (like sets) – see next slide Methods keys(), values(), items() Refer to key + value as an item: encoded as a tuple
17
Dictionaries (dict) Example from class…
18
Dictionaries (dict) Python 3.6
Dictionary order preservation depends on the version of Python used … Python 3.6
19
Dictionaries (dict) How to print the contents of a dictionary?
Use a for-loop and method items(): k,v is a tuple
20
Dictionaries (dict) All values are lists
Advantage: simplifies the code Trade-off Values are lists or a string Advantage: simpler-looking dict
21
Dictionaries Works too! Less transparent: relies on a
Python-particuar quirk …
22
Dictionaries (dict) function zip() pairs up elements from two lists into an iterable
23
Dictionaries (dict) function zip() doesn't always work quite the same …
24
For loops and range() range(start,stop,step) returns an iterable that can be used with a for- loop range(stop) start assumed to be 0; step assumed to be 1; range does not include stop
25
For loops and range() Ranges can be indexed like a list
Can use in and not in
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.