Numbers lists and tuples Genome 559 Introduction to
Numbers, lists and tuples Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble
Numbers • Python defines various types of numbers: • • Integers (1234) Floating point numbers (12. 34) Octal and hexadecimal numbers (0177, 0 x 9 gff) Complex numbers (3. 0+4. 1 j) • You will likely only need the first two. • Python will automatically convert to a float when it needs to. >>> 3/4 0. 75
Lists • A list is an ordered series of objects >>> my_string = “Larry” >>> my_list = [“Larry”, “Curly”, “Moe”] • Lists are • ordered left to right • indexed like strings (from 0) • mutable • heterogeneous >>> list 1 = >>> list 2 = >>> list 3 = >>> list 4 [[0, 1, 2], [0, 1, 2] [‘A’, ‘B’, ‘C’] [‘D’, ‘E’, 3, 4] [list 1, list 2, list 3] [‘A’, ‘B’, ‘C’], [‘D’, ‘E’, 3, 4]]
>>> my_string = 'A'+'T'+'C'+'G' >>> my_string = "ATCG" >>> print(my_string[0]) A >>> print(my_string[-1]) G >>> print(my_string[2: ]) CG >>> my_string * 3 'ATCGATCG' Lists and strings are similar >>> my_string[9] Traceback (most recent call last): File "<stdin>", line 1, in ? Index. Error: string index out of range >>> my_list = ["adenine", "thymine"] + ["cytosine", "guanine"] >>> my_list = ["adenine", "thymine", "cytosine", "guanine"] >>> print(my_list[0]) adenine >>> print(my_list[-1]) guanine >>> print(my_list[2: ]) ['cytosine', 'guanine'] >>> my_list * 3 ['adenine', 'thymine', 'cytosine', 'guanine', 'adenine', 'thymine', 'cytosine', 'guanine'] >>> my_list[9] Traceback (most recent call last): File "<stdin>", line 1, in ? Index. Error: list index out of range
Lists can be changed; strings are immutable. >>> my_string = "ATCG" >>> print(my_string) ATCG >>> my_string[1] = "U" Traceback (most recent call last): File "<stdin>", line 1, in ? Type. Error: object doesn't support item assignment >>> my_string. reverse() Traceback (most recent call last): File "<stdin>", line 1, in ? Attribute. Error: 'str' object has no attribute 'reverse' >>> my_list = ["adenine", "thymine", "cytosine", "guanine"] >>> print(my_list) ['adenine', 'thymine', 'cytosine', 'guanine'] >>> my_list[1] = "uracil" >>> print(my_list) ['adenine', 'uracil', 'cytosine', 'guanine'] >>> my_list. reverse() >>> print(my_list) ['guanine', 'cytosine', 'uracil', 'adenine'] >>> del my_list[0] >>> print(my_list) ['cytosine', 'uracil', 'adenine']
del() versus remove() • The del operator deletes one object from a list. • The remove operator looks for and removes a given object in the list. • The object to be deleted is specified by index. >>> my_list. remove(3) >>> my_list = [1, 2, 3] >>> [1, my_list 2, 3] del my_list[1] my_list 3] • It is an error to delete beyond the end of the list. >>> del my_list[3] Traceback (most recent call last): File "<stdin>", line 1, in <module> Index. Error: list assignment index out of range >>> my_list [1] • It is an error to try to remove an object that is not there. >>> my_list. remove(2) Traceback (most recent call last): File "<stdin>", line 1, in <module> Value. Error: list. remove(x): x not in list
More list operations and methods >>> my_list = ["thymine", "cytosine", "guanine"] >>> my_list. insert(0, "adenine") >>> print(my_list) ['adenine', 'thymine', 'cytosine', 'guanine'] >>> my_list. insert(2, "uracil") >>> print(my_list) ['adenine', 'thymine', 'uracil', 'cytosine', 'guanine'] >>> print(my_list[: 2]) ['adenine', 'thymine'] >>> my_list[: 2] = ["A", "T"] >>> print(my_list) ['A', 'T', 'uracil', 'cytosine', 'guanine'] >>> my_list[: 2] = [] >>> print(my_list) ['uracil', 'cytosine', 'guanine'] >>> my_list = [‘A’, ‘T’, ‘C’, ‘G’] >>> my_list. index('C') 2 >>> my_list. remove('C') >>> print(my_list) ['A', 'T', 'G'] >>> last = my_list. pop() >>> print(last) 'G' >>> print(my_list) ['A', 'T']
>>> my_list = [] # make an empty list >>> print(my_list) [] >>> my_list. append("Hello!") # append means "add to the end" >>> print(my_list) ['Hello!'] >>> my_list. append(5) >>> print(my_list) ['Hello!', 5] >>> my_list. append([9, 8, 7]) # append the list to end of list >>> print(my_list) ['Hello!', 5, [9, 8, 7]] >>> my_list. extend([4, 5, 6]) # extend means append each element >>> print(my_list) ['Hello!', 5, [9, 8, 7], 4, 5, 6] >>> print(my_list)[2][0] [9, 8, 7] 9 Methods for expanding lists
Turn a string into a list <string>. split(x) or list(S) >>> protein = "ALA PRO ILE CYS" >>> residues = protein. split() # split() uses whitespace >>> print(residues) ['ALA', 'PRO', 'ILE', 'CYS'] >>> list(protein) # list explodes each char ['A', 'L', 'A', 'P', 'R', 'O', 'I', 'L', 'E', 'C', 'Y', 'S'] >>> print(protein. split()) ['ALA', 'PRO', 'ILE', 'CYS'] >>> protein 2 = "HIS-GLU-PHE-ASP" >>> protein 2. split("-") #split using a delimiter ['HIS', 'GLU', 'PHE', 'ASP']
Turn a list into a string join is the opposite of split <delimiter>. join(L) >>> L 1 = ["Asp", "Gly", "Gln", "Pro", "Val"] >>> print("-". join(L 1)) Asp-Gly-Gln-Pro-Val >>> print("**". join(L 1)) The order is confusing. Asp**Gly**Gln**Pro**Val - String to join with is first. >>> L 2 = "n". join(L 1) >>> L 2 - List to be joined is second. 'Aspn. Glyn. Glnn. Pron. Val' >>> print(L 2) Asp Gly Gln Pro Val
>>> T = (1, 2, 3, 4) >>> T*4 (1, 2, 3, 4, 1, 2, 3, 4) >>> T + T (1, 2, 3, 4, 1, 2, 3, 4) >>> T (1, 2, 3, 4) >>> T[1] = 4 Traceback (most recent call last): File "<stdin>", line 1, in ? Type. Error: object doesn't support item assignment >>> x = (T[0], 5, "eight") >>> print(x) (1, 5, 'eight') >>> y = list(x) # converts a tuple to a list >>> print(y. reverse()) ('eight', '5', '1') >>> z = tuple(y) # converts a list to a tuple Tuples: immutable lists Tuples are immutable. Why? • Sometimes you want to guarantee that a list won’t change.
Basic list operations: L = ['dna', 'rna', 'protein'] L 2 = [1, 2, 'dogma', L] L 2[2] = 'central' L 2[0: 2] = 'ACGT' del L[0: 1] = 'nucs' L 2 + L L 2*3 L[x: y] len(L) ''. join(L) S. split(x) list(S) list(T) # list assignment # list hold different objects # change an element (mutable) # replace a slice # delete a slice # concatenate # repeat list # define the range of a list # length of list # convert a list to string # convert string to list- x delimited # convert string to list - explode # converts a tuple to list Methods: L. append(x) L. extend(x) L. count(x) L. index(x) L. insert(i, x) L. remove(x) L. pop(i) L. reverse() L. sort() # add to the end # append each element from x to list # count the occurences of x # give element location of x # insert at element x at element i # delete first occurrence of x # extract element I # reverse list in place # sort list in place
Sample problem #1 • Write a function called dna_composition that takes a DNA sequence as input and prints the number of A’s, C’s, G’s and T’s. dna_composition(“ACGTGCGTTAC”) 2 A’s 3 C’s 3 G’s 3 T’s
Solution #1
Solution #1
Sample problem #2 • The melting temperature of a primer sequence can be estimated as T = 2 * (# of A or T nucleotides) + 4 * (# of G or C nucleotides) • Write a function melting_temperature that computes the melting temperature of a given DNA sequence. melting_temperature(“ACGGTCA”) 22
Solution #2
Solution #2
Solution #2
Sample problem #3 (optional) • Write a function my_reverse that removes the first string from a given list and then prints the remaining strings in reverse order with asterisks in between. my_reverse([“null”, “A”, “B”, C”]) C*B*A
Solution #3
Reading • Chapters 10 and 12 of Think Python (1 st edition) by Allen B. Downey.
- Slides: 22