<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <figure>
  <IMG SRC="../../logo/logo.png" WIDTH=250 ALIGN="right">
</figure>
IHE Python course, 2017
Tuples, lists and sets
T.N.Olsthoorn, Feb 2017
Step1:  Tuples, lists, sets, dicts, strings and numpy arrays are the hard core of the objects just to handle data in phython. In this notebook we learn to handle tuples, lists and sets as basic tools for every day python. Next we'll do strings as immutable sequences of characters and then dicts storage devices in which we can reach values by keys. We'll show various practical ways to use them and their methods.
Lists, sets and dicts are often generated and filtered with comprehensions, we'll handle these powerfull tools in a separate notebook.
Tuples, immutable lists
Tuples are recognized by comma-separted items within paranthesis ( ) 
Step2:  Tuples are hany for multiple assignments
Step3:  Famous Python way of reversing values of parameters
Step4:  Tuples like lists are sequences of aribitrary itmes
Step5:  You an iterate over tuples, lists and sets (and dicts and strings and possible other sequences)
Step6:  But you cannot change a tuple because it's immutable.
Step7:  The methods that are associated with tuple can be seen by typing tuple then a dot and then tapping the tab key. There are only two, count(item) and index(item).
Step8:  Lists
Step9:  Lists like tuples are sequences of aribitrary itmes
Step10:  Till here there's no difference between tuples and lists. But contratry to tuples, list are mutable. You can see this from the methods that are associated with lists
Step11:  Instead of only two (count() and index()) there are now 11. The other 9 methods can change the contents of the list.
Let's use the append to append itmes to an initally empty list to get the same list as before
Step12:  Let's pop out the 'copy' using pop()
Step13:  Item copy was popped out and assigned to c; is now missing from list publicListMethods.
We can insert it again at (before) any position using insert
Step14:  We will often see more complex structures like list of lists and list of tuples or tuples of list or even lists of lists of lists. Hence we can build arbitrary comples structures, even in combination with sets and dicts.
But list of lists or lists of tuples or tuples of tuples or tuples of lists are very common.
Step15:  We can access any item with indexing and slicing
Step16:  Sets
Python has a full set of set logic.
A set is recognized by itmes within curly braces { }
Step17:  You see that only the unique itmes are kept. Their order if of no importance.
Which itmes are in mySet and not in yourSet ??
Step18:  Which itmes are in yourSet and not in mySet ??
Step19:  Which items are in either set?
Step20:  Which items are in both sets?
Step21:  Adding and multiplying tuples and lists
Becaus Python is an object oriented languages, in which any item is an object that carries along a set of methods that is associated with it, any method can be defined to mean what is useful for the class of objects to which it belongs.
Arithmathic addition and multiplication have not direct meaning for lists and tuples. But the + and the * can be benificially redefined for tuples and lists (and strings) to mean concatenation and multiple concatenation instead.
<ASSISTANT_TASK:>
Python Code:
from pprint import pprint
import numpy as np
Explanation: <figure>
  <IMG SRC="../../logo/logo.png" WIDTH=250 ALIGN="right">
</figure>
IHE Python course, 2017
Tuples, lists and sets
T.N.Olsthoorn, Feb 2017
End of explanation
myTuple = ('This', 'is', 'our', 'tuple', 'number', 1)
print("This tuple contains {} itmes.".format(len(myTuple)))
print("Here you see that the object is a tuple: {}".format(type(myTuple)))
print("If you ask if this is a tuple, this is the answer:", isinstance(myTuple, tuple))
'duck', 'goat', 'dog'  # this is also a tuple, parenthesis are not needed
print( type( ('duck')) )
('duck')  # this is not a tuple, it's a single item between paranthesis
print( type( ('duck',) ) )
('duck',)   # to make python recognize a single item as a tuple, put a comma behind it
'duck',  # parenthesis are not needed, but the comma is to make it a tuple
12, # this hold true for numbers as well as for any objects
Explanation: Tuples, lists, sets, dicts, strings and numpy arrays are the hard core of the objects just to handle data in phython. In this notebook we learn to handle tuples, lists and sets as basic tools for every day python. Next we'll do strings as immutable sequences of characters and then dicts storage devices in which we can reach values by keys. We'll show various practical ways to use them and their methods.
Lists, sets and dicts are often generated and filtered with comprehensions, we'll handle these powerfull tools in a separate notebook.
Tuples, immutable lists
Tuples are recognized by comma-separted items within paranthesis ( ) :
End of explanation
a, b, c, d = 12, ['a', 'duck'], np.sin, ['quack', 2]
print("a = ", a)
print("b = ", b)
print("c = ", c)
print("d = ", d)
Explanation: Tuples are hany for multiple assignments:
End of explanation
c, a, d, b = a, b, c, d
print("a = ", a)
print("b = ", b)
print("c = ", c)
print("d = ", d)
Explanation: Famous Python way of reversing values of parameters:
End of explanation
myTuple = (12, 'a duck', np.sin, {'cat': 'Spooky', 'dog' : 'Barky', 'horse' : 'Duky'}, {'a', 'b', 'a', 'c', 'b', 'b'})
myTuple
Explanation: Tuples like lists are sequences of aribitrary itmes:
End of explanation
for k in myTuple:
    print(type(k))
12 and np.sin in myTuple
And get values by indexing:
myTuple[3]
myTuple[1:3]
Explanation: You an iterate over tuples, lists and sets (and dicts and strings and possible other sequences):
End of explanation
myTuple[0] = 14
Explanation: But you cannot change a tuple because it's immutable.
End of explanation
myTuple.count(np.sin) # returns the number of occurences of item
myTuple.index(np.sin) # returns the index (location) of itme in tuple
dir(tuple) # gives all methods, both "private" and public
Explanation: The methods that are associated with tuple can be seen by typing tuple then a dot and then tapping the tab key. There are only two, count(item) and index(item).
End of explanation
Lists are like tuples sequences, but the are mutable.
Lists are recognized by a series of comma-separted items within square brackets [ ]
myList = ['This', 'is', 'our', 'list', 'number', 1]
print("This list contains {} itmes.".format(len(myList)))
print("Here you see that the object is a list: {}".format(type(myList)))
print("If you ask if this is a list, this is the answer:", isinstance(myList, list))
Explanation: Lists
End of explanation
myList = [12, 'a duck', np.sin, {'cat': 'Spooky', 'dog' : 'Barky', 'horse' : 'Duky'}, {'a', 'b', 'a', 'c', 'b', 'b'}]
myList
Explanation: Lists like tuples are sequences of aribitrary itmes:
End of explanation
[p for p in dir(myList) if not p.startswith('_')]
Explanation: Till here there's no difference between tuples and lists. But contratry to tuples, list are mutable. You can see this from the methods that are associated with lists:
Let's use a list comprehension to get all public methods associated with lists:
End of explanation
publicListMethods = []  # empty list, you could also use = list()
for p in dir(myList):
    if not p.startswith('_'):
        publicListMethods.append(p)
publicListMethods
Explanation: Instead of only two (count() and index()) there are now 11. The other 9 methods can change the contents of the list.
Let's use the append to append itmes to an initally empty list to get the same list as before:
End of explanation
c = publicListMethods.pop(2)
print(c)
publicListMethods
Explanation: Let's pop out the 'copy' using pop():
End of explanation
publicListMethods.insert(4, 'copy')
print(publicListMethods)
publicListMethods.sort(reverse=True)  # inplace sort
print(publicListMethods)
publicListMethods.reverse()  # inplace reverse
print(publicListMethods)
sorted(publicListMethods)  # sorts, not in place, it makes a copy, returns a new list
help(list.sort)
help(sorted)
Explanation: Item copy was popped out and assigned to c; is now missing from list publicListMethods.
We can insert it again at (before) any position using insert:
End of explanation
listList = [['quack', 'duck', 'age', 2],
            ['bark', 'dog', 'age', 2],
            ['splash', 'fish', 'age', 5],
            ['tweet', 'Trump', 'age', 70]]
Explanation: We will often see more complex structures like list of lists and list of tuples or tuples of list or even lists of lists of lists. Hence we can build arbitrary comples structures, even in combination with sets and dicts.
But list of lists or lists of tuples or tuples of tuples or tuples of lists are very common.
End of explanation
listList[1]
listList[3:0:-1]
listList[2][2]
listList[2][3:1:-1]
p = listList.pop(2)
print(p)
print(listList)
listList.append(p)
print(listList)
Explanation: We can access any item with indexing and slicing:
End of explanation
mySet =  {'dog', 'cat', 'cat', 'horse', 'fish', 'dog', 'camel'}
yourSet = {'bird', 'cat', 'elephant', 'chimp', 'dog', 'elephant', 'zebra'}
print(mySet)
print(yourSet)
Explanation: Sets
Python has a full set of set logic.
A set is recognized by itmes within curly braces { }
End of explanation
print(mySet - yourSet)
Explanation: You see that only the unique itmes are kept. Their order if of no importance.
Which itmes are in mySet and not in yourSet ??
End of explanation
print(yourSet - mySet)
Explanation: Which itmes are in yourSet and not in mySet ??
End of explanation
print(yourSet | mySet)
Explanation: Which items are in either set?
End of explanation
print(yourSet & mySet)
Explanation: Which items are in both sets?
End of explanation
myList = ['john', 'goes', 'home']
yourList=['with', 'mary']
print(myList + yourList)
print(yourList * 6)
print( (myList + yourList) * 2)
print('John walks home'  + ' with Mary')  # addition of two strings
print('-' * 80)   # holds also for strings, multiplication of a string
'Today, ' + str(5) + ' boats arrived at about ' + str(12) + " o'clock."  # use str(number) to convert to string
Explanation: Adding and multiplying tuples and lists
Becaus Python is an object oriented languages, in which any item is an object that carries along a set of methods that is associated with it, any method can be defined to mean what is useful for the class of objects to which it belongs.
Arithmathic addition and multiplication have not direct meaning for lists and tuples. But the + and the * can be benificially redefined for tuples and lists (and strings) to mean concatenation and multiple concatenation instead.
End of explanation
<END_TASK>