Lists
Lets make a list:
Execute the code by pressing ctrl + enter
Name: my_list
Value: []
Write your code to make lists:
empty list which can hold 4 items
list called molecules containing elements: DNA, RNA, Protein and Lipid
containing 4 sequences
containing at least 1 string, 1 integer, and 1 boolean
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-10-20b0201bec85> in <module>()
1 print molecules[1:].index("DNA")
----> 2 print molecules[1:2].index("DNA")
ValueError: 'DNA' is not in list
Accessing Lists
Using the lists that you have created, check what is stored at index 1 of the list 'molecules'
Lists – Exercise
Create the following 3 string variables with their restriction sites:
eco_r1 : gaattc
bam_h1 : ggatcc
hind3 : aagctt
Make a list called restriction_enzymes containing the 3 restriction sites.
Using a single command print the restriction sites in the list on 3 different lines.
Print the first letter of eco_r1 using the list.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-59eeb8ac5c35> in <module>()
9 print restriction_enzymes
10
---> 11 restriction_enzymes[0][0] = restriction_enzymes[0][0].upper()
TypeError: 'str' object does not support item assignment
Basic List operations
Adding (concatenate) two lists
Repeating items in list
Compare two lists: cmp(L1, L2)
Length of a list: len(L1)
Maximum value: max(L1)
Minimum value: min(L1)
Basic List operations - Exercise
Remember that we made a list of restriction enzymes as: restriction_enzymes = [eco_r1, bam_h1, hind3]. Create a new variable my_enzyme = 'catgac’
Make a new list with my_enzyme added to the existing list of restriction enzymes.
Make a new list containing 3 repetitions of eco_r1, 4 repetitions of bam_h1, and 2 repetitions of hind3.
What is the length of the lists you just created?
What is the maximum value of the list?
What is the minimum value of the list?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-573ede14846e> in <module>()
15 print restriction_enzymes
16
---> 17 restriction_enzymes = restriction_enzymes + my_enzyme
18 # Create list with repeated entries and find its length
TypeError: can only concatenate list (not "str") to list
Looping Lists - Exercise
Calculate the number of nucleotides (A, T, G, and C) used by each restriction enzyme?
Calculate the total number of nucleotides used by all restriction enzymes taken together?
Use the list entry seqs given in the worksheet. How many DNA and RNA sequences are given in seqs? (Hint: try for loop, if elif conditions and string find command)
Remember EcoRI cuts at restriction site 'gaattc'. Can you check how many sequences given in list seqs have EcoRI motif?
Bonus Question: Make a list containing ['A', 'AA', 'AAA', 'AAAA', ….., 20 A].
Modifying Lists
Take list seqs and add 3 random DNA/RNA sequences at the end of the list
Take list seqs and add 'atcg' as the third sequence of the list
Create a new sorted list seq_sort from seqs
Modifying Lists
Remove 'atcg' sequence of the list seqs
Print 5th to 10th sequence entry from the lists seqs and seq_sort
Converting strings to Lists
Two essential command of list conversion are:
Create a list from a string: StringName.split()
Create a string from lists: ' '.join(ListName)
Split 'atgcatgcatgc' sequence using split() command
Create a list by splitting 'atgcatgcatgc' sequence using 't' as the separator.
Tuples
Tuples are Python sequence but unlike lists they are immutable. Tuples use parentheses and lists use square brackets.
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = ('a', 'b', 'c', 'd')
Accessing tuples:
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]
Try to assign tuples:
tup1[2] = 2015
tup4 = tup2 + tup3
Tuples Exercise
Suppose you want to find distance between Carbon atoms in a protein sequence.
Assign 4 tuples each containing the 3D co-ordinates:
X | Y | Z | |
---|---|---|---|
C29 | 0.55 | 1.256 | -1.153 |
C103 | 1.28 | -1.72 | 1.24 |
C115 | 1.59 | 1.73 | 1.04 |
C118 | 1.36 | -1.86 | -1.74 |
Calculate square distance between different carbon atoms. Hint use: (x1-x2)^2+(y1-y2)^2+(z1-z2)^2