1 / 14

COMPSCI 101 Principles of Programming

COMPSCI 101 Principles of Programming. Lecture 25 – Dictionaries. Learning outcomes. At the end of this lecture, students should be able to: Create a dictionary Retrieve items from a dictionary. Review. Lists are retrieved by indexes: >>> my_list = [1,2,3,4,5] >>> my_list [0] 1

tuwa
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 25 – Dictionaries

  2. Learning outcomes • At the end of this lecture, students should be able to: • Create a dictionary • Retrieve items from a dictionary COMPSCI 101 - Principles of Programming

  3. Review Lists are retrieved by indexes: >>> my_list = [1,2,3,4,5] >>> my_list[0] 1 >>> my_list[-1] 5 >>> my_list[3] = 8 >>> my_list [1,2,3,8,5] COMPSCI 101 - Principles of Programming

  4. Python Dictionaries • A dictionary is like a list, but more general. • In a list, the indices have to be integers; • in a dictionary they can be (almost) any type. • You can think of a dictionary as a mapping between • a set of indices (which are called keys) and • a set of values. • Each key maps to a value. • The association of a key and a value is called a • key-value pair or sometimes an item. COMPSCI 101 - Principles of Programming

  5. Dictionary Examples >>> english2spanish = dict() >>> print (english2spanish) {} >>> english2spanish['one'] = 'uno' >>> print english2spanish {'one':'uno'} >>> english2spanish = {'one': 'uno', 'two': 'dos', 'three': 'tres'} >>> print english2spanish['four'] KeyError: ‘four’ >>> 'one'in english2spanish(this only works for keys) True COMPSCI 101 - Principles of Programming

  6. Making an Address Book >>> address_book = dict() >>> address_book["Helen"] = "Helen Clark, New York City, 2231-12434-4534" >>> address_book["Helen"] 'Helen Clark, New York City, 2231-12434-4534' >>> address_book["HELEN"] KeyError: 'HELEN' COMPSCI 101 - Principles of Programming

  7. Children’s word game A certain childrens game involves starting with a word in a particular category. Each participant in turn says a word, but that word must begin with the final letter of the previous word. Once a word has been given, it cannot be repeated. If an opponent cannot give a word in the category, they fall out of the game. For example, with "animals" as the category, Child 1: dog Child 2: goldfish Child 1: hippopotamus Child 2: snake ... COMPSCI 101 - Principles of Programming

  8. Exercise Write a function named play_word_game() to play this game. Our function is not restricted to a single category. COMPSCI 101 - Principles of Programming

  9. Playing the game >>> Pick the first word: cat This is my word: t Give next word (or q to quit): tat This is my word: t's Give next word (or q to quit): sat This is my word: ta Give next word (or q to quit): at This is my word: tab Give next word (or q to quit): bat This is my word: tabernacle Give next word (or q to quit): enter This is my word: r Give next word (or q to quit): rat This is my word: table Give next word (or q to quit): q >>> COMPSCI 101 - Principles of Programming

  10. Answer COMPSCI 101 - Principles of Programming

  11. Helper Functions COMPSCI 101 - Principles of Programming

  12. More Helper Functions COMPSCI 101 - Principles of Programming

  13. Summary • How to create a dictionary • How to retrieve things from a dictionary • How to create a larger piece of code with many functions COMPSCI 101 - Principles of Programming

  14. Tomorrow • More fun with Dictionaries and Histograms (counting Dictionaries) COMPSCI 101 - Principles of Programming

More Related