61A Lecture 10Wednesday, September 21Strings are an Abstraction2Representing data:'200' '1.2e-5' 'False' '(1, 2)'Representing language:"""O! methinks how slow This old moon wanes; she lingers my desires ,Like to a step dame, or a dowager Long withering out a young man's revenue."""Representing programs:'curry = lambda f: lambda x: lambda y: f(x, y)'Representing Strings: the ASCII Standard3American Standard Code for Information Interchange3 bits4 bits•Layout was chosen to support sorting by character code•Rows indexed 2-5 are a useful 6-bit (64 element) subset•Control characters were designed for transmission"Line feed""Bell"Representing Strings: the Unicode Standard4http://ian-albert.com/unicode_chart/unichart-chinese.jpg•109,000 characters•93 scripts (organized)•Enumeration of character properties, such as case•Supports bidirectional display order•32 bits per character number •A canonical name for every characterU+0058 LATIN CAPITAL LETTER XU+263a WHITE SMILING FACEU+2639 WHITE FROWNING FACE'!''"'DemoBonusMaterialRepresenting Strings: UTF-8 EncodingUTF: (UCS (Universal Character Set) Transformation Format)Unicode: Correspondence between characters and 32 bit numbersUTF-8: Correspondence between numbers and bytesA byte is 8 bits, and can encode any integer 0-2555Variable-length encoding: integers vary in the number of bytes required to encode them!00000000 000000001 100000011 300000010 2bytes integersIn Python: string length in characters, bytes length in bytesDemoBonusMaterialStrings are Sequences>>> city = 'Berkeley'>>> len(city)8>>> city[3]'k'6Length. A sequence has a finite length.Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0 for the first element.An element of a string is itself a string!>>> 'Berkeley' + ', CA''Berkeley, CA'>>> 'Shabu ' * 2'Shabu Shabu 'String arithmetic is like tuple arithmeticString Membership Differs from Other SequencesThe "in" and "not in" operators match substrings7>>> 'here' in "Where's Waldo?"True>>> 'Mississippi'.count('i')4>>> 'Mississippi'.count('issi')1The "count" method also matches substringsthe number of non-overlapping occurrences of a substringWhy? Working with strings, we care about words, not charactersString Literals Have Three Forms8>>> 'I am string!''I am string!'>>> "I've got an apostrophe""I've got an apostrophe">>> '您好''您好'>>> """The Zen of Pythonclaims, Readability counts.Read more: import this."""'The Zen of Python\nclaims, "Readability counts."\nRead more: import this.'"Line feed" character represents a new lineA backslash "escapes" the following characterSingle- and double-quoted strings are equivalentString CoercionAny object can be "coerced" into a string.Coercion doesn't change an object; it produces a corresponding object of a different type.9>>> digits(1, 8, 2, 8)>>> 2 in digitsTrue>>> str(2) + ' is an element of ' + str(digits)'2 is an element of (1, 8, 2, 8)'The constructor for a string can take any object as its argumentHow is string coercion implemented? October 10Methods on Strings10>>> '1234'.isnumeric()True>>> 'rOBERT dE nIRO'.swapcase()'Robert De Niro'>>> 'snakeyes'.upper().endswith('YES')TrueDemoSequences as Conventional InterfacesConsider two problems:!Sum the even members of the first n Fibonacci numbers.!List the letters in the acronym for a name, which includes the first letter of each capitalized word.110, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.0, 2, 8, 34, . enumerate naturals:map fib:filter iseven:accumulate sum: 44Sequences as Conventional InterfacesConsider two problems:!Sum the even members of the first n Fibonacci numbers.!List the letters in the acronym for a name, which includes the first letter of each capitalized word.12'University', 'of', 'California', 'Berkeley'enumerate words:filter iscap:map first:accumulate tuple:'University', 'California', 'Berkeley' 'U', 'C', 'B'( 'U', 'C', 'B' )Mapping a Function over a Sequence13>>> alternates = (-1, 2, -3, 4, -5)>>> tuple(map(abs, alternates))(1, 2, 3, 4, 5)Apply a function to each element of the sequenceDemoThe returned value of map is an iterable map objectA constructor for the built-in map typeThe returned value of filter is an iterable filter objectAccumulation and Iterable ValuesIterable objects give access to some elements in order.Many built-in functions take iterable objects as argument.14tuple Return a tuple containing the elementssum Return the sum of the elementsmin Return the minimum of the elementsmax Return the maximum of the elementsFor statements also operate on iterable values.DemoGenerator Expressions(<map exp> for <name> in <iter exp> if <filter exp>)15One large expression that evaluates to an iterable object(<map exp> for <name> in <iter exp>)•Evaluates to an iterable object.•<iter exp> is evaluated when the generator expression is evaluated.•Remaining expressions are evaluated when elements are accessedPrecise evaluation rule introduced in Chapter 4.Reducing a SequenceReduce is a higher-order generalization of max, min, & sum.16>>> from operator import mul>>> from functools import reduce>>> reduce(mul, (1, 2, 3, 4, 5))120Similar to accumulate from Homework
View Full Document