#100DaysofCode >> Week 2
2nd week is in the books. I was dragging a little bit this week since I didn't sleep that well a few nights but I kept with it, even if it felt like crawling along sometimes.
Incoherent stream of what I covered in week 2:
>>> Unstructured and structured data types that are built into Python; list, tuple, dictionary and set.
>>>Lists are unordered mutable collections of objects, similar to an array. Mutable in that it can change your list at anytime and dynamically grow and shrink on command. You can mix and match objects of different types and you don't have to predeclare the type of objects you are storing; e.g. [1, "Hello", 2.5]
Lists have useful functions such as len, append, remove, pop, extend and insert. Determine the number of objects in a list with len(). Using dot-notation you can append an object to the end of a list, remove an object by passing it's value to the function, pop an object and return its value from the end of a list or a specific object by passing an index value to the function, extend a list by passing another list as a value to the function, and insert an object by passing an index value to the function. E.g.
num = [1, 2, 3,]
len(num)
num.append(4)
num.remove(3)
num.pop()
num.extend([3, 5])
num.insert(5, 6) index value first, object second
Use the in or not in operators to check membership within a list. Turn anything into a list by just passing it as a value to the list function, e.g.
word = "Hello World"
list_word = list(word)
Return a list back into a string using the join function:
''.join(list_word)
Be careful when copying lists by using an assignment operator. If done by this method then any changes to the copied list will affect the original. Instead use the copy function, e.g.
first = [1, 2, 3]
second = first.copy()
You can access the objects in a list using the square bracket notation and an index value. Unlike other languages, python also lets you use negative values to access objects as well by starting from the end of the list, e.g.
first[1] prints 2
first[-1] prints 3
This can also be utilized in the range function with the start, stop, step values. Using that same method we can carve a slice out of our list using those same start, stop, step values, e.g.
name = "Head First, Python!"
name[0:4:1] prints 'Head'
You don't have to provide all values, you can just use one, e.g.
name[::2] prints every other
For loops can iterate through lists, you can also utilize slice notation to iterate only over parts of a list.
>>> Tuple is an ordered immutable list.
>>>Dictionary is an unordered, mutable set of key/value pairs; similar to an associative array or a hash table. Each key is unique and has a value associated with it. Generally 2 columns and multiple rows, encased in curly brackets and key/values connected by a colon and separated by a comma, e.g.
info = { 'Name' : 'Jared',
'Gender' : 'male' }
Dictionaries understand square bracket notation by passing the key as the value, e.g.
info['Name] prints 'Jared'
Add to your dictionary using bracket notation as well:
info['Age'] = 33
info['Age'] += 1 will increase by 1 to 34
info['Age'] -= 1 will decrement by 1 to 33
In order to use a for loop over a dictionary we have to assign the iteration variable as a key value in square bracket notation, e.g.
for k in found:
print(k, 'was found', found[k], 'time(s).')
However it is better to use the items function to loop over a dictionary, e.g.
for k, v in sorted(found.items()): sorted function will sort results
print(k, 'was found', v, 'time(s).')
When creating a dictionary dynamically values have to be initialized otherwise you will encounter a KeyError. We can do this a few ways; one way is to use an if/in/else block, e.g.
fruits = {}
if 'bananas' in fruits:
fruits['bananas'] += 1
else:
fruit['bananas'] = 1
You can also us an if/not in block:
if 'pears' not in fruits:
fruits['pears'] = 0
fruits['pears'] += 1
This is so common that python has a functions specifically for this called setdefault and is generally the method that is used:
fruits.setdefault('pears', 0)
fruits['pears'] +=1
>>>Set is an unordered set of unique objects; that has useful functions such as unions, intersections and differences. It is useful in removing duplicates from a list. A set is encased in curly braces and the objects and separated by a comma, e.g.
vowels = {'a', 'e', 'i', 'o', 'u'}
Similar to list you can create a set by passing the object as a value to the function, e.g.
word = 'hello'
new_set = set(word) will remove duplicated 'l'
You can use union method to combine two sets (which of course will remove an duplicates):
u = vowels.union(set(word))
u_list = sorted(list(u)) sorts and converts to list
The difference method when given two sets will tell what is in one but not the other:
d = vowels.difference(set(word))
The above compares the objects in vowels against the objects in set(word), then returns a new set (called d here) which are in the vowels set but not the word set.
Intersections will show you the commonalities between two sets:
i = vowels.intersection(set(word))
One nice thing about these weekly recaps is I can more accurately reflect on how much information I am getting through. With the day to day it doesn't feel like you are accomplishing that much, but when you review everything from the past week you can see that you are progressing more than you realize. Up next, week 3!