61a lecture 10
play

61A Lecture 10 Monday, September 17 Sequence Iteration 2 Sequence - PowerPoint PPT Presentation

Not on Midterm 1 61A Lecture 10 Monday, September 17 Sequence Iteration 2 Sequence Iteration def count(s, value): total = 0 for elem in s: Name bound in the first frame of the current environment if elem == value: total = total + 1


  1. String Literals Have Three Forms >>> 'I am string!' 'I am string!' Single- and double-quoted >>> "I've got an apostrophe" strings are equivalent "I've got an apostrophe" >>> ' 您好 ' ' 您好 ' >>> """The Zen of Python claims, Readability counts. Read more: import this.""" 'The Zen of Python\nclaims, Readability counts.\nRead more: import this.' A backslash "escapes" the following character 8

  2. String Literals Have Three Forms >>> 'I am string!' 'I am string!' Single- and double-quoted >>> "I've got an apostrophe" strings are equivalent "I've got an apostrophe" >>> ' 您好 ' ' 您好 ' >>> """The Zen of Python claims, Readability counts. Read more: import this.""" 'The Zen of Python\nclaims, Readability counts.\nRead more: import this.' "Line feed" character A backslash "escapes" the represents a new line following character 8

  3. Strings are Sequences 9

  4. Strings are Sequences Length . 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. 9

  5. Strings are Sequences >>> city = 'Berkeley' >>> len(city) 8 >>> city[3] 'k' Length . 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. 9

  6. Strings are Sequences >>> city = 'Berkeley' >>> len(city) 8 >>> city[3] An element of a string 'k' is itself a string! Length . 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. 9

  7. Strings are Sequences >>> city = 'Berkeley' >>> len(city) 8 >>> city[3] An element of a string 'k' is itself a string! Length . 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. >>> 'Berkeley' + ', CA' 'Berkeley, CA' >>> 'Shabu ' * 2 'Shabu Shabu ' 9

  8. Strings are Sequences >>> city = 'Berkeley' >>> len(city) 8 >>> city[3] An element of a string 'k' is itself a string! Length . 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. >>> 'Berkeley' + ', CA' String arithmetic is similar 'Berkeley, CA' to tuple arithmetic >>> 'Shabu ' * 2 'Shabu Shabu ' 9

  9. String Membership Differs from Other Sequence Types 10

  10. String Membership Differs from Other Sequence Types The "in" and "not in" operators match substrings 10

  11. String Membership Differs from Other Sequence Types The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True 10

  12. String Membership Differs from Other Sequence Types The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True Why? Working with strings, we care about words, not characters 10

  13. String Membership Differs from Other Sequence Types The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True Why? Working with strings, we care about words, not characters The count method also matches substrings 10

  14. String Membership Differs from Other Sequence Types The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True Why? Working with strings, we care about words, not characters The count method also matches substrings >>> 'Mississippi'.count('i') 4 >>> 'Mississippi'.count('issi') 1 10

  15. String Membership Differs from Other Sequence Types The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True Why? Working with strings, we care about words, not characters The count method also matches substrings >>> 'Mississippi'.count('i') 4 >>> 'Mississippi'.count('issi') the number of 1 non-overlapping occurrences of a substring 10

  16. String Membership Differs from Other Sequence Types The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True Why? Working with strings, we care about words, not characters The count method also matches substrings >>> 'Mississippi'.count('i') 4 >>> 'Mississippi'.count('issi') the number of 1 non-overlapping occurrences of a substring 10

  17. Bonus Material Representing Strings: the ASCII Standard American Standard Code for Information Interchange 11

  18. Bonus Material Representing Strings: the ASCII Standard American Standard Code for Information Interchange 8 rows: 3 bits 11

  19. Bonus Material Representing Strings: the ASCII Standard American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits 11

  20. Bonus Material Representing Strings: the ASCII Standard American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits • Layout was chosen to support sorting by character code 11

  21. Bonus Material Representing Strings: the ASCII Standard American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits • Layout was chosen to support sorting by character code • Rows indexed 2-5 are a useful 6-bit (64 element) subset 11

  22. Bonus Material Representing Strings: the ASCII Standard American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 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 11

  23. Bonus Material Representing Strings: the ASCII Standard American Standard Code for Information Interchange "Line feed" 8 rows: 3 bits 16 columns: 4 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 11

  24. Bonus Material Representing Strings: the ASCII Standard American Standard Code for Information Interchange "Bell" "Line feed" 8 rows: 3 bits 16 columns: 4 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 11

  25. Bonus Material Representing Strings: the ASCII Standard American Standard Code for Information Interchange "Bell" "Line feed" 8 rows: 3 bits 16 columns: 4 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 Demo 11

  26. Bonus Material Representing Strings: the Unicode Standard 12

  27. Bonus Material Representing Strings: the Unicode Standard http://ian-albert.com/unicode_chart/unichart-chinese.jpg 12

  28. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters http://ian-albert.com/unicode_chart/unichart-chinese.jpg 12

  29. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) http://ian-albert.com/unicode_chart/unichart-chinese.jpg 12

  30. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) • Enumeration of character properties, such as case http://ian-albert.com/unicode_chart/unichart-chinese.jpg 12

  31. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order http://ian-albert.com/unicode_chart/unichart-chinese.jpg 12

  32. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg 12

  33. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg U+0058 LATIN CAPITAL LETTER X 12

  34. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE 12

  35. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE 12

  36. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg U+0058 LATIN CAPITAL LETTER X ' ☺ ' U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE 12

  37. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg U+0058 LATIN CAPITAL LETTER X ' ☺ ' ' ☹ ' U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE 12

  38. Bonus Material Representing Strings: the Unicode Standard • 109,000 characters • 93 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg U+0058 LATIN CAPITAL LETTER X ' ☺ ' ' ☹ ' U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE Demo 12

  39. Bonus Material Representing Strings: UTF-8 Encoding 13

  40. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) 13

  41. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers 13

  42. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes 13

  43. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 13

  44. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 bytes 13

  45. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 bytes integers 13

  46. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 00000000 0 bytes integers 13

  47. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 00000000 0 00000001 1 bytes integers 13

  48. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 00000000 0 00000001 1 bytes integers 00000010 2 13

  49. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 00000000 0 00000001 1 bytes integers 00000010 2 00000011 3 13

  50. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 00000000 0 00000001 1 bytes integers 00000010 2 00000011 3 Variable-length encoding: integers vary in the number of bytes required to encode them! 13

  51. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 00000000 0 00000001 1 bytes integers 00000010 2 00000011 3 Variable-length encoding: integers vary in the number of bytes required to encode them! In Python: string length in characters, bytes length in bytes 13

  52. Bonus Material Representing Strings: UTF-8 Encoding UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between numbers and bytes A byte is 8 bits and can encode any integer 0-255 00000000 0 00000001 1 bytes integers 00000010 2 00000011 3 Variable-length encoding: integers vary in the number of bytes required to encode them! In Python: string length in characters, bytes length in bytes Demo 13

  53. Sequences as Conventional Interfaces 14

  54. Sequences as Conventional Interfaces Consider two problems: 14

  55. Sequences as Conventional Interfaces Consider two problems:  Sum the even members of the first n Fibonacci numbers. 14

  56. Sequences as Conventional Interfaces Consider 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. 14

  57. Sequences as Conventional Interfaces Consider 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. 14

  58. Sequences as Conventional Interfaces Consider 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. enumerate naturals: 14

  59. Sequences as Conventional Interfaces Consider 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. enumerate naturals: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend