CMS 1

Collection data types

# Terminology

• Bug
• Data
• We usually think about lots of data => we need containers of data
• Containers are objects that contain other objects
• Collections/ containers of data => Python sequence types
• 2 major aspects of computing
• computation (arithmetics, branch statements, loops, functions, error handling, …)
• collected data (arrays, vectors, strings, lists, files, databases, …)
• Mathematical set
• Collection of well defined and distinct objects
• Intersection, union, complement, subset, ...
• Alias
• Alternative way of referring to an object

# Python Lists

• Aka. array, vector, ...
• Contiguous memory - not a "linked list"
• Sequence type
• Can be iterated over
• String (immutable): " ", ' ', """ """
• List (mutable): [ ]
• Tuple (immutable): ( )
• Not just an array
• Aware of its length: len()
• Built-in functionality
• Can be sorted
• Optimized for usability, not speed
• In-place methods
• Working with lists
• #!/usr/bin/env python3

l = [3, 5, 1, 9]
ordered = list(range(8))
ordered = list(range(3, 8))
ordered = list(range(12, 2, -2))
print(len(l))
print(ordered.sort())
print(ordered)

• Performance
• Don't worry about performance until it becomes a problem
• But: understand the way lists work
• Many inserts at beginning vs. reverse -> append -> reverse
• iterations = 1000000
l = [7, 9, 3]  # initial content
for i in range(iterations):
l.insert(0, 0)  # slow

l.reverse()
for i in range(iterations):
l.append(0)  # fast
l.reverse()


# Python Tuples

• Tuples are comparable to lists, but immutable
• a_tuple = (3, 54, 2)
t2 = (3, )

• Named tuples allow to name their values
• #!/usr/bin/env python3

from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(11, y=22)  # instantiate with positional args or keywords
print(p[0] + p[1])  # indexable like a plain tuple
print(p.x + p.y)  # fields also accessable by name

• Python 3 Video Tutorial - Tuples

# Multiple Assignment

• Pythonic way to work with "more than one return value"
• Retrieving multiple values
• result = "Joe, 117"
name, score = result.split(",")
score = int(score)

• Returning multiple values
• def read_result(result):
name, score = result.split(",")
return name, int(score)

# ...

result = "Joe, 117"


# Python Dictionaries

• Hash table
• Aka. hash map, associative array, mapping or dictionary
• Collection of key: value pairs
• Like an array, but "keys" are used as indices
• No sort order
• Creating a dict
• d = {"key1": "value1", "key2": "value2"}

• Getting data
• Using keys instead of indices
• dict.keys()
• dict.values()
• dict.items()
• Populating a dict
• d = {}
d["key1"] = "value1"
d["key2"] = "value2"
d["key1"] = "new value"

• Iterating over a dictionary
• #!/usr/bin/env python3

d = {"telephone": "+43(0)316/380-1234",
"email": "contact@uni-graz.at",
"office": "Resowi, E3"}
for key in d:
print(key, "--", d[key])
# alternatives
for key in d.keys():
print(key)
for value in d.values():
print(value)
for key, value in d.items():
print(key, "--", value)

• Iterating over sorted keys
• d = {"telephone": "+43(0)316/380-1234",
"email": "contact@uni-graz.at",
"office": "Resowi, E3"}
for key in sorted(d.keys()):
print(key, "--", d[key])

• Python 3 Video Tutorial - Dictionaries

# Matrices

• Matrix
• 2D array, arranged in rows and columns
• Easiest way is a list of lists (eg. list of rows)
• Iterating over a matrix
• #!/usr/bin/env python3

matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
for row in matrix:
for index, elem in enumerate(row):
row[index] = elem ** 2

• For everything else use numpy
• sudo apt-get install python3-numpy
• np_matrix.py
#!/usr/bin/env python3

import numpy as np
print("np.zeros([3,4])\n", np.zeros([3,4]), sep='')
a1 = np.ones([4,3])
print("np.ones([4,3])\n", a1, sep='')
print("3 * np.ones([4,3])\n", 3 * a1, sep='')
a = np.arange(15).reshape(3, 5)
m = np.matrix(np.arange(6).reshape(2, 3))
print("matrix m\n", m, sep='')
print("array a\n", a, sep='')
print("m * a\n", m * a, sep='')

print("m.T (transposed)\n", m.T, sep='')
print("m.I (inverse)\n", m.I, sep='')

• Tentative NumPy Tutorial
• Python 3 Video Tutorial - 2D Data

1. Read and understand the content of Python Programming - Source Documentation and Comments
2. Work through the Python3 documentation for doctests (from the top of the file until (not including) "How It Works")
3. Read about One-line Docstrings and Multi-line Docstrings directly in the relevant Python Enhancement Proposal (PEP 257)
4. Do the 4th homework

# Summary

• Python lists can...
• Store multiple values
• Be changed in place
• Be extended
• Be used as matrices
• Be iterated over using for loops
• Besides list, Python offers other powerful collection data types
• Dictionaries - mappings of keys and values
• Sets - allow to implement algorithms using mathematical sets