Final InOne Lecture 9, 10 and 11 Data Structures
Final InOne Lecture 9, 10 and 11 Data Structures
STRUCTURES
Data Structures In Python
KEY CONCEPTS
Key Concepts that we are going to
see here
List -- VarName = []
Dictionary – Dict --- VarName = {}
Tuple -- VarName = ()
WHAT ARE DATA
STRUCTURES?
In computer science, a data
structure is a particular way of
organizing data in a computer so that
it can be used efficiently.
LISTS
A List is a kind of Collection
>>> print( [] )
[]
GO THROUGH THE LIST
5
for i in [5, 4, 3, 2, 1] : 4
print(i) 3
2
print('Blastoff!‘)
1
Blastof
f!
LIST TRAVERSAL
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
LIST OPERATIONS
>>> a_list = ["a", "b", "c", "d", "e",
"f"]
>>> a_list[1:3]
['b', 'c']
>>> a_list[:4]
['a', 'b', 'c', 'd']
LOOKING INSIDE LISTS
Just like strings, we can get at any single
element in a list using an index specified
in square brackets
‘Homer ‘Marge
‘Bart' ‘Lisa’
’ ’
0 1 2 3
UPDATING LISTS
friends_cast = [‘Chandler’, ‘Phoebe’, ‘Joey’]
print(friends_cast)
Output:
['Monica', 'Phoebe', 'Joey']
MUTABILITY: LIST VS
STRINGS
Lists are mutable
Note: In Python, a data type is mutable if its values can be changed, updated, or
modified after the data type has been created. In other words, once an object of the
mutable data type is initialized, you can update its content without creating a new object
STRING
>>> my_string = "TEST"
>>> my_string[2] = "X"
B/C String is immutable
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
my_string[2] = 'x'
TypeError: 'str' object does not support item assignment
DEEP COPY & SHALLOW
COPY
>>> game_of_thrones = [‘Targaryen’, ‘Stark’]
single “variable”
A collection is nice because we can carry many
>>> t1 = [1, 2]
>>> t1.append(3)
>>> print(t1)
[1, 2, 3]
LIST INSERT METHOD
Syntax: list.insert(position, element)
>>> k = [1, 2]
>>> print(k)
[1, 2, 4]
LIST INSERT METHOD
>>> l = [1, 2]
>>> print(l)
>>> l = [1, 2]
>>> print(l)
[1, 4, 2]
LIST METHODS
>>> t1 = [1, 2]
>>> t2 = t1.append(3)
>>> print(t1)
[1,2,3]
>>> print(t2)
None #append work for the list itself not to uppend and assign at the same
time
‘is’ OPERATOR
Given two strings
a = ‘banana’
b = ‘banana’
key-value pair
DICTIONARY
Lists index their entries based on the position
in the list.
Dictionaries are like bags - no order
>>> eng2sp = {}
dict()
>>> print(eng2sp)
{}
DICTIONARY
>>> print(eng2sp)
{'one': 'uno'}
DICTIONARY
>>> print(eng2sp)
>>> item_price = {}
>>> item_price[‘milk’] = 10
>>> print(item_price[‘milk’])
10
DICTIONARY
>>> item_price = {}
>>> item_price[‘milk’] = 10
>>> print(item_price[‘sugar’])
KeyError: ‘sugar’
DICTIONARY
>>> number_of_days[‘February’]
DEBUGGING
Key Error
KeyError: 3
ITERATION ON
DICTIONARIES
Even though dictionaries are not stored in
order, we can write a for loop that goes
through all the entries in a dictionary -
actually it goes through all of the keys in the
dictionary and looks up the values
ITERATION
eng2sp = {'one': 'uno', 'two': 'dos',
'three': 'tres'}
for k in eng2sp:
print(k)
ITERATION: VALUES
eng2sp = {'one': 'uno', 'two': 'dos',
'three': 'tres'}
for k in eng2sp:
print(eng2sp[k])
‘in’ OPERATOR
>>> eng2sp = {'one': 'uno', 'two': 'dos', 'three':
'tres'}
>>> counts.values()
>>> counts.items()
DATA
STRUCTURE
From Lecture 11
REVIEW: DICTIONARY
key-value pair
REVIEW
>>> print(eng2sp)
{'one': 'uno'}
TUPLES
TUPLES
a sequence of values
>>> t1 = ()
>>> t2==(1,)
(1)
TUPLE ASSIGNMENT
a, b = 2, 3
RETURN VALUES
def divmod(a,b):
return a//b, a%b
DICTIONARIES & TUPLES
>>> t = d.items()
[(‘b’,18),(‘a’,12)]
LOOPING