-
Bertrand Néron authoredBertrand Néron authored
Dive into Functions
Exercises
8 Exercise
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.
Hint locals print a dictionary with local variable as keys and their respective values.

x = 4
def func():
y = 5
print locals()
>>> func()
{'y': 5}
>>> print x
4

9 Exercise
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.
Hint locals print a dictionary with local variable as keys and their respective values.

x = 4
def func():
y = 5
x = 8
print locals()
x = x + 2
>>> y = func()
{'y': 5, 'x': 8}
>>>
>>> print y
None
>>> print x
4

10 Exercise
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.
Hint locals print a dictionary with local variable as keys and their respective values.

x = 4
def func(a):
y = x + 2
print locals()
x = y
return y
>>> y = func(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in func
UnboundLocalError: local variable 'x' referenced before assignment
Unlike what we might think in y = x + 2 x is not get from the global scope. As soon as you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope. even if the assignment appear later in code. Here x = y make x as local variable whatever you are in func. so at line y = x + 2 we try to use the local variable x but we have to asign it a value (it is done later) so Python raise an UnboundLocalError (see python faq for details)

11 Exercise
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.
Hint locals print a dictionary with local variable as keys and their respective values.

x = 4
def func(a):
x = x + 2
print locals()
return x
y = func(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in func
UnboundLocalError: local variable 'x' referenced before assignment
print y
print y == x

12 Exercice
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.

x = 4
def func(x):
x = x + 2
return x
y = func(x)
>>> print x
4
>>> print y == x
False

13 Exercice
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.

def func():
y = x
return y
>>> x = 4
>>> z = func()
>>>
>>> print x
4
>>> print z
4
>>> print id(z) == id(x)
True

14 Exercice
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.

x = 4
def func(x = 5):
x = x + 2
return x
>>> y = func(x)
>>>
>>> print x
4
>>> print y
6

15 Exercice
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.
Hint locals print a dictionary with local variable as keys and their respective values.

x = 4
def func(a):
global x
def func2():
print locals()
y = x + 4
return y
z = func2()
return z
y = func(x)
{}
>>> print x
4
>>> print y
8

16 Exercice
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.

x = {'a' : 4}
def func(a):
a['b'] = 5
return a
y = func(x)
>>> print x
{'a': 4, 'b': 5}
>>> print y
{'a': 4, 'b': 5}
>>> print x is y
True

17 Exercice
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.

x = {'a' : 4}
def func(a):
a['b'] = 5
y = func(x)
>>> print x
{'a': 4, 'b': 5}
>>> print y
None

18 Exercice
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.

x = {'a' : 4}
def func(a):
x['b'] = 5
def func2():
a['b'] = 6
return a
y = func(x)
print x
{'a': 4, 'b': 5}
print y
{'a': 4, 'b': 5}

19 Exercice
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.

x = {'a' : 4}
def func(a):
x['b'] = 5
def func2():
a['b'] = 6
func2()
return a
y = func(x)
print x
{'a': 4, 'b': 6}

20 Exercice
Without executing the code in Python interpreter, can you determine what the code below print out. help you by drawing diagram.

x = {'a' : 4}
def func(a):
x['b'] = 5
def func2(x):
x['b'] = 6
func2(a.copy())
return a
y = func(x)
print x
{'a': 4, 'b': 5}

21 Exercice
Use the code of the exercise 4.5.7 on the kmer. Make a function which compute all kmer of a given length in a sequence.
:download:`kmer.py <_static/code/kmer.py>` .
22 Exercise
Write a function translate that have a nucleic sequence as parameter, and return the translate sequence. We give you a genetic code :
code = { 'ttt': 'F', 'tct': 'S', 'tat': 'Y', 'tgt': 'C',
'ttc': 'F', 'tcc': 'S', 'tac': 'Y', 'tgc': 'C',
'tta': 'L', 'tca': 'S', 'taa': '*', 'tga': '*',
'ttg': 'L', 'tcg': 'S', 'tag': '*', 'tgg': 'W',
'ctt': 'L', 'cct': 'P', 'cat': 'H', 'cgt': 'R',
'ctc': 'L', 'ccc': 'P', 'cac': 'H', 'cgc': 'R',
'cta': 'L', 'cca': 'P', 'caa': 'Q', 'cga': 'R',
'ctg': 'L', 'ccg': 'P', 'cag': 'Q', 'cgg': 'R',
'att': 'I', 'act': 'T', 'aat': 'N', 'agt': 'S',
'atc': 'I', 'acc': 'T', 'aac': 'N', 'agc': 'S',
'ata': 'I', 'aca': 'T', 'aaa': 'K', 'aga': 'R',
'atg': 'M', 'acg': 'T', 'aag': 'K', 'agg': 'R',
'gtt': 'V', 'gct': 'A', 'gat': 'D', 'ggt': 'G',
'gtc': 'V', 'gcc': 'A', 'gac': 'D', 'ggc': 'G',
'gta': 'V', 'gca': 'A', 'gaa': 'E', 'gga': 'G',
'gtg': 'V', 'gcg': 'A', 'gag': 'E', 'ggg': 'G'
}
22.1 bonus
This function have to take the phase as parameter
:download:`translate.py <_static/code/translate.py>` .
23 Exercise
Write a program that calculates the similarity of 2 RNA sequences.
-
To compute the similarity you need to parse a file containing the :download:`similarity matrix <_static/data/similarity_matrix>`.
Hint: use the module containing the functions that handle a matrix from previous chapter. put this matrix.py file in a directory named "my_python_lib" in your home or Desktop and import it in your current program (the similarity script must be placed elsewhere).
-
The similarity of the 2 sequences is the sum of base similarities. so you have to compare the first base of two sequences and use the matrix to get the similarity from the similarity table, on so on for all bases then sum these similarities.
First implementation
:download:`matrix.py <_static/code/matrix.py>` .
Second implementation
:download:`matrix2.py <_static/code/matrix2.py>` .