COP 3223H meeting -*- Outline -*- * Sequences in Python This unit is about another way to process collections of data, specifically about loops. But to give examples of that, we will use certain kinds of random-access data structures, specifically lists, which are similar to arrays in C/C++/Java etc. strings, tuples, and ranges are very useful in Python, but not so important for our purposes. ** concept of sequences in Python ------------------------------------------ SEQUENCES IN PYTHON A concept, or application programming interface (API), Idea: Main built in sequence types: lists, strings, tuples, ranges lists include strings Subtypes of collections.abc.Sequence ------------------------------------------ ... for treating random-access collections of elements Random access means each element can be accessed in a constant amount of time. Q: How long does it take to access the 10th element of a LispList? need to do call tail() 10 times and then call first() Q: How long does it take to access the first element of a LispList? just need to call first() Random access means that there should be no difference in the time needed to access the first or 10th element of a sequence ... for a sequence of length n, the elements are in index positions 0, 1, 2, ..., n-1 (draw pictures of an array and a mapping) ------------------------------------------ COMMON OPERATIONS ON SEQUENCE OBJECTS len(), e.g., len("hi") indexing, e.g., "hi"[1] membership test, e.g., 'i' in "hi" slicing, e.g., "Drump"[1:4] concatenation, e.g., "hi" + " there" ------------------------------------------ ... == 2 ... == 'i' ... == True ... == "rum" note how 'T' is at index 0, and 'p' is at index 4, but the slice covers indexes 1, 2, and 3 (not 4) so in general the slice [beg,end] goes from indexes beg, beg+1, ..., end-1 and covers the indexes in {i | beg <= i and i < end} ... == "hi there" Q: Why isn't the ending index included in a slice? because of Python's use of 0-based indexing. consider the floors in a building and European numbering ** tuples ------------------------------------------ PYTHON TUPLES Immutable collections of possibly heterogeneous elements Construction: >>> etup = () >>> etup >>> rbwo = ("RBWO", 5, True, [3, 4]) >>> rbwo Indexing: >>> rbwo[0] ------------------------------------------ ... 'RBWO' ** ranges these are useful for "for"-loops ------------------------------------------ PYTHON RANGES immutable sequences of numbers Construction: >>> range(5) >>> to6 = range(0,6) >>> to6 Indexing: >>> to6[0] >>> to6[1] >>> to6[5] ------------------------------------------ ... range(0,5) ... range(0,6) ... 0 ... 1 ... 5 note that to6[6] is an error ** lists ------------------------------------------ PYHTHON'S BUILT-IN LISTS Finite, mutable sequences of elements Construction: list elements in square brackets: >>> emp = [] >>> emp >>> lst012 = [0,1,2] >>> lst012 >>> lst3223 = [1+2, 0+2, 1+1, 2+1] >>> lst3223 >>> list(range(0,5)) Indexing: >>> lst3223[0] >>> lst3223[1] >>> lst3223[3] Slicing: >>> lst3223[1:4] >>> lst3223[0:3] Concatenation: >>> lst012 + lst3223 ------------------------------------------ ... [] ... [0, 1, 2] ... [3, 2, 2, 3] ... [0, 1, 2, 3, 4, 5] ... 3 ... 2 ... 3 ... [2, 2, 3] ... [3, 2, 2] ... [0, 1, 2, 3, 2, 2, 3] Unlike strings (and tuples), in Python you can assign to the elements of a list ------------------------------------------ PYTHON LISTS ARE MUTABLE >>> two = 2 >>> lst3223 = [1+two, 0+two, 1+1, two+1] >>> lst3223[0] = 5 >>> lst3223 >>> lst3223[1] = 4 >>> lst3223 ------------------------------------------ ... [5, 2, 2, 3] ... [5, 4, 2, 3]