Sequences A sequence is a list of elements



![Adding items to a list a. List = [] # create empty list # Adding items to a list a. List = [] # create empty list #](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-4.jpg)


![passing_lists_to_functions. py Output Top 3 of the grades [10, 5, 11, 5, 13, 5] passing_lists_to_functions. py Output Top 3 of the grades [10, 5, 11, 5, 13, 5]](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-7.jpg)
![passing_lists_to_functions. py Explanation [ , , , ] grades 10 5 11 5 13 passing_lists_to_functions. py Explanation [ , , , ] grades 10 5 11 5 13](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-8.jpg)
![passing_lists_to_functions. py Explanation a [ , , , ] grades 10 5 11 5 passing_lists_to_functions. py Explanation a [ , , , ] grades 10 5 11 5](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-9.jpg)


![top. N. py Output - current top 4: [10, 4, 4, 4] - current top. N. py Output - current top 4: [10, 4, 4, 4] - current](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-12.jpg)









- Slides: 21

Sequences • A sequence is a list of elements • Lists and tuples – Lists mutable – Tuples immutable • Sequence elements can be indexed with subscripts – First index is 0 – Access element with sequence. Name[ subscript ] – Subscripts may be negative – Slicing works same way as with strings 1

Creating Sequences • Creations – Strings • Use quotes: s = "" – Lists • Use brackets • list 1 = [] • list 2 = [ 1, 7, 66 ] – Tuples • • Use parentheses tuple 1 = () Tuple 2 = 5, tuple 3 = ( 19, 27 ) 2

for item in. . ? Two ways of accessing all elements in a sequence startlist = [‘John’, ‘George’, …, ‘Wayne’] # long list for i, name in enumerate( startlist ): # here we get both the subscript and the item: print “%s has index %d” %( name, i ) for name in startlist: # here we get the item directly, but not the subscript print name
![Adding items to a list a List create empty list Adding items to a list a. List = [] # create empty list #](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-4.jpg)
Adding items to a list a. List = [] # create empty list # add values to list for number in range( 1, 11 ): a. List += [ 1. 0/number ] Adds the values to the list (by adding a new list with one element to the existing list in each round) • Using the + operator, you need to add a list to a list: a. List += [ number ] • Using the append method, you add an element to the list: a. List. append( number )

Using Lists and Tuples • Only difference between lists and tuples is: – Lists are mutable, tuples are immutable >>> >>> [3, a. List = [3, 6, 9] a. List[2] = 141 a. List 6, 141] >>> a. Tuple = (3, 6, 9) >>> a. Tuple[2] = 141 Traceback (most recent call last): File "<stdin>", line 1, in ? Type. Error: object doesn't support item assignment 7

passing_lists_to_functions. py Passing a list as an argument to a function • sum and max functions 8
![passingliststofunctions py Output Top 3 of the grades 10 5 11 5 13 5 passing_lists_to_functions. py Output Top 3 of the grades [10, 5, 11, 5, 13, 5]](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-7.jpg)
passing_lists_to_functions. py Output Top 3 of the grades [10, 5, 11, 5, 13, 5] are: [13, 11, 10] Average grade: 5 9
![passingliststofunctions py Explanation grades 10 5 11 5 13 passing_lists_to_functions. py Explanation [ , , , ] grades 10 5 11 5 13](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-8.jpg)
passing_lists_to_functions. py Explanation [ , , , ] grades 10 5 11 5 13 5 10
![passingliststofunctions py Explanation a grades 10 5 11 5 passing_lists_to_functions. py Explanation a [ , , , ] grades 10 5 11 5](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-9.jpg)
passing_lists_to_functions. py Explanation a [ , , , ] grades 10 5 11 5 13 5 11

Better way of finding top. N from a list of numbers Algorithm: Don’t find the max N times: Inefficient! 1. Keep a sorted list of current top-N 2. Go through given list one number at a time 3. If number is greater than current N’th greatest, 4. insert in correct position 12

Better way of finding top. N from a list of numbers Want to compare to top‘s N’th element, so top must be initialized with N values Make sure any value can enter top. N. py top 11 10 5 4 n 5 i 13
![top N py Output current top 4 10 4 4 4 current top. N. py Output - current top 4: [10, 4, 4, 4] - current](https://slidetodoc.com/presentation_image_h/0cd5d8a3b9436b0d19365d9a524cf3f7/image-12.jpg)
top. N. py Output - current top 4: [10, 4, 4, 4] - current top 4: [10, 5, 4, 4] - current top 4: [11, 10, 5, 5] - current top 4: [13, 11, 10, 5] Top 4 of the grades [10, 5, 11, 5, 13, 5] are: [13, 11, 10, 5] Average grade: 8. 17 14

Functions working on lists • • map reduce filter zip 15

longest_string_in_list. py Unelegant way of finding length of longest string Don’t call len twice! 16

longest_string_in_list 2. py Elegant way using map • map: applies given function (first argument) to all elements in given sequence (second argument), returns list with results 17

Sequence unpacking – testing a function gcd_function_test. py • for/else construct 19

gcd_function 2_test. py New implementation of function, same test 20

Dictionaries • Dictionaries – – Mapping that consists of unordered key-value pairs Each value is referenced though its key Curly braces {} are used to create a dictionary Creating a dictionary: { key 1: value 1, … } or: dict( [ (key 1, value 1), …] ) – Keys must be immutable values such as strings, numbers and tuples – Values can be anything – Add item to dictionary d with d[key] = value 21

gcd_function_test. py Dictionary example: DNA dinucleotides List comprehension: Create list of all tuples a, b where a and b are from nuc Dictionary method fromkeys: Create dictionary with keys from the given list. Iterate and all valuess, 0 update dictionary value through for each dinucleotide key Dictionary method iteritems: Iterate through (a sorted copy of) all of d’s Add report string to key/value pairs. list, The keys are tuples, the values histogram are integers. convert to string and return 22

gcd_function_test. py Output Input DNA string: atcagcgatattcgatcagctag AG : 3 AT : 4 CA : 2 CG : 2 CT : 1 GA : 2 GC : 2 TA : 2 TC : 3 TT : 1 23

On to the exercises. . 25