Python Note
Python Note
Unit 4
1. A function has two parts - the head and the body. The head contains the
def keyword, the function name, the parameters the function operates on
within parentheses and a colon. The body contains the operations the
function performs (using the parameters if any) to return a value (usually).
IMPORTANT - Remember the body of the function is indented!
3. There are three kinds of function that you'll use: functions that you
define yourself (like PatternCount), functions that are part of per-existing
python module (like the function 'sqrt' in the module 'math'), functions
that are built-in to python (like len() and .upper() )
6. For this course, the max() function will be important because we'll need
python to compare the frequencies of different k-mers and return the k-
mer with the highest frequency
Unit 5
The main thing to take away from Unit 5 is how to store the k-mer instances and their
frequencies in a dictionary. The example here in Step 4 of 1.3 can be stored in a
dictionary name frequency_map like so - frequency_map = {"ATA" : 3, "ATC" : 1, "CAT" :
1, "CCA" : 1, "CGA" : 1, "GAT" : 1, "TAT" : 2, "TCC" : 1, "TAG" : 1}. If we want to add
another k-mer (let's say CGC) and its frequency (say 3) to the dict, we can add it by
frequency_map["CGC"] = 3
-----------
Unit 5 is about two ways to store a collection of values (so far we were just storing one
value in one variable). By using lists or dictionaries we can store collections. And we have
many ways to access, change, add to and delete the content of lists and dictionaries.
1. Lists - A list is made by writing the name of the list followed by items in the list
enclosed within square brackets (e.g. list_of_colors = ["red", "blue", "green"] ). Items on
the list need not be of the same type (e.g list = [1, 1.5. "red", True] has four items each
with a different type). The items are numbered from zero, so the first item on the list is
indexed as zero, the second as one, and so on.
To access a certain item on a list write the list name followed by the index
number of that item inside [ ]. (e.g for the list n=[1,2,3] to extract the item '2' we
write n[1] because '2' is indexed at 1 on the list ). To access a consecutive set of
items on the list we write the list name followed by the index number of the first
item on that set and a colon, then the index number of the final item on that set +
1 (e.g. if we want to access items 2,3,4 on the list n=[1,2,3,4,5] we write n[1:4] ).
This way of accessing also works with strings, when you need to access certain
segments of it (e.g the segment 'gi' in 'giraffe' would be 'giraffe'[0:2] )
To change a certain item on the list to something else, we access the item by
its index number and then assign a new value. (e.g. if we want to add 4 to the
final item in n=[1,2,3] then we write n[2] = n[2] + 4 OR n[2] += 4 ; the list will
update to [1,2,7] ). If we want to change all the items on the list then we can use
a for loop (e.g to add 4 to all items in n=[1,2,3] we write 'for i in n' - here i will
take on the value of every list item one by one - and then m.append(i+4) - this
will create the list m=[5,6,7]
To add an item to the list we can use .append() and .insert() . When we want to
add a new item to the end of the list, we use append (e.g. to add the item '4' to
the end of the list n=[1,2,3], we write n.append(4). The list updates to [1,2,3,4] ).
When we want to add the new item at the beginning of the list or somewhere
other than the end, we use insert (e.g. to add the item '4' as the second item on
the list n=[1,2,3] we write n.insert(1,4) . The list updates to [1,4,2,3] ). Note that
append only needs one argument (the value it needs to add to the end of the list),
while insert() needs two arguments (the value to be added to the list as well as
the index number at which to add the value)
To remove an item from a list we use .remove() (e.g. to remove '2' from
n=[1,2,3] we write n.remove(2) )
Other functions that work with lists are: len() - gives you the number of items in a
list, index() - gives you the index number of a list item. sort() - arranges the items
in ascending or alphabetical order
2. Dictionaries - A dictionary also stores a collection of items, but each item has two
components - they key and the value. Unlike in an array where we access an item by its
index number, in a dictionary we access an item by its key. Another difference from lists
is that we enclose the dictionary items inside { } instead of [ ]. To make a dictionary,
write the name of the dict followed by key:value pairs inside { } (e.g. student_grades =
{"Sam" : "B", "Jack" : "A", "Lily" : "A+"} )
To access a certain value from a dict, we write the dict name followed by the
key within [ ] (e.g. student_grades["Jack"] lets us access "A")
To change a certain value from a dict, write the dict name then the key within [
] and then the new value (e.g. student_grades["Sam"] = "A" - changes the value
of they key "Sam" from "B" to "A")
To add a new item at the end of a dict, we write dict name followed by the
new key name within [ ] followed by the value (e.g. student_grades["Emily"] =
"B+" - will update student_grades to {"Sam" : "B", "Jack" : "A", "Lily" : "A+",
"Emily" : "B+"}
To remove an item from the dict, we use del (e.g del student_grades["Sam"]
updates the dict to {"Jack" : "A", "Lily" : "A+", "Emily" : "B+"} )
Functions which work with dictionaries: len() - gives the number of items in the
dict