product = 0
print('Product before: '+ str(product))
def do_calculation(a,b):
product = a * b
return product
r = do_calculation(3,2)
print('Result: '+ str(r))
print('Product after: '+ str(product))
Product before: 0 Result: 6 Product after: 0
num2
from? What are the risks with this?def division(num1):
result = num1/num2
return result
num2 = 2
division(8)
function
that takes a list as input and returns a list with only the even valuesBONUS EXERCISES
fh = open('../../downloads/genotypes_small.vcf', 'r', encoding = 'utf-8')
wt = 0
het = 0
hom = 0
for line in fh:
if not line.startswith('#'):
cols = line.strip().split('\t')
chrom = cols[0]
pos = cols[1]
if chrom == '2' and pos == '136608646':
for geno in cols[9:]:
alleles = geno[0:3]
if alleles == '0/0':
wt += 1
elif alleles == '0/1':
het += 1
elif alleles == '1/1':
hom += 1
freq = (2*hom + het)/((wt+hom+het)*2)
print('The frequency of the rs4988235 SNP is: '+str(freq))
fh.close()
def updateCounts(#inputs):
# put code here
return wt, het, hom
def calculateFreq(#inputs):
# put code here
return freq
def formatNicely(#inputs):
# put code here
return formatted
fh = open('../../downloads/genotypes_small.vcf', 'r', encoding = 'utf-8')
wt = 0
het = 0
hom = 0
for line in fh:
if not line.startswith('#'):
cols = line.strip().split('\t')
chrom = cols[0]
pos = cols[1]
if chrom == '2' and pos == '136608646':
for geno in cols[9:]:
wt, het, hom = updateCounts(geno[0:3], wt, het, hom)
fh.close()
freq = calculateFreq(wt, het, hom)
print('The frequency of the rs4988235 SNP is: '+formatNicely(freq)) # print result with 2 decimals
# first define the function
def square(number): # this function takes one argument as input
squared = number * number # do the calculation
return squared # return the result
square(5) # call the function with the input to test
25
What you name your input arguments (in this example number) does not matter, that is just like naming any other variable. Just make sure to give it a reasonable name (writing def square(elephants) works, but probably you'll just confuse yourself)
2. Write a function that takes two numbers as input and returns the products of the two:
def product(num1, num2): # this function takes exactly two arguments as input
result = num1 * num2
return result
product(3,6) # call the function with 2 arguments as input
18
3. Study the code below. Why does product not change?
4. Study the code below. Where does it get num2 from? What are the risks with this?
product = 0
print('Product before: '+ str(product))
def do_calculation(a,b):
product = a * b
return product
r = do_calculation(3,2)
print('Result: '+ str(r))
print('Product after: '+ str(product))
Product before: 0 Result: 6 Product after: 0
def division(num1):
result = num1/num2
return result
num2 = 2
res = division(8)
print(res)
4.0
Above in the first example we first assign 0 to product, but inside the function we re-assign a*b to product, so why hasn't product changed when we print it in the end? Because any variable assigned within the function is a local variable that does not exist outside the function. On the other hand, any variable assigned outside a function is global, meaning functions can access them from the outside, but not the other way around. This can be seen in the second example above, where the function first looks for any local variables within the function called num2, and when it doesn't find anything looks outside for a global variable. The risks with this behaviour comes when re-using variable names inside the function that has already been used outside. If you forget to re-assign it inside the function it will not crash with an error message, but actually give you a results that might be false.
5. Write a function that returns a list with only even values:
def evenList(lst): # input list
newList = [] # create an empty list
for item in lst: # loop over list
if item%2 == 0: # check if item in list is dividable with 2
newList.append(item) # append to new list
return newList
myList = [1,2,5,8,9,13,16]
myEvenList = evenList(myList) # save the new list
print(myEvenList)
[2, 8, 16]
BONUS EXERCISES
6. Reformat the below code to use functions as shown under (Note, this is the code from Day_2_Exercise_1):
def updateCounts(alleles, wt, het, hom):
if alleles == '0/0':
wt += 1
elif alleles == '0/1':
het += 1
elif alleles == '1/1':
hom += 1
return wt, het, hom
def calculateFreq(wt, het, hom):
freq = (2*hom + het)/((wt+hom+het)*2)
return freq
def formatNicely(freq):
formatted = str(round(freq,2))
return formatted
fh = open('../../downloads/genotypes_small.vcf', 'r', encoding = 'utf-8')
wt = 0
het = 0
hom = 0
for line in fh:
if not line.startswith('#'):
cols = line.strip().split('\t')
chrom = cols[0]
pos = cols[1]
if chrom == '2' and pos == '136608646':
for geno in cols[9:]:
wt, het, hom = updateCounts(geno[0:3], wt, het, hom)
fh.close()
freq = calculateFreq(wt, het, hom)
print('The frequency of the rs4988235 SNP is: '+formatNicely(freq))
The frequency of the rs4988235 SNP is: 0.78
7. Put all the functions you wrote above into a separate file, and re-do the above assignments by importing the necessary functions from the file
If you put the functions from above into the file myFunctions.py located in the same folder, you would import the functions with:
from myFunctions import updateCounts, calculateFreq, formatNicely
If you have many functions to import, you can use:
from myFunctions import *