An introduction to solving biological problems with Python

Learning objectives

  • Recall what we've learned so far on variables, common data types and collections
  • Propose and create solutions using these concepts in an exercise
  • Use conditions to execute specific code block
  • Employ loops to repeat code block
  • Practice reading and writing files with Python
  • Solve more complex exercises

Course schedule - day two

  • 09:30-09:45: [0h15] Introduction
  • 09:45-10:45: [1h00] Session 2.1 - Conditional execution
  • 10:45-11:00: break
  • 11:00-12:30: [1h30] Session 2.2 - Loops
  • 12:30-13:30: lunch break
  • 13:30-15:00: [1h30] Session 2.3 - Files
  • 15:00-15:15: break
  • 15:15-16:15: [1h00] Session 2.4 - Delimited files
  • 16:15-16:30: break
  • 16:30-17:00: [0h30] Wrap-up

What we've learned so far

  • Simple data types, Collections
  • Functions used so far...

Simple data types


In [1]:
## Integer
i = 1
print('Integer:', i)
## Float
x = 3.14
print('Float', x)
## Boolean
print(True)


Integer: 1
Float 3.14
True

In [2]:
## String
s0 = '' # empty string
s1 = 'ATGTCGTCTACAACACT' # single quotes
s2 = "spam's" # double quotes
print(s1 + s2) # concatenate
print(s1, s2) # print


ATGTCGTCTACAACACTspam's
ATGTCGTCTACAACACT spam's

Collections


In [3]:
## Tuple - immutable
my_tuple = (2, 3, 4, 5)
print('A tuple:', my_tuple)
print('First element of tuple:', my_tuple[0])


A tuple: (2, 3, 4, 5)
First element of tuple: 2

In [4]:
## List
my_list = [2, 3, 4, 5]
print('A list:', my_list)
print('First element of list:', my_list[0])
my_list.append(12)
print('Appended list:', my_list)
my_list[0] = 45
print('Modified list:', my_list)


A list: [2, 3, 4, 5]
First element of list: 2
Appended list: [2, 3, 4, 5, 12]
Modified list: [45, 3, 4, 5, 12]

In [5]:
## String - immutable, tuple of characters
text = "ATGTCATTT"
print('Here is a string:', text)
print('First character:', text[0])
print('Slice text[1:3]:', text[1:3])
print('Number of characters in text', len(text))


Here is a string: ATGTCATTT
First character: A
Slice text[1:3]: TG
Number of characters in text 9

In [6]:
## Set - unique unordered elements
my_set = set([1,2,2,2,2,4,5,6,6,6])
print('A set:', my_set)


A set: {1, 2, 4, 5, 6}

In [7]:
## Dictionary
my_dictionary = {"A": "Adenine", 
                 "C": "Cytosine", 
                 "G": "Guanine", 
                 "T": "Thymine"}
print('A dictionary:', my_dictionary)
print('Value associated to key C:', my_dictionary['C'])


A dictionary: {'A': 'Adenine', 'C': 'Cytosine', 'G': 'Guanine', 'T': 'Thymine'}
Value associated to key C: Cytosine

Functions used so far...


In [8]:
my_list = ['A', 'C', 'A', 'T', 'G']
print('There are', len(my_list), 'elements in the list', my_list)
print('There are', my_list.count('A'), 'letter A in the list', my_list)
print("ATG TCA CCG GGC".split())


There are 5 elements in the list ['A', 'C', 'A', 'T', 'G']
There are 2 letter A in the list ['A', 'C', 'A', 'T', 'G']
['ATG', 'TCA', 'CCG', 'GGC']

Next session

Go to our next notebook: python_basic_2_1