03 Python Data Structures
03 Python Data Structures
- Data Structures -
EL Moukhtar ZEMMOURI
ENSAM – Meknès
• Lists list
• Byte Arrays bytearray
• Set types
o Sets set
o Frozen sets frozenset
• Mappings
o Dictionaries dict
E. Zemmouri, ENSAM - Meknès 3
Sequences
• Represent finite ordered sets indexed by non-negative numbers
• Built-in function len() returns the number of items
• A sequence a is indexed by 0 to len(a)-1
o Item i of sequence a is accessed by a[i]
• Mutable sequences
o Object can be changed after creation
• Common operations :
o x in s True if an item of s is equal to x, else False
o x not in s False if an item of s is equal to x, else True
o s + t the concatenation of s and t
o s * n or n * s equivalent to adding s to itself n times
o min(s) smallest item of s
o max(s) largest item of s
o sum(s)
Set types
Strings
Strings
• Declaration, indexing, slicing
>>> a[0]
'H' >>> a[-1] >>> b[1:6]
>>> b[0] '!' 'Hello'
'"' >>> a[-len(a)] >>> a[6:]
>>> a[11] 'H' 'world!'
'!' >>> a[0] >>> a[:5]
>>> a[12] 'H' 'Hello'
IndexError: string index out >>> a[len(a)-1] >>> b[-6:]
of range '!' 'I said'
Quick exercise
• Use the method find to print out indices of all occurrences of a
substring sub in a string s.
Lists
• A list is a collection of items
o Can contain items of different types
Quick exercise
• A matrix (2d-array) can be represented in Python as a list of lists.
o Create a matrix of zeros with size (n, m)
• List comprehensions
>>> n = 3
>>> m = 4
>>> M = [[0 for j in range(m)] for i in range(n)]
>>> M
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Exercise
• A matrix (2d-array) can be represented in Python as a list of lists.
• Define a function that returns a random matrix with size (n, m) and values
between 0.0 and 1.0
o Hint : use function of random module
Dictionaries
• A dictionary is an unordered collection of (key, value) pairs.
o A dictionary stores data as a key:value pair >>> list(d)
['casa', 'fes', 'ifrane']
o Dictionaries are indexed by keys >>> 'fes' in d
True
>>> d = {"fes":60, "rabat":120, "ifrane":55}
>>> d >>> for k in d.keys():
... print(k, d[k])
{'rabat': 120, 'fes': 60, 'ifrane': 55}
>>> type(d) ...
casa 300
<class 'dict’>
>>> d['rabat'] fes 60
ifrane 55
120
>>> for k, v in d.items():
>>> d["casa"] = 300
... print(k, v)
>>> d
...
{'rabat': 120, 'casa': 300, 'fes': 60, 'ifrane': 55}
>>> d.pop('rabat') casa 300
fes 60
120
>>> d ifrane 55
>>> {x:x**2 for x in (2, 4, 8)}
{'casa': 300, 'fes': 60, 'ifrane': 55}
{8: 64, 2: 4, 4: 16}
E. Zemmouri, ENSAM - Meknès 20
Tuples
Tuples
>>> t = (100, 200, 'Hello', 12.5)
• A tuple is an immutable list >>> type(t)
<class 'tuple'>
>>> t
• tuples are usually used :
(100, 200, 'Hello', 12.5)
o to pass arguments into functions >>> t[0]
100
o to return values from functions >>> t[len(t)-1]
12.5
o as keys in dictionaries. >>> t[1] = 10
TypeError: 'tuple' object does not support item
assignment
>>> a, b, c, d = t # tuple unpacking
>>> c
'Hello'
>>> L = list(t)
>>> L
[100, 200, 'Hello', 12.5]
>>> pts = [(x, x*x) for x in range(5)]
>>> pts
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
E. Zemmouri, ENSAM - Meknès 22
E. Zemmouri, ENSAM - Meknès 23