In [2]:
# Let's do something with strings
a_string = "Happy birthday"
type(a_string)
Out[2]:
In [3]:
a_string.count("p")
Out[3]:
In [4]:
a = 3
In [5]:
# We can't count strings in an integer
a.count("p")
In [6]:
x = True
In [7]:
type(x)
Out[7]:
In [8]:
x = a > 4
In [9]:
x
Out[9]:
In [10]:
x = "3"
x + 2
In [11]:
my_tuple = ('I', 'like', 'cake')
my_tuple
Out[11]:
In [13]:
my_tuple[1]
Out[13]:
In [15]:
my_tuple[1:3]
Out[15]:
Tuples are simple containers for data. They are ordered, meaining the order the elements are in when the tuple is created are preserved. We can get values from our tuple by using array indexing, similar to what we were doing with pandas.
In [ ]:
my_tuple[0]
Recall that Python indexes start at 0. So the first element in a tuple is 0 and the last is array length - 1. You can also address from the end
to the front
by using negative (-
) indexes, e.g.
In [ ]:
my_tuple[-1]
You can also access a range of elements, e.g. the first two, the first three, by using the :
to expand a range. This is called slicing
.
In [ ]:
my_tuple[0:2]
In [ ]:
my_tuple[0:3]
What do you notice about how the upper bound is referenced?
Without either end, the :
expands to the entire list.
In [ ]:
my_tuple[1:]
In [ ]:
my_tuple[:-1]
In [ ]:
my_tuple[:]
Tuples have a key feature that distinguishes them from other types of object containers in Python. They are immutable. This means that once the values are set, they cannot change.
In [ ]:
my_tuple[2]
So what happens if I decide that I really prefer pie over cake?
In [16]:
my_tuple[2] = 'pie'
Facts about tuples:
So then, what are the use cases of tuples?
Write-protects
data that other pieces of code should not alter
In [17]:
my_list = ['I', 'like', 'cake']
a_tuple = ('I', 'like', 'cake')
In [19]:
my_list[2]
Out[19]:
At first glance, tuples and lists look pretty similar. Notice the lists use '[' and ']' instead of '(' and ')'. But indexing and refering to the first entry as 0 and the last as -1 still works the same.
In [ ]:
my_list[0]
In [ ]:
my_list[-1]
In [ ]:
my_list[0:3]
Lists, however, unlike tuples, are mutable.
In [ ]:
my_list[2] = 'pie'
my_list
In [7]:
# Some methods for lists
my_list.index("cake")
Out[7]:
In [20]:
my_list
Out[20]:
In [23]:
my_list.append("very")
In [24]:
my_list
Out[24]:
In [29]:
my_list.sort()
In [30]:
my_list
Out[30]:
There are other useful methods on lists, including:
methods | description |
---|---|
list.append(obj) | Appends object obj to list |
list.count(obj) | Returns count of how many times obj occurs in list |
list.extend(seq) | Appends the contents of seq to list |
list.index(obj) | Returns the lowest index in list that obj appears |
list.insert(index, obj) | Inserts object obj into list at offset index |
list.pop(obj=list[-1]) | Removes and returns last object or obj from list |
list.remove(obj) | Removes object obj from list |
list.reverse() | Reverses objects of list in place |
list.sort([func]) | Sort objects of list, use compare func, if given |
Try some of them now.
my_list.count('I')
my_list
my_list.append('I')
my_list
my_list.count('I')
my_list
#my_list.index(42)
my_list.index('puppies')
my_list
my_list.insert(my_list.index('puppies'), 'furry')
my_list
Dictionaries are similar to tuples and lists in that they hold a collection of objects. Dictionaries, however, allow an additional indexing mode: keys. Think of a real dictionary where the elements in it are the definitions of the words and the keys to retrieve the entries are the words themselves.
word | definition |
---|---|
tuple | An immutable collection of ordered objects |
list | A mutable collection of ordered objects |
dictionary | A mutable collection of named objects |
Let's create this data structure now. Dictionaries, like tuples and elements use a unique referencing method, '{' and its evil twin '}'.
In [31]:
my_dict = {"birthday": "Jan 1", "present": "vacine"}
In [33]:
my_dict.keys()
Out[33]:
In [34]:
my_dict.values()
Out[34]:
In [ ]:
In [ ]:
my_dict = { 'tuple' : 'An immutable collection of ordered objects',
'list' : 'A mutable collection of ordered objects',
'dictionary' : 'A mutable collection of objects' }
my_dict
We access items in the dictionary by name, e.g.
In [ ]:
my_dict['dictionary']
Since the dictionary is mutable, you can change the entries.
In [ ]:
my_dict['dictionary'] = 'A mutable collection of named objects'
my_dict
Notice that ordering is not preserved!
And we can add new items to the list.
In [ ]:
my_dict['cabbage'] = 'Green leafy plant in the Brassica family'
my_dict
To delete an entry, we can't just set it to None
In [ ]:
my_dict['cabbage'] = None
my_dict
To delete it propery, we need to pop that specific entry.
In [ ]:
my_dict.pop('cabbage', None)
my_dict
You can use other objects as names, but that is a topic for another time. You can mix and match key types, e.g.
In [ ]:
my_new_dict = {}
my_new_dict[1] = 'One'
my_new_dict['42'] = 42
my_new_dict
You can get a list of keys in the dictionary by using the keys
method.
In [ ]:
my_dict.keys()
Similarly the contents of the dictionary with the items
method.
In [ ]:
my_dict.items()
We can use the keys list for fun stuff, e.g. with the in
operator.
In [ ]:
'dictionary' in my_dict.keys()
This is a synonym for in my_dict
In [ ]:
'dictionary' in my_dict
Notice, it doesn't work for elements.
In [ ]:
'A mutable collection of ordered objects' in my_dict
Other dictionary methods:
methods | description |
---|---|
dict.clear() | Removes all elements from dict |
dict.get(key, default=None) | For key key, returns value or default if key doesn't exist in dict |
dict.items() | Returns a list of dicts (key, value) tuple pairs |
dict.keys() | Returns a list of dictionary keys |
dict.setdefault(key, default=None) | Similar to get, but set the value of key if it doesn't exist in dict |
dict.update(dict2) | Add the key / value pairs in dict2 to dict |
dict.values | Returns a list of dictionary values |
In [36]:
help( dict)