In [10]:
message = "Hello world"
print ("My message is:", message)
Vanilla use of print
In [37]:
message = "Hello world"
print ("My 1st message is:", message)
print ("My 2nd message is: %s" % message)
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.
In [13]:
message = "Hello world"
print ("My 1st message is:", message, end=(". "))
print ("My 2nd message is: %s " % message)
Python adds a newline character (\n) after every print statement by default. You can suppress this by adding the argument: end=""
In [39]:
Name = "Romissa"
Donut_number = 30
print("%s has %d donuts" % (Name, Donut_number))
%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)
In [40]:
import datetime
date = datetime.date.today()
a = str(date)
b = repr(date)
print(a, end=" ")
print(b)
print("%s %r" % (date, date))
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
In [14]:
seq = input("Enter a DNA sequence:").upper()
print(seq + seq)
In [41]:
x = 5
y = 10
print("Result 1 = " , x * y )
print("Result 2 = " , x ** y ) # ** - meaning “to the power of”
print("Result 3 = , x ** y ")
Small mistakes can be difficult to spot. You will avoid and spot these mistakes with practice.
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
In [42]:
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)
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
In [43]:
names = ["Andy", "Bob", "Chris"]
removed_name = names.pop(0)
print (removed_name)
print(names)
The opposite of list.pop(index) is list.insert(index,value)
In [44]:
numbers = []
for i in range(1,24,3):
numbers.append(i)
print(numbers)
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 are a lookup key-value system:
Simple to declare:
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
In [45]:
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
In [46]:
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
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
In [47]:
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])
The two print statements illustrate alternatives
When using multiple formatters, variables are provided in order within brackets
Be careful with multiple parentheses!
In [6]:
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())
Out[6]:
The keys() method returns a view of the keys which can be looped through like a list or turned into a list
In [1]:
student_records = {20071213: "Andy Jones",
20081423 : "John Smith",
20096137 :"Jane Doe"}
del student_records[20081423]
print("Total number of students = " , len(student_records.keys()))
In [2]:
student_records = {20071213: "Andy Jones",
20081423 : "John Smith",
20096137 :"Jane Doe"}
for name in student_records.keys():
print(name, ":", student_records[name])
Here we setup a temporary key (called name) and then loop through every key, retrieving the corresponding value for each key
In [3]:
student_records = {20071213: "Andy Jones",
20081423 : "John Smith",
20096137 :"Jane Doe"}
for name in student_records.keys():
print(name, ":", student_records[name])
Here we setup a temporary key (called name) and then loop through every key, retrieving the corresponding value for each key
Slicing is very useful for manipulating strings and lists
In [10]:
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=" ")
In [5]:
DNA = "ACTGATCGACTGATCGATCGA"
print("Index for CGA : ", DNA.index( "CGA" ))
print("Index for GAT : ", DNA.index( "GAT" ))
Remember – the numbering starts from 0!
In [17]:
codons = ["ATG", "GAC", "TTG"]
print("Index for TTG : ", codons.index( "TTG" ))
print("Index for ATG : ", codons.index( "ATG" ))
Remember – the numbering starts from 0!
In [13]:
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)
In [14]:
Output will be different every time
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
In [16]:
s = "-"
seq = ["a", "b", "c"] # This is sequence of strings.
print (seq)
print (s.join( seq ))