Creating a dictionary
Python Dictionaries Quick GuideDictionary: a collection of paired data elements: a key and a value. The key is used to store and retrieve the data.Keys can be strings, integers, or floating point numbers.Open the interactive window and try the following examples.Creating a dictionaryphonebook = {"Al":"555-1111", "Bob":"555-2222", "Chuck":"555-3333"}The keys are the names. The values are the phone numbers.Creating an empty dictionaryphonebook2 = {}Retrieving a value from a dictionaryprint(phonebook["Al"])or:print(phonebook.get("Al")Testing for inclusionif "Al" in phonebook:Adding elementsphonebook["Dan"] = "555-4444"Deleting elementsdel phonebook["Chuck"]Finding the sizeprint(len(phonebook))Clearing a dictionaryphonebook2.clear()The items method: iterating over an entire dictionaryprint(phonebook.items()) # does NOT work!for key, value in phonebook.items(): print(key, value)The keys methodReturns the keys:print(phonebook.keys())You can iterate over them, too:for key in phonebook.keys(): print(key)The values methodReturns the values:print(phonebook.values())You can iterate over the values:for value in phonebook.values(): print(value)The pop methodReturns AND removes the requested item from the dictionary.phonebook.pop("Al")Dictionary ExampleWrite a program to count words. Input file: Dracula.txt ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- chapter 1 scalar variables and data types
- student lab 1 input processing and output
- final virginia tech
- python part iv storing multiple values in lists
- python murray state university
- python json
- perl primer university of california davis
- laboratory manual for computer programming with python
- creating a dictionary
- python part i set up
Related searches
- creating a mission statement worksheet
- creating a business model
- creating a business plan step by step
- importance of creating a will
- creating a budget worksheet pdf
- creating a resume
- creating a vision statement exercise
- creating a business email
- creating a business plan
- creating a vision statement worksheet
- creating a thesis for a research paper
- append a list to a dictionary python