Title: Numbers, lists and tuples
1Numbers, lists and tuples
- Genome 559 Introduction to Statistical and
Computational Genomics - Prof. William Stafford Noble
2Numbers
- Python defines various types of numbers
- Integers (1234)
- Floating point numbers (12.34)
- Octal and hexadecimal numbers (0177, 0x9gff)
- Complex numbers (3.04.1j)
- You will likely only need the first two.
3Conversions
- gtgtgt 6/2
- 3
- gtgtgt 3/4
- 0
- gtgtgt 3.0/4.0
- 0.75
- gtgtgt 3/4.0
- 0.75
- gtgtgt 34
- 12
- gtgtgt 3.04.0
- 12.0
- The result of a mathematical operation on two
numbers of the same type is a number of that
type. - The result of an operation on two numbers of
different types is a number of the more complex
type. - integer ? float
- float ? complex
4Formatting numbers
- The operator formats a number.
- The syntax is
- ltformat stringgt ltnumbergt
- gtgtgt f 3
- 3.000000
- gtgtgt .2f 3
- 3.00
- gtgtgt 5.2f 3
- 3.00
5Conversion codes
- d integer
- f decimal value
- e scientific notation
- g easily readable notation (i.e., use decimal
notation unless there are too many zeroes, then
switch to scientific notation)
6More complex conversions
d, f, e, g
Number of digits after decimal
Total width of field
Left justify (-) Include numeric sign
() Fill in with zeroes (0)
7Examples
- gtgtgt x 7718
- gtgtgt d x
- 7718
- gtgtgt -6d x
- 7718
- gtgtgt 06d x
- 007718
- gtgtgt x 1.23456789
- gtgtgt d x
- 1
- gtgtgt f x
- 1.234568
- gtgtgt e x
- 1.234568e00
- gtgtgt g x
- 1.23457
- gtgtgt g (x 10000000)
- 1.23457e07
8Lists
- A list is an ordered string of objects
- gtgtgt myString Hillary
- gtgtgt myList Hillary, Barack, John
- Lists are
- ordered left to right
- indexed like strings (from 0)
- mutable
- heterogeneous
- gtgtgt list1 0, 1, 2
- gtgtgt list2 A, B, C
- gtgtgt list3 D, E, 3, 4
- gtgtgt list4 list1, list2, list3
- gtgtgt list4
- 0, 1, 2, A, B, C, D, E, 3, 4
9Lists and strings are similar
- Lists
- gtgtgt L "adenine", "thymine" "cytosine",
"guanine" - gtgtgt L "adenine", "thymine", "cytosine",
"guanine" - gtgtgt print L0
- adenine
- gtgtgt print L-1
- guanine
- gtgtgt print L2
- 'cytosine', 'guanine'
- gtgtgt L 3
- 'adenine', 'thymine', 'cytosine', 'guanine',
'adenine', 'thymine', 'cytosine', 'guanine',
'adenine', 'thymine', 'cytosine', 'guanine' - gtgtgt L9
- Traceback (most recent call last)
- File "ltstdingt", line 1, in ?
- IndexError list index out of range
- Strings
- gtgtgt s 'A''T''C''G'
- gtgtgt s "ATCG"
- gtgtgt print s0
- A
- gtgtgt print s-1
- G
- gtgtgt print s2
- CG
- gtgtgt s 3
- 'ATCGATCGATCG'
- gtgtgt s9
- Traceback (most recent call last) File
"ltstdingt", line 1, in ? IndexError string index
out of range
10Lists can be changedstrings are immutable.
- Lists
- gtgtgt L "adenine", "thymine", "cytosine",
"guanine" - gtgtgt print L
- 'adenine', 'thymine', 'cytosine', 'guanine'
- gtgtgt L1 "uracil"
- gtgtgt print L
- 'adenine', 'uracil', 'cytosine', 'guanine'
- gtgtgt L.reverse()
- gtgtgt print L
- 'guanine', 'cytosine', 'uracil', 'adenine'
- gtgtgt del L0
- gtgtgt print L
- 'cytosine', 'uracil', 'adenine'
- Strings
- gtgtgt s "ATCG"
- gtgtgt print s
- ATCG
- gtgtgt s1 "U"
- Traceback (most recent call last)
- File "ltstdingt", line 1, in ?
- TypeError object doesn't support item assignment
- gtgtgt s.reverse()
- Traceback (most recent call last)
- File "ltstdingt", line 1, in ?
- AttributeError 'str' object has no attribute
'reverse'
11More list operations and methods
- gtgtgt L "thymine", "cytosine", "guanine"
- gtgtgt L.insert(0, "adenine")
- gtgtgt print L
- 'adenine', 'thymine', 'cytosine', 'guanine'
- gtgtgt L.insert(2, "uracil")
- gtgtgt print L
- 'adenine', 'thymine', 'uracil', 'cytosine',
'guanine' - gtgtgt print L2
- 'adenine', 'thymine'
- gtgtgt L2 "A", "T"
- gtgtgt print L
- 'A', 'T', 'uracil', 'cytosine', 'guanine'
- gtgtgt L2
- gtgtgt print L
- 'uracil', 'cytosine', 'guanine'
- gtgtgt L A, T, C, G
- gtgtgt L.index('C')
- 2
- gtgtgt L.remove('C')
12Methods for expanding lists
- gtgtgt data make an empty list
- gtgtgt print data
-
- gtgtgt data.append("Hello!") append means "add to
the end" - gtgtgt print data
- 'Hello!'
- gtgtgt data.append(5)
- gtgtgt print data
- 'Hello!', 5
- gtgtgt data.append(9, 8, 7) append the list to
end of list - gtgtgt print data
- 'Hello!', 5, 9, 8, 7
- gtgtgt data.extend(4, 5, 6) extend means append
each element - gtgtgt print data
- 'Hello!', 5, 9, 8, 7, 4, 5, 6
- gtgtgt
- gtgtgt print data2
- 9, 8, 7
- gtgtgt print data20
13Turn a string into a list
- ltstringgt.split(x) or use list(S)
- gtgtgt protein "ALA PRO ILE CYS"
- gtgtgt residues protein.split() split() uses
whitespace - gtgtgt print residues
- 'ALA', 'PRO', 'ILE', 'CYS'
- gtgtgt list(protein) list explodes each char
- 'A', 'L', 'A', ' ', 'P', 'R', 'O', ' ', 'I',
'L', 'E', ' ', 'C', 'Y', 'S' - gtgtgt print protein.split()
- 'ALA', 'PRO', 'ILE', 'CYS'
- gtgtgt protein2 "HIS-GLU-PHE-ASP"
- gtgtgt protein2.split("-") split using a
delimiter - 'HIS', 'GLU', 'PHE', 'ASP'
14Turn a list into a string
- join is the opposite of split
- ltdelimitergt.join(L)
- gtgtgt L1 "Asp", "Gly", "Gln", "Pro", "Val"
- gtgtgt print "-".join(L1)
- Asp-Gly-Gln-Pro-Val
- gtgtgt print "".join(L1)
- AspGlyGlnProVal
- gtgtgt L2 "\n".join(L1)
- gtgtgt L2
- 'Asp\nGly\nGln\nPro\nVal'
- gtgtgt print L2
- Asp
- Gly
- Gln
- Pro
- Val
The order is confusing. - String to join with
is first. - List to be joined is second.
15Tuples immutable lists
- Tuples are immutable.
- Why? Sometimes you want to guarantee that a list
wont change. - Tuples support operations but not methods.
- gtgtgt T (1,2,3,4)
- gtgtgt T4
- (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
- gtgtgt T T
- (1, 2, 3, 4, 1, 2, 3, 4)
- gtgtgt T
- (1, 2, 3, 4)
- gtgtgt T1 4
- Traceback (most recent call last)
- File "ltstdingt", line 1, in ?
- TypeError object doesn't support item assignment
- gtgtgt x (T0, 5, "eight")
- gtgtgt print x
- (1, 5, 'eight')
- gtgtgt y list(x) converts a tuple to a list
16- Basic list operations
- L 'dna','rna','protein' list assignment
- L2 1,2,'dogma',L list hold different
objects - L22 'central' change an element (mutable)
- L202 'ACGT' replace a slice
- del L01 'nucs' delete a slice
- L2 L concatenate
- L23 repeat list
- Lxy define the range of a list
- len(L) length of list
- ''.join(L) convert a list to string
- S.split(x) convert string to list- x
delimited - list(S) convert string to list - explode
- list(T) converts a tuple to list
- Methods
- L.append(x) add to the end
- L.extend(x) append each element from x to
list - L.count(x) count the occurences of x
17Sample problem 1
- Write a program called dna-composition.py that
takes a DNA sequence as the first command line
argument and prints the number of As, Cs, Gs
and Ts. - gt python dna-composition.py ACGTGCGTTAC
- 2 As
- 3 Cs
- 3 Gs
- 3 Ts
18Solution 1
- import sys
- sequence sys.argv1.upper()
- print sequence.count(A), As
- print sequence.count(C), Cs
- print sequence.count(G), Gs
- print sequence.count(T), Ts
19Sample 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 program melting-temperature.py that
computes the melting temperature of a given DNA
sequence. - gt python melting-temperature.py ACGGTCA
- 22
20Solution 2
- import sys
- sequence sys.argv1.upper()
- numAs sequence.count('A')
- numCs sequence.count('C')
- numGs sequence.count('G')
- numTs sequence.count('T')
- temp (2 (numAs numTs)) (4 (numGs
numCs)) - print temp
21Sample problem 3 (optional)
- The object sys.argv is a list of strings.
- Write a program reverse-args.py that removes the
program name from the beginning of this list and
then prints the remaining command line arguments
in reverse order with asterisks in between. - gt python reverse-args.py 1 2 3
- 321
22Solution 3
- import sys
- args sys.argv1
- args.reverse()
- print "".join(args)
23Reading
- Chapters 9-12 of Learning Python (3rd edition) by
Lutz.