prints objects to stdout | |
len | returns the length (number of items) |
reversed | Return a reverse iterator (ex. reverse a string) |
float | convert a string or number to a floating point |
str | convert an object to a string |
range | retuns a sequence of integers |
abs | return the absolute value of a number |
import
statement[module].[function]
:string.format | format string |
string.upper | uppercase string |
string.strip | remove leading and trailing characters |
random.randint | return a random integer |
import random
seq = "ATGTAATCGGGTAC"
seq_len = len(seq) # number of characters in string
seq_lower = seq.lower() # lower case all characters
print(f"Length: {seq_len}")
print(f"Lower case: {seq_lower}")
# Generate 5 random numbers between 0 and 100
for i in range(0, 5):
rand_int = random.randint(0, 100) # 0 <= rand_int <= 100
print(f"{i}: {rand_int}")
$ python3 ex1.py
Length: 14
Lower case: atgtaatcgggtac
0: 23
1: 84
2: 69
3: 55
4: 45
random.randint(A, B)
len()
returns the length in characters of a stringrange(0, 5)
returns a sequence of five ints: 0, 1, 2, 3, 4import random
imports the “random” module from the python standard librarydef power(num, pw):
result = num ** pw
return result
sq = power(8, 2)
print(f"8 squared = {sq}")
cu = power(3, 3)
print(f"3 cubed = {cu}")
$ python3 ex2.py
8 squared = 64
3 cubed = 27
def
keyword is used to define your own functionsdef
keyword needs to be indented 4 spacesreturn
keyword[]
or ()
list = [32]
list = ["DNA Replication"]
nums = [10, 20, 30, 40, 50, 60]
first = nums[0]
second = nums[1]
last = nums[-1]
# string.join() takes in a list of strings
print(",".join([str(first), str(second), str(last)]))
# Extract a "slice". start at index 1 up to index 4
# 0 1 2 3 4 5
# [10, 20, 30, 40, 50, 60]
# * * *
sub_list = nums[1:4]
for n in sub_list:
print(n)
$ python3 ex3.py
10,20,60
20
30
40
nums[3]
nums[-1]
second last item: nums[-2]
nums[1:4]
str()
functionindex: 0 1 2 3 4 5
list: [10, 20, 30, 40, 50, 60] len() = 6
{}
dct = {}
dct = {'key': 'value'}
fruit = {
'oranges': 10,
'grapes': 20,
}
# Add new key
fruit['pears'] = 30
keys = fruit.keys() # fetch all keys as a list
values = fruit.values() # fetch values keys as a list
print(','.join(keys))
total_grapes = fruit['grapes']
print(f"Total grapes: {total_grapes}")
for key in fruit:
value = fruit[key]
print(f"{key} = {value}")
$ python3 ex4.py
pears,grapes,oranges
Total grapes: 20
pears = 30
grapes = 20
oranges = 10
fruit['grapes']
fruit.keys()
for key in fruit
Here’s a short DNA sequence:
ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT
Write a program that will print out the AT content of this DNA sequence.
seq = 'ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT'
print(seq.count('A') + seq.count('T'))
seq = 'ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT'
at_counter = 0
for base in seq:
if base.lower() == 'a' or base.lower() == 't':
at_counter = at_counter + 1
print(at_counter)
seq = 'ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT'
print(sum(1 for b in seq if b == 'A' or b == 'T'))
Here’s a short DNA sequence:
ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT
Write a program that will print the complement of this sequence.
seq = 'ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT'
comp = ''
for base in seq:
if base.upper() == 'A':
comp = comp + 'T'
elif base.upper() == 'T':
comp = comp + 'A'
elif base.upper() == 'C':
comp = comp + 'G'
elif base.upper() == 'G':
comp = comp + 'C'
print(comp)
seq = 'ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT'
base_map = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'}
comp = ''
for base in seq:
comp = comp + base_map[base]
print(comp)
# or shorthand version
print(''.join(base_map[base] for base in seq))
Here’s a short DNA sequence:
ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT
The sequence contains a recognition site for the EcoRI restriction enzyme,
which cuts at the motif G*AATTC
(the position of the cut is indicated by an
asterisk). Write a program which will calculate the size of the two
fragments that will be produced when the DNA sequence is digested with
EcoRI.
seq = 'ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT'
index = seq.find('GAATTC')
print(f'Frag 1 size: {len(seq[0:index+1])}')
print(f'Frag 2 size: {len(seq[index+1:])}')
seq = 'ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT'
frag1_size = seq.find('GAATTC') + 1
print(frag1_size)
print(len(seq) - frag1_size)
Here’s a short DNA sequence:
ATCGATCGATCGATCGACTGACTAGTCATAGCTATGCATGTAGCTACTC
GATCGATCGATCGATCGATCGATCGATCGATCGATCATGCTATCATCGA
TCGATATCGATGCATCGACTACTAT
It comprises two exons and an intron. The first exon runs from the start of the sequence to the sixty-third character, and the second exon runs from the ninety-first character to the end of the sequence. Write a program that will print just the coding regions of the DNA sequence.
seq = 'ATCGATCGATCGATCGACTGACTAGT'
seq += 'CATAGCTATGCATGTAGCTACTCGATCGATCGATCGA'
seq += 'TCGATCGATCGATCGATCGATCATGC'
seq += 'TATCATCGATCGATATCGATGCATCGACTACTAT'
exon1 = seq[0:62]
exon2 = seq[90:]
print(exon1 + exon2)
Using the data from part one, write a program that will calculate what percentage of the DNA sequence is coding.
seq = 'ATCGATCGATCGATCGACTGACTAGTCATAGCTA'
seq += 'TGCATGTAGCTACTCGATCGATCGATCGA'
seq += 'TCGATCGATCGATCGATCGATCATGCTAT'
seq += 'CATCGATCGATATCGATGCATCGACTACTAT'
print( ( len(seq[0:62]) + len(seq[90:]) ) / len(seq) )
Using the data from part one, write a program that will print out the original genomic DNA sequence with coding bases in uppercase and non-coding bases in lowercase.
seq = 'ATCGATCGATCGATCGACTGACTAGTCATAGC'
seq += 'TATGCATGTAGCTACTCGATCGATCGATCGA'
seq += 'TCGATCGATCGATCGATCGATCATGCTATCA'
seq += 'TCGATCGATATCGATGCATCGACTACTAT'
print( seq[0:62].upper() + seq[63:90].lower() + seq[90:].upper() )