<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Dictionaries, lists and looping structures
Overview
Recap
Step1:  Vanilla use of print
Step2:  The second print uses a “formatter” (%s, %r and %d) 
which provide an alternative way to print by replacing the symbol with the desired variable after a %. 
When printing many variables in a single statement they can be much more efficient.
Step3:  Python adds a newline character (\n) after every print statement by default. 
You can suppress this by adding the argument
Step4:  %s is intended for string substitution whereas %d is for integers. r% converts using the repr() function rather than str() or int() and is useful when you want to return something in valid Python syntax (see next)
Step5:  Don’t worry about datetime, it’s just to illustrate the difference between repr() and str() or %s and %d
Think about why there are three print statements but only two lines of output
Step6:  Recap on maths
Step7:  Small mistakes can be difficult to spot. You will avoid and spot these mistakes with practice.
Recap on lists
A list stores an ordered list of values
Loop through all elements using for
Useful list functions
Step8:  Two common ways to access elements in an list
Step9:  The opposite of list.pop(index) is list.insert(index,value)
Adding to lists in aloop
Step10:  A list needs to be created (a.k.a. initialised) before you can apply methods to it. 
Initialising the list before running the loop is important. If we had tried to initialise within the loop structure the list would be reset after each iteration of the loop!
Dictionaries
Dictionaries are a lookup key-value system
Step11:  Dictionary advanced
Step12:  Sometimes you’ll find you need a data structure more complex than a dictionary
More advanced data structures can be created by combining simpler structures e.g. a dictionary of lists
Codon table
Step13:  The two print statements illustrate alternatives
When using multiple formatters, variables are provided in order within brackets
Be careful with multiple parentheses! 
Get dictionary size – keys method
Step14:  The keys() method returns a view of the keys which can be looped through like a list or turned into a list
delete a record from a dictionary
Step15:  Here we setup a temporary key (called name) and then loop through every key, retrieving the corresponding value for each key
Loop through a dictionary with for
Step16:  Here we setup a temporary key (called name) and then loop through every key, retrieving the corresponding value for each key
Manipulating strings
Slicing is very useful for manipulating strings and lists
Step17:  The index method can be used to find the position of specific substrings
Step18:  Remember – the numbering starts from 0!
index() and slicing can also be used with lists
Step19:  Remember – the numbering starts from 0!
Incrementally adding to a string
Step20:  Don’t worry about the random bit. It’s just an example of some useful code that’s already been written. It’s more important to understand what the rest of the code is doing than how random.choice() works at this stage.
Randomly generated DNA sequence can be very useful – e.g. if you are trying to establish whether a certain pattern occurs more often than expected by chance
<ASSISTANT_TASK:>
Python Code:
message = "Hello world"
print ("My message is:", message)
Explanation: Dictionaries, lists and looping structures
Overview
Recap:
Dictionary
Structure for storing values with keys
List
Structure for storing ordered values
Loop (for/while)
Allows iterative processing (e.g. line-by-line)
Recap on strings, printing etc...
End of explanation
message = "Hello world"
print ("My 1st message is:", message)
print ("My 2nd message is: %s" % message)
Explanation: Vanilla use of print
End of explanation
message = "Hello world"
print ("My 1st message is:", message, end=(". "))
print ("My 2nd message is: %s " % message)
Explanation: The second print uses a “formatter” (%s, %r and %d) 
which provide an alternative way to print by replacing the symbol with the desired variable after a %. 
When printing many variables in a single statement they can be much more efficient.
End of explanation
Name = "Romissa"
Donut_number = 30
print("%s has %d donuts" % (Name, Donut_number))
Explanation: Python adds a newline character (\n) after every print statement by default. 
You can suppress this by adding the argument: end=""
More on strings, printing etc...
End of explanation
import datetime
date = datetime.date.today()
a = str(date)
b = repr(date)
print(a, end=" ")
print(b)
print("%s %r" % (date, date))
Explanation: %s is intended for string substitution whereas %d is for integers. r% converts using the repr() function rather than str() or int() and is useful when you want to return something in valid Python syntax (see next)
End of explanation
seq = input("Enter a DNA sequence:").upper()
print(seq + seq)
Explanation: Don’t worry about datetime, it’s just to illustrate the difference between repr() and str() or %s and %d
Think about why there are three print statements but only two lines of output
End of explanation
x = 5
y = 10
print("Result 1 = " , x * y )
print("Result 2 = " , x ** y ) # ** - meaning “to the power of”
print("Result 3 =  , x ** y  ")
Explanation: Recap on maths
End of explanation
values = [5, 7, 4, 6, 1, 2]
print (values)
print(values[3]) #By the position
for temp_value in values: #Using a loop
    print (temp_value)
Explanation: Small mistakes can be difficult to spot. You will avoid and spot these mistakes with practice.
Recap on lists
A list stores an ordered list of values
Loop through all elements using for
Useful list functions:
extend(list) - adds a list to the end of a list
append(value) - adds a value to the end of a list
Also insert(index, value) and pop(index)
sort 
sorted_names = sorted(names)  #using the function
names.sort()                  #using the method
Recap on list methods
End of explanation
names = ["Andy", "Bob", "Chris"]
removed_name = names.pop(0)
print (removed_name)
print(names)
Explanation: Two common ways to access elements in an list:
By the position (a.k.a. index)  -  values(2)
or 
Use a loop to grab every value and put it into a temporary variable (here called temp_value
Recap on list methods
End of explanation
numbers = []
for i in range(1,24,3):
    numbers.append(i)
print(numbers)
Explanation: The opposite of list.pop(index) is list.insert(index,value)
Adding to lists in aloop
End of explanation
student_records = {20071213: "Alistair Darby"} # declaring a dictionary 
student_records[20081423] = "John Smith"   # adding some more records
student_records[20096137] = "Jane Doe"
student_records[20109334] = "Fred Blogs"
print(student_records[20081423])           # printing a specific record
Explanation: A list needs to be created (a.k.a. initialised) before you can apply methods to it. 
Initialising the list before running the loop is important. If we had tried to initialise within the loop structure the list would be reset after each iteration of the loop!
Dictionaries
Dictionaries are a lookup key-value system:
Very similar to a list except the index is replaced by a defined key
The keys and values can be anything you like.
However, keys must be unique.
Simple to declare:
A variable: Value = 10
A list: Values = [10,11,12,13]
A dictionary: Value_lookup = {"Apple" : "Pie", "Banana" : "split", 3 : 60}
You can retrieve elements from a list using their numerical index whereas a dictionary can use any value specified by the programmer.
Declaring a dictionary requires curly braces {} and a colon : between key and value
Dictionary example
End of explanation
student_records = {20071213: ["Alistair Darby", "A.C. Darby"]}
student_records[20081423] = ["John Smith", "J. L. Smith"]
student_records[20096137] = ["Jane Doe", "J. P. Doe"]
student_records[20109334] = ["Fred Blogs","Frederick Blogs","F. J. Blogs"]
print(student_records[20109334])        # print the list for this specific key
for name in student_records[20109334]:  # iterate through the list and print 
    print(name, end=" ")                # each value
Explanation: Dictionary advanced
End of explanation
codons = {
       "ATT" : "Ile",
       "ATC" : "Ile",
       "ATA" : "Ile",
       "CTT" : "Leu",
       "CTC" : "Leu",
       "CTA" : "Leu",
       "CTG" : "Leu",
     #... And so on...
        }
Codon_1 = "CTC"
print("%s encodes %s" % (Codon_1, codons[Codon_1]))
print(Codon_1, "encodes", codons[Codon_1])
Explanation: Sometimes you’ll find you need a data structure more complex than a dictionary
More advanced data structures can be created by combining simpler structures e.g. a dictionary of lists
Codon table
End of explanation
student_records = {20071213: "Andy Jones"}
student_records[20081423] = "John Smith"
student_records[20096137] = "Jane Doe"
print("Total number of students =" , len(student_records.keys()))
print(student_records.keys()) 
list(student_records.keys())
Explanation: The two print statements illustrate alternatives
When using multiple formatters, variables are provided in order within brackets
Be careful with multiple parentheses! 
Get dictionary size – keys method
End of explanation
student_records = {20071213: "Andy Jones", 
                   20081423 : "John Smith", 
                   20096137 :"Jane Doe"}
del student_records[20081423]
print("Total number of students = " , len(student_records.keys()))
student_records = {20071213: "Andy Jones", 
                    20081423 : "John Smith", 
                    20096137 :"Jane Doe"}
for name in student_records.keys():
     print(name, ":", student_records[name])
Explanation: The keys() method returns a view of the keys which can be looped through like a list or turned into a list
delete a record from a dictionary
End of explanation
student_records = {20071213: "Andy Jones", 
                    20081423 : "John Smith", 
                    20096137 :"Jane Doe"}
for name in student_records.keys():
     print(name, ":", student_records[name])
Explanation: Here we setup a temporary key (called name) and then loop through every key, retrieving the corresponding value for each key
Loop through a dictionary with for
End of explanation
DNA = "ACTGATCGACTGATCGATCGA"
for index in range(0, len(DNA), 3): # Remember - range requires the arguments (start, stop,  step). Step has a default value of 1 
    chunk = DNA[index:index+3] # Remember - you can slice a string by giving [start:stop] indexes
    print(chunk,end=" ")
Explanation: Here we setup a temporary key (called name) and then loop through every key, retrieving the corresponding value for each key
Manipulating strings
Slicing is very useful for manipulating strings and lists
End of explanation
DNA = "ACTGATCGACTGATCGATCGA"
print("Index for CGA : ", DNA.index( "CGA" ))
print("Index for GAT : ", DNA.index( "GAT" ))
Explanation: The index method can be used to find the position of specific substrings
End of explanation
codons = ["ATG", "GAC", "TTG"]
print("Index for TTG : ", codons.index( "TTG" ))
print("Index for ATG : ", codons.index( "ATG" ))
Explanation: Remember – the numbering starts from 0!
index() and slicing can also be used with lists
End of explanation
import random                        #Import the random module
dna = "GCTAGCTACGTACGATCGT"          #Starting string
for i in range(0,10):                #Loop to 10 starting from 0
    dna += random.choice("CGTA")     #Choose a random base and add it
print(dna)
Output will be different every time
Explanation: Remember – the numbering starts from 0!
Incrementally adding to a string
End of explanation
s = "-"
seq = ["a", "b", "c"] # This is sequence of strings.
print (seq)
print (s.join( seq ))
Explanation: Don’t worry about the random bit. It’s just an example of some useful code that’s already been written. It’s more important to understand what the rest of the code is doing than how random.choice() works at this stage.
Randomly generated DNA sequence can be very useful – e.g. if you are trying to establish whether a certain pattern occurs more often than expected by chance
End of explanation
<END_TASK>