1 / 21

COMPSCI 101 Principles of Programming

COMPSCI 101 Principles of Programming. Lecture 19 – Strings as Sequences Dr. Patricia J. RIddle. Who am I?. My research area is Artificial Intelligence Girls are as good as boys at Computer Science research

Download Presentation

COMPSCI 101 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101Principles of Programming Lecture 19 – Strings as Sequences Dr. Patricia J. RIddle

  2. Who am I? • My research area is Artificial Intelligence • Girls are as good as boys at Computer Science research • If you want to talk about either of these topics (or just talk about Computer Science 101) my office hours are: • Friday 1-3pm COMPSCI 101 - Principles of Programming

  3. Learning outcomes • At the end of this lecture, students should be able to: • understand that strings are just sequences • use an index to obtain a character from a string • use loops to iterate through a string • make good test cases COMPSCI 101 - Principles of Programming

  4. Review of return from test Do these two functions print the same output? Why or why not? deffunction1(): print(["fred"]) print(["sally"]) return(["mary"]) def function2(): print(["fred"]) return(["mary"]) print(["sally"]) COMPSCI 101 - Principles of Programming

  5. Review of Strings • Strings and Characters • A string is a SEQUENCE of characters. • String literals can be enclosed in matching single quotes (') or double quotes ("). • Python does not have a data type for characters. • A single-character string represents a character COMPSCI 101 - Principles of Programming

  6. Lists as Sequences • List of strings • fruit_list = ["apple", "banana", "orange"] • fruit_list[3] • len(fruit_list) • List of strings • my_word_list= ["s", "t", "r", "I", "n", "g"] • my_word_list[3] • len(my_word_list) COMPSCI 101 - Principles of Programming

  7. Strings as Sequences • String • my_word= "string” • my_word[3] • len(my_word) COMPSCI 101 - Principles of Programming

  8. Sequences • Lists and strings are examples of a more basic Python type: the sequence • Sequences can be: • Indexed • Iterated over with for loops COMPSCI 101 - Principles of Programming

  9. How to get the first letter of a string? • Just like a list • Which one will work? • my_word[0] • my_word[1] COMPSCI 101 - Principles of Programming

  10. How to get the last letter of a string? • Just like a list • Can use len(my_word) • What does my_word[ len(my_word) ] return? • Can use my_word[ len(my_word)-1 ] • Or can use my_word[-1] COMPSCI 101 - Principles of Programming

  11. Testing • isinstance(x,str) • Returns True if variable x is a string (i.e., is of type “str”) and False otherwise COMPSCI 101 - Principles of Programming

  12. Exercise Write a function named substring() that accepts 2 strings as parameters and prints True if the first string is a substring of the second string and False otherwise. >>> substring("ab", "ab fab") True >>> substring("ab", "fact") False COMPSCI 101 - Principles of Programming

  13. Answer COMPSCI 101 - Principles of Programming

  14. Exercise Write a function named palindrome() that accepts a string as a parameter and prints True if the string is a palindrome and False otherwise. A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Wikipedia >>> palindrome("otto") True >>> palindrome("fact") False COMPSCI 101 - Principles of Programming

  15. Answer COMPSCI 101 - Principles of Programming

  16. Exercise Write a function named heterogram() that accepts a string as a parameter and prints True if it is a heterogram and False otherwise. • A heterogram is a word, phrase, or sentence in which no letter of the alphabet occurs more than once. • Wikipedia >>> heterogram("otto") False >>> heterogram("fact") True COMPSCI 101 - Principles of Programming

  17. Answer COMPSCI 101 - Principles of Programming

  18. More Test Cases heterogram("i am") heterogram("iam boy") heterogram("iam a boy") COMPSCI 101 - Principles of Programming

  19. Answer COMPSCI 101 - Principles of Programming

  20. Summary • Learned that Strings and Lists are analogous in many ways, both are the basic Python type Sequence • Learned how to iterate through strings • Learned how to make good test cases COMPSCI 101 - Principles of Programming

  21. Tomorrow • More fun with Boolean Conditions COMPSCI 101 - Principles of Programming

More Related