# Introduction:
# * Who are we
# * Organisation of the first week
# * Group work in the second week
# * Quick overview of projects
# * Organisation of the second week
# * How will we rate?
# * Questions?
# Python is a scripting language -> exactly the commands you enter are executed by the interpreter
# Exception: Everything after a # is a comment, the interpreter ignores it
# Simplest use: claculator
# Run the following command:
2+2
# Whitespace within a command doesn't matter:
30 - 7+2
# Try to stick to one style though
# Whitespace before a command does though, we'll come to that later:
30 - 7 + 2
# The usual rules of arithmetics apply:
50 - 5 * 6
5 + 4 / 2
#There are a few additional useful operators:
17 / 3 # classic division returns a float
17 // 3 # floor division discards the fractional part
17 % 3 # the % operator returns the remainder of the division
5 * 3 + 2 # result * divisor + remainder
5 ** 2 # x ** y is x to the yth power
# You can define variables and give them values using the = sign, the names have to start with a
# letter or an underscore and can contain letters, numbers and underscoes:
N0 = 200 # Number of bacteria in the parental generation
Q = 2 # Number of offspring per generation
n = 10 # Number of generations
# Now we can calculate the number of bacteria in generation n:
N0 * Q ** n
# Note that we can use only variables that have been defined previously:
N0 * Q2 ** n
Q2 = 4
N0 * Q2 ** n
# And that there is a difference between lower- and higher-case letters
N0 * q2 ** n
# We can also assign the result directly to a new variable:
Nn = N0 * Q ** n
# Note how, when we assign something to a variable, it isn't printed on the console any more.
# To print something to the console, we can use the function print:
print(Nn)
# print is a function: It is a piece of reusable code that does something to the arguments passed to it.
# The general structure is: function(argument_1, argument_2, ... argument_n). There are many useful
# built-in functions and many more in modules - collections of functions provided by the Python developers
# or the community - that Andy, Simon and Alona are going to talk about.
# For now, this is how you can define and run your own function:
def plus(value_1, value_2): # function name: plus, two arguments: value_1 and value_2
result = value_1 + value_2
return result
plus(3, 12)
# There two new concepts in the lines above: Blocks and Statements. Let's start with blocks:
# Every time a line ends with a colon, what follows is called a block. A block can be recognised
# by consistent indentation and ends with the first non-indented line. The colon means that the
# line ending with it relates to the entire block following it. So the line:
# def plus(value_1, value_2): defines a new funtion made from the block below, which ends with
# return result - because the next line, plus(3, 12), is not indented anymore.
# A statement is like a function but without the brackets. You cannot define your own statements,
# they are integral parts of the language. This is just a side note and not something that is likely
# to be relevant to you, just note that "return" is a statement, not a function.
# Exercises:
# * Write a function calc_offspring(N0, Q, n) that calculates the number of bacteria in generation
# n given Q and N0 using the above formula.
def calc_offspring(N0, Q, n):
result = N0 * Q ** n
return result
calc_offspring(10, 1.3, 20)
# * Write a function calc_coli_offspring(n) that calculates the number of coli bacteria after n
# generations given that there is just one bacterium to start with and each bacterium generates
# exactly 2 offspring per generation
def calc_coli_offspring(n):
result = 2 ** n
return result
x = calc_coli_offspring(5)
# * Modify the two above functions to print the result in addition to returning it
# There are different types of variables in Python. We are going to look at many over the course
# of this week, but let's start with the two most common ones: floats and strings.
# Every number you are likely to encounter is a float (or an integer, but Python 3+ takes care of
# necessary conversions automatically). Text is stored in strings that are defined as text between
# quotes:
text = "Something"
print(text)
# When you want to use a string, you have to use the quotes, otherwise Python will think you mean a
# variable - this is an extremely common mistake:
text = Something
# If something is a text, Python can not understand its contents - the following does not work:
text = "12"
5 + text
# You first have to convert the text to a number (float or integer) using the appropriate function:
# float and int are functions that take a string as an argument and return a float or an integer
number = float(text)
5 + number
# or:
5 + float(text)
# You can also add strings together:
part_1 = "There are "
part_2 = " students in this room."
print(part_1 + part_2)
newtest = part_1 + part_2
# However, as we have seen before, we can't add strings and numbers:
num_students = 15
print(part_1 + num_students + part_2)
# We first have to convert the number to a string:
print(part_1 + str(num_students) + part_2)
# The character \ is special in strings, it makes the next character special:
# \": The quote will be part of the string instead of ending the string
print("He said: \"I don't think so\".")
# \n: This is a newline
print("This text\nconsists of\nseveral lines.")
# \t: Tab
print("This text\tconsists of\tseveral lines.")
# \\: This is just the character \
print("Otherwise, how would you print \\n instead of inserting a newline?")
# Exercises:
# * Write a function get_num_copies(cT, E, num_cT) that calculates the number of copies of a template in a
# PCR reaction given the cT value, the efficiency of the reaction (how many new molecules are created
# from the a molecule of template in one round of amplification) and the number of molecules necessary to
# create the threshold signal intensity. Have this function print the following result:
# "There were <number> molecules in the mix if the cT was reached after <number> cycles at an efficiency of <number>"
def get_num_copies(cT, E, num_cT):
num_template = num_cT / E ** cT
part_1 = "There were "
part_2 = " molecules in the mix if the cT was reached after "
part_3 = " cycles at an efficiency of "
print(part_1 + str(num_template) + part_2 + str(cT) + part_3 + str(E))
return num_template
get_num_copies(20, 1.5, 30000000)
# * Extend the functions from the first exercise to also print pretty reports of their results
# Now let's come to a more interesting data type: Arrays.
# Instead of holding a single value, an array holds several of them - think of them as vectors:
data = [1,3,5,7,9,33,12,7]
data2 = [1, "bla", "test", 15]
print(data)
# You can access single values in an array using a so-called index, with the first value having the
# index 0:
data[0]
#data = [0]
data[3]
# The number of elements in an array can be found using the function len:
len(data)
# Remember that we start counting from 0, so the last element in an array is the number of elements - 1
data[len(data)] # This does not work, as the index len(data) points to an element that is not in the array
data[len(data) - 1]
# You can also access the last elements by counting from behind:
data[-1]
data[-2]
# Arrays can also be "sliced" - you can create a sub-array containing a range of elements:
data[2:5] # 3 elements from the range [2,5): including data[2] but excluding data[5]
# You can also slice from the start or until the end by omitting the first or last index:
data[:3]
data[3:]
# Slicing can be used to replace or remove elements:
data[3:5] = [1,1,1,1]
data[4:6] = []
# Arrays can also be concatenated:
dat_1 = [1,2,3]
dat_2 = [4,5,6]
dat_both = dat_1 + dat_2
dat_both = dat_both + [7]
dat_both += [7] # "x += y" means "x = x + y", this also works for -=, /= and *=
#dat_both += 7
# And the cool thing? Strings understand all of these array operations, since a string is more or less
# just an array of letters!
text = "This is a sentence."
text[0]
text[:4]
text[-1]
# The one thing that does not work is replacing elements:
text[5:7] = "was"
# But strings can be added together:
text = "This"
text += " is "
text += " a horrible "
text += " example."
text2 = ["This"]
text2 += [" is "]
text2 += [" a horrible "]
text2 += [" example."]
# This is indeed a horrible example - this operation is insanely slow and should only be done for
# very small datasets. In the next days you will learn a more computationally efficient way of adding
# strings together, but for now, let's just go with this.
# Of course, we can do more complex things than just add numbers and text together. For that, we\
# need control structures, such as loops:
a = 0
while a < 10:
print(a)
a += 1
# The structure of this loop is: while condition is met, do something. Here again we have a colon followed
# by a block: The block of code after the loop is executed again and again until the condition is not
# met any more. Remember that this could be forever:
a = 0
b = 0
while a < 10:
print("a: " + str(a) + ", b: " + str(b))
b += 1
# The infinite loop is a very common problem and not always as easy to find as in the example above.
# One use of the while loop is to iterate over an array:
data = [1,5,7,3,2]
i = 0
while i < len(data):
print("Element " + str(i) + ": " + str(data[i]))
i += 1
# There is also another kind of loop that can be used when the exact position within the array is not
# interesting and that greatly reduces the risk of creating an infinite loop:
for x in data:
print(x)
# In the for loop, a running variable is defined (x) and an array is provided (data). For each element
# in the array, the loop block is executed once with x having the value of the element.
# A useful function that is often used in connection with the for loop is the range function - it
# generates a list of numbers in a certain range:
for i in range(5):
print(i)
# Range has up to three arguments: from, to and step:
for i in range(5, 10):
print(i)
for i in range(0,10,3):
print(i)
# By the way: range and len together can be used to substitute the while loop in some cases:
data = [1,5,7,3,2]
for i in range(len(data)):
print("Element " + str(i) + ": " + str(data[i]))
# Exercises (don't use the equivalent built-in functions):
# * Write a function my_sum(values) that takes an array and calculates the sum of the values in the array
data = [1,2,3]
def my_sum(values):
result = values[0]
for number in values:
result += number
return result
my_sum(data)
# * Write a function my_mean(values) that takes an array and calculates the mean of the values in the array
data = [1,2,3]
def my_mean(values):
result = my_sum(values) / len(values)
return result
my_mean(data)
# * Write a function my_fac(val) that calculates the factorial of a value (e.g. 5! = 1*2*3*4*5 = 120)
def my_fac(val):
result = 1
i = 2
while i <= val:
result = result * i
i = i + 1
return result
def my_fac2(val):
result = 1
for i in range(2, val+1):
result = result * i
return result
my_fac2(5)
my_fac(5)
# * Write a function my_fib(val) that prints (or returns) the fibonacci series of the length val.
# The fibonacci series is a series of numbers that starts with [0, 1]. It is continued by adding the
# last two numbers of the series - so my_fib(8) would be
# 0, 1, 1 (0+1), 2 (1+1), 3 (1+2), 5 (2+3), 8 (3+5), 13 (5+8)
# You will likely need to use more than one variable.
def my_fib(val):
result = [0, 1]
i = 2
while i < val:
result += [result[i-1] + result[i-2]]
i = i + 1
return result
my_fib(5)
# In order to write really meaningful code, there is one more control statement that we need to look at: if
x = 5
if x < 10:
print("X is less than 10")
# This works for both numbers and strings:
world = "round"
if world == "flat":
print("Don't fall off!")
# Note the use of "==" here - "=" is used to assign a value, "==" is used to test for equality.
# Conceptionally, this is really simple - if a condition is met, execute the block afterwards.
# If can also be extended using one or more elif - do something if none of the conditions above was
# satisfied and this conditions is - and, if needed, a final else - do something if none of the
# conditions above was satisfied.
x = 15
if x < 10:
print("X is less than 10")
elif x < 20:
print("X is between 10 and 20")
else:
print("X is 20 or more")
# The conditions can be arbitrarily complex and use any previously defined or built-in functions:
data = [1,5,7,3,2]
if max(data) < 8:
print("There are no values larger than 7 in the data array")
# They can also be combined using the keywords "and" and "or":
if max(data) < 6 or max(data) > 8:
print("The maximum value in the array is between 6 and 8")
elif max(data) == 7 and data[0] == 1:
print("The maximum value of the array is 7 and its first value is 0")
# Though conceptionally simple, this is what gives you immense power in programming - choosing
# what to do when is the most important question.
# It is important to note that blocks of code can be nested: You can use if in a loop:
for x in range(0, 10):
if x > 5 and x < 8:
print(x)
else:
print("waiting...")
# And vice versa:
data = [1,2,3,4,5,6,7,8,9,10]
printed = 0
if len(data) < 5:
for x in data:
print(x)
printed += 1
else:
print("Data too long, only using the first 5 elements.")
for x in data[:5]:
print(x)
printed += 1
print("Processed " + str(printed) + "values.")
# Exercises:
# * Write a function my_abs(value) that returns the absolute value of value (e.g. my_abs(-3) = 3)
def my_abs(value):
if value < 0:
return -1*value
else:
return value
def my_abs2(value):
result = 0
if value < 0:
result = -1*value
else:
result = value
return result
def my_abs3(value):
if value < 0:
return -1*value
return value
# * Write a function maxdiff(array) that takes an array and returns the largest difference between two
# consecutive numbers in the array. So the result for maxdiff([1,2,4,12,7]) would be 8 (|4-12|)
def maxdiff(array):
result = 0
i = 0
while i < len(array) - 1:
diff = array[i] - array[i+1]
diff = my_abs(diff)
if diff > result:
result = diff
i += 1
return result
def maxdiff2(array):
result = []
i = 0
while i < len(array) - 1:
diff = array[i] - array[i+1]
diff = my_abs(diff)
result += [diff]
i += 1
return max(result)
maxdiff2([1,2,4,12,7])
maxdiff([1,2,4,12,7])
# * Write a function count_a(seq) that, given a sequence, counts the number of As. So the result for
# count_a("AGTCTGGCATGGCTA") would be 3.
def count_a(seq):
result = 0
for base in seq:
if base == "A":
result = result + 1
return result
count_a("AGTCTGGCATGGCTA")
array = [1, 5, 8]
for kafjsdhf in array:
print(kafjsdhf)
print(kafjsdhf+1)
print("-----")
count_a("AGTCTGGCATGGCTA")
# * Extend the function above to count_base(seq, base) that, given a sequence, counts the number of
# occurrences of base. So the result for count_base("ACGTCGCTAGC", "C") would be 4.
# * Write a function rev(seq) that returns a reversed sequence. So the result for rev("AGGTC") would
# be "CTGGA"
# * Write a function compl(seq) that returns a complemented sequence. So the result for compl("CTGGA")
# would be "GACCT"
# * Using the two above functions (don't overwrite them or copy the code, call them!) write a function
# reverse_complement(seq) that returns the reverse complement of the sequence seq.
# * Write a function find_codon(seq, frame, codon) that, given a sequence, a translation frame (0, 1 or 2)
# and a codon either returns the AA position of the codon in the sequence or -1 if it was not found. So
# the result for find_codon("AGGTGTAGCA", 0, "TAG") should be -1 and the result for
# find_codon("AGGTGTAGCA", 2, "TAG") should be 2 (the second amino acid from the start of the sequence
# given the frame)
# * Using the first function from the exercise (don't overwrite it, call it from your new function), write
# a function count_bases(seq) that prints a table of the frequencies of bases in the sequence.
# Be creative, the result for count_bases("AAGTATCC") could be something like"
# Base Occurrence Frequency
# A 3 37.5%
# G 1 12.5%
# T 2 25%
# C 2 25%
# For bonus points, deal with unsanitary input ("AAGTATCC" could also be "AAG TAT CC") - can you
# get that to work correctly?
# * Write a function maxanydiff(array) that, given an input array, finds the maximum difference between
# any two values in the array. So the result for maxanydiff([1,2,4,12,7]) would be 11 (12-1).
Musterloesungen
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.