Lab 2 - Data Structures. Functions. Modules

Functions

Functions are used to group code statements in order to perform a task. Similar to most programming languages, a function can take one or more inputs, perform the calculations, and then return the result. Once defined, a function can be used many times in a program.

def function_name(params):
# do something
return res

def add(a, b):
s = a + b
return s

a = 10
b = 20
print (s)

T1 (1p) Having a list of numbers, write a function to calculate the sum of all elements in the list. Tip: List given as input, using a for loop

import math
list1 = [1, 3, 4, 5, 8]
list3 = [1, 3, -4, 5, -8, -12]
weights1 = [0.2, 0.3, 0.6, 0.1, 0.5]
list2 = [1, 3, 4, 5, 7, 8]

# function to compute the arithmetic mean of a list

def my_mean(v):
s_elem = sum(v)
n_elem = len(v)
return s_elem / n_elem

# function to compute the weighted arithmetic mean of a list

def my_pmean(v, p):
s_pond = sum(p)
i = 0
s_lp = 0
while i < len(v):
s_lp += v[i] * p[i]
i += 1
return s_lp / s_pond

# function to compute the variance and standard deviation of a list

def my_stdvar(v):
s = 0
m = my_mean(v)
n = len(v)
for elem in v:
s += (elem - m)**2
return s / n, math.sqrt(s / n)

# function to compute the L2-norm normalization for list

def l2norm(v):
new_list = []
s = 0
# compute the L2 norm
for elem in v:
s += elem**2
s = math.sqrt(s)

# list normalization using the L2 norm
for elem in v:
new_elem = elem / s
new_list.append(new_elem)

# TODO: same task with list comprehension

return new_list


T2.1 (1p) Use the functions to compute the arithmetic mean, weighted arithmetic mean, variance, standard deviation and L2-norm normalization of the list

T2.2 (2p) Implement the functions using list comprehension, without using for loops

Modules

Libraries are collections of functions and resources that can be used by multiple programs, having a well-defined functionality and interface. In Python, libraries are implemented as modules, allowing to logically organize related code. A module is defined in a Python source file and can be imported in other source files using the import statement.

module1.py

def hello_name(name):
print("Hello " + name)

main.py

# import a single resource from a module
from module1 import hello_name
hello_name("Alex")

# import all resources from a module
from module1 import *
hello_name("Alex")

# import the module
import module1
module1.hello_name("Alex")

# import the module using an alias
import module1 as m
m.hello_name("Alex")

T3 (2p) Create two Python files (.py) in the same project folder, as shown in the example. Run the program main.py and check the output in the console

Data Structures

Data structures are used to organize data so that it can be used effectively. Each data structure has its own set of possible and recommended use cases in dealing with data.

Lists

List is one of the most frequently used and very versatile datatype used in Python. As opposed to more traditional arrays, used to store elements having the same data-type, Python lists do not have such constrain. Lists are discussed in the previous lab (Lab 1 - Introduction to Python), showing how to create, read, update and delete (CRUD) contained elements using basic operators. In Python, the List type also contains some useful methods: append, extend, insert, remove, pop, clear, index, count, sort, reverse, copy.

my_list = [1, 2, 3, 4, 5]
print(my_list)

# add an element to the list
my_list.append(6)
print(my_list)

# combine lists
my_other_list = [7, 8, 9, 10]
my_list.extend(my_other_list)
print(my_list)

# sort list (descending)
my_list.sort(reverse=True)
print(my_list)

# check if an element is found in the list
# (python specific, normally used with sets or dictionaries)
print(1 in my_list)
print(10 in my_list)

Sets

In Python, mathematical sets are implemented using set(). A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

Create, access, check membership

# declare a set
s = {"apple", "banana", "cherry"}
print(s)

# access items
for x in s:
print(x)

# check if "banana" is present in the set
print("banana" in s)

Modify

# declare an empty set
s=set()
s=set([])

s.update(["orange", "mango", "grapes"])
print(s)

print(s)
# get the length of a set
print(len(s))

# remove item
s.remove("orange")
print(s)

Operations (set theory)

# declare a set using a list:
s1 = set(['a', 'b', 'd'])
s2 = set(['a', 'b', 'c', 'e'])
s3 = set(['a', 'd'])
print("s1:", s1)
print("s2:", s2)
print("s3:", s3)

# verify if a set is a subset of another set:
print("s1 subset of s3:")
print(s1.issubset(s3))

print("s3 subset of s1:")
print(s3.issubset(s1))

# get the union between 2 sets:
print("union s1, s3:")
print(s1.union(s3))

# get the intersection between 2 sets:
print("intersection s1, s2:")
print(s1.intersection(s2))

# get the difference between 2 sets:
print("s1-s2:")
print(s1.difference(s2))

print("s2-s1:")
print(s2.difference(s1))

T4 (4p) The set is a collection of unordered (and unique) elements. Use this to your advantage to remove duplicates from a list

Hint:

• you can create a set from a list: s = set([1,2,3])
• you can create a list from a set: v = list({1, 2, 3})

Sets can also be used to make things easier when working with lists

Dictionaries

A dictionary is a collection of hashable (immutable) keys and mutable values. Dictionaries are used for fast data access e.g. looking up entries in a dictionary is much faster that searching through a list (except in Python).

d = {}
d = dict()

# a dictionary with keys and values can be declared as follows:
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

# adding a new key to a dictionary:
d = {}
d['a'] = 1
d['b'] = 2
d['c'] = 3
d['d'] = 4

# looping through a dictionary:
for key in d:
print(key)  # print only key
print(key, d[key])  # print key and value d[key]

# looping using the key and value:
for key, value in d.items():
print(key, value)

# to verify if a dictionary has a specific key use the get() function:
if d.get('a'):  # verify if the dictionary contains the key 'a'
print('ok')
else:

# alternative
if 'a' in d:
print('ok')
else:
print('not found')
# counting the number of times a letter appears in a string:
s = 'ana are mere'
d = {}
for elem in s:  # pass through all the characters in the string
if d.get(elem):  # verify if the character exists in the dictionary
d[elem] += 1  # if it exist add 1 to the value for that character
else:  # if it doesn’t exist initialize a new key with the value of the character
d[elem] = 1  # and initialize the value (which is the counter) to 1

Using sets, dictionaries and combinations the Apriori algorithm can be implemented. Apriori algorithm, a classic algorithm, is useful in mining frequent itemsets and relevant association rules. Usually, you operate this algorithm on a database containing a large number of transactions.

T5 (4p - bonus) You have a list of numbers. Write a program to show the frequency of each number as the number of times that number is found in the list.

Hint:

• Use dictionaries to keep track of each element

Resources: