## Before session 3

Before the next class session, watch the videos about tuples and read the text material about sets.

### Tuples

Slides

Text-book: chapter 10 - tuples

### Sets

Python also has a data type for *sets*. A set is an unordered collection of unique elements. A set does not have duplicate elements. Every element is unique (no duplicates) and immutable (cannot be changed). The elements of a set can be of any data type: integers, floats, strings, even tuples, and so on.

Reminder: a set cannot have mutable elements (like lists or dictionaries have). However, a set itself is mutable - we can add or remove elements from it.

#### Creating sets

A set is created by placing all the elements inside the curly brackets {}, separated by comma or by using the built-in function *set()*. Note: to create an empty set you have to use *set()*, not *{}*; the latter creates an empty dictionary.

s1 = {8, 2, 3, 6, 7} s2 = set([6, 4, 5]) s3 = set('Good morning') s4 = set()

Go through the following examples as well.

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> print(basket) # show that duplicates have been removed {'orange', 'apple', 'pear', 'banana'} >>> a = set('abracadabra') >>> a # unique letters in a {'d', 'a', 'r', 'c', 'b'}

#### Set operations

Sets have a large number of built-in methods. In the table below, only some methods are tabulated. More information about sets and their methods can be found in Python documentation.

Operation | Description |
---|---|

`s.add(el)` | adds element `el` to set `s` |

`s.remove(el)` | removes element `el` from set `s` if the element is in the set. If `el` is not in the set, a KeyError is raised. |

`s.discard(el)` | removes element `el` from set `s` , if the element is in the set. If `el` is not in the set, nothing is done. |

`s.update(s1)` | updates set `s` adding the elements from another set `s1` |

`s.pop()` | removes an arbitrary element from set `s` , the method returns the removed element |

`s.clear()` | removes all the elements from set `s` |

`s.copy()` | returns a new set with a shallow copy of set `s` |

Sets also support mathematical operations like union, intersection, difference, and symmetric difference.

Operation | Description |
---|---|

`A & B` `A.intersection(B)` | a new set with elements common to A and B |

`A | B` `A.union(B)` | a new set with all elements from both A and B |

`A - B` `A.difference(B)` | difference of A and B (A - B) is a set of elements that are only in A, but not in B |

`A ^ B` `A.symmetric_difference(B)` | symmetric difference of A and B is a set with elements in either A or B, but not in both |

`A <= B` `A.issubset(B)` | tests whether every element in set A is in set B |

`A >= B` `A.issuperset(B)` | tests whether every element in set B is in set A |

Try yourself how all these operations work!

#### Iterating through a set

Using a for loop, we can iterate though a set.

for letter in set("apple"): print(letter,end=" ")

In the output, the duplicates are removed and the order is not preserved:

e a l p

### Test

Go to Moodle and take the third test on tuples and sets.

### Homework

**The deadline for homework is Friday.** This means, you have to start with homework already before the session.

#### Example 1. Meetings

The following program is a modified example from last week - *The meetings program*. The file *meetings.txt* contains records of meetings. Each number in the file indicates the day of the week when an appointment takes place. The following program reads the data from the file and creates a dictionary (the days of the week are in the keys, and the numbers from the file are in the values). Then the program creates a list of tuples and sorts it in the reversed order. Finally, the program outputs three days which have the most meetings.

def day_of_week(n): days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] return days[n-1] ffile = open("meetings.txt") meetings = dict() for line in ffile: try: number = int(line) day = day_of_week(number) meetings[day] = meetings.get(day,0) + 1 except: print("Invalid value") ffile.close() lst = list() for key, val in meetings.items(): lst.append( (val, key) ) lst.sort(reverse=True) for val, key in lst[:3]: print(key, val)

#### Example 2. Names

The present program prompts the user for first and last names of three people and outputs two names which have at most common letters in both names. Pay attention to function *names* which returns several values (actually one tuple).

def names(number): first = input("Please enter the first name of person number "+str(number)+": ") last = input("Please enter the last name of person number "+str(number)+": ") return first.lower(), last.lower() def common(firstX, lastX, firstY, lastY): firsts = set(firstX) & set(firstY) lasts = set(lastX) & set(lastY) return len(firsts) + len(lasts) first1, last1 = names(1) first2, last2 = names(2) first3, last3 = names(3) common12 = common(first1, last1, first2, last2) common23 = common(first2, last2, first3, last3) common13 = common(first1, last1, first3, last3) if common12 >= common13 and common12 >= common23: print("Names of persons 1 and 2 are more common to each other.") if common13 >= common12 and common13 >= common23: print("Names of persons 1 and 3 are more common to each other.") if common23 >= common12 and common23 >= common13: print("Names of persons 2 and 3 are more common to each other.")

### Homework 3

The deadline is Friday, the 26th of November, 19:00 (Estonian time).

#### Exercise 1. Birthdays

Modify the *birthdays program* : the program should output top 5 months that have the most birthdays.

#### Exercise 2. Distances between points

Write a function *coordinates* that has one argument - *point_name*. The function has to prompt the user for x and y coordinates of the point and return the coordinates in a tuple.

Write a function *distance* that takes four coordinates (two coordinates for each point) as its arguments and returns the distance between two points. *Hint:*

Write a program that asks the user for the coordinates of three points (A, B, C) using function *coordinates*, finds out and outputs two points that are the closest to each other (use function *distance*) (you can assume that all distances are different).

Here is an example of the program output:

Please enter point A x coordinate: 0 Please enter point A y coordinate: 0 Please enter point B x coordinate: 3 Please enter point B y coordinate: 2 Please enter point C x coordinate: 8 Please enter point C y coordinate: 9 Points A and B are the closest to each other.