Introduction to Python: Chapter 3 – Reading and writing files

Since I made an error during the live teaching, I re-recorded a walkthrough of the Jupyter notebook, which can be found here.

The Jupyter notebook for Chapter 3 is here. Note the challenge at the end:

Write DNA Fragments to a file

The following code splits a sequence of DNA into two parts (in variables part1 and part2).


# the three parameters of the algorithm
my_dna = 'ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT'
cut_offset = 1
motif = 'GAATTC'

# the algorithm itself
# how long are my DNA fragments?
string_length = len(my_dna)
match_pos = my_dna.find(motif)
cut_position = match_pos + cut_offset
part1 = my_dna[0:cut_position]
part2 = my_dna[cut_position:]

The challenge: write the contents of part1 and part2 into a file called "mydna.txt", with each part on its own line (i.e. part1 followed by part2).