dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3} dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, } python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function']) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning) for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable. # Create an empty dictionary. python_words = {} # Fill the dictionary, pair by pair. python_words['list'] ='A collection of values that are not connected, but have an order.' python_words['dictionary'] = 'A collection of key-value pairs.' python_words['function'] = 'A named set of instructions that defines a set of actions in Python.' # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print('dictionary: ' + python_words['dictionary']) # Clarify one of the meanings. python_words['dictionary'] = 'A collection of key-value pairs. Each key can be used to access its corresponding value.' print('\ndictionary: ' + python_words['dictionary']) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Show the current set of words and meanings. print("\n\nThese are the Python words I know:") for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning) # Remove the word 'list' and its meaning. del python_words['list'] # Show the current set of words and meanings. print("\n\nThese are the Python words I know:") for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning) def show_words_meanings(python_words): # This function takes in a dictionary of python words and meanings, # and prints out each word with its meaning. print("\n\nThese are the Python words I know:") for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } show_words_meanings(python_words) # Remove the word 'list' and its meaning. del python_words['list'] show_words_meanings(python_words) def show_words_meanings(python_words): # This function takes in a dictionary of python words and meanings, # and prints out each word with its meaning. print("\n\nThese are the Python words I know:") for word, meaning in python_words.items(): print("\n%s: %s" % (word, meaning)) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } show_words_meanings(python_words) # Remove the word 'list' and its meaning. del python_words['list'] show_words_meanings(python_words) # We have a spelling mistake! python_words = {'lisst': 'A collection of values that are not connected, but have an order.'} # Create a new, correct key, and connect it to the old value. # Then delete the old key. python_words['list'] = python_words['lisst'] del python_words['lisst'] # Print the dictionary, to show that the key has changed. print(python_words) my_dict = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3', } for key, value in my_dict.items(): print('\nKey: %s' % key) print('Value: %s' % value) my_dict = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3', } print(my_dict.items()) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning) my_dict = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3', } for key in my_dict.keys(): print('Key: %s' % key) my_dict = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3', } for key in my_dict: print('Key: %s' % key) my_dict = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3', } for key in my_dict: print('Key: %s' % key) if key == 'key_2': print(" The value for key_2 is %s." % my_dict[key]) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Show the words that are currently in the dictionary. print("The following Python words have been defined:") for word in python_words: print("- %s" % word) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Show the words that are currently in the dictionary. print("The following Python words have been defined:") for word in python_words: print("- %s" % word) # Allow the user to choose a word, and then display the meaning for that word. requested_word = raw_input("\nWhat word would you like to learn about? ") print("\n%s: %s" % (requested_word, python_words[requested_word])) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Show the words that are currently in the dictionary. print("The following Python words have been defined:") for word in python_words: print("- %s" % word) requested_word = '' while requested_word != 'quit': # Allow the user to choose a word, and then display the meaning for that word. requested_word = raw_input("\nWhat word would you like to learn about? (or 'quit') ") if requested_word in python_words.keys(): print("\n %s: %s" % (requested_word, python_words[requested_word])) else: # Handle misspellings, and words not yet stored. print("\n Sorry, I don't know that word.") python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Show the words that are currently in the dictionary. print("The following Python words have been defined:") for word in python_words: print("- %s" % word) requested_word = '' while requested_word != 'quit': # Allow the user to choose a word, and then display the meaning for that word. requested_word = raw_input("\nWhat word would you like to learn about? (or 'quit') ") if requested_word in python_words.keys(): # This is a word we know, so show the meaning. print("\n %s: %s" % (requested_word, python_words[requested_word])) elif requested_word != 'quit': # This is not in python_words, and it's not 'quit'. print("\n Sorry, I don't know that word.") else: # The word is quit. print "\n Bye!" my_dict = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3', } for value in my_dict.values(): print('Value: %s' % value) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } for meaning in python_words.values(): print("Meaning: %s" % meaning) python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print each meaning, one at a time, and ask the user # what word they think it is. for meaning in python_words.values(): print("\nMeaning: %s" % meaning) guessed_word = raw_input("What word do you think this is? ") # The guess is correct if the guessed word's meaning matches the current meaning. if python_words[guessed_word] == meaning: print("You got it!") else: print("Sorry, that's just not the right word.") python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print each meaning, one at a time, and ask the user # what word they think it is. for meaning in python_words.values(): print("\nMeaning: %s" % meaning) # Assume the guess is not correct; keep guessing until correct. correct = False while not correct: guessed_word = input("\nWhat word do you think this is? ") # The guess is correct if the guessed word's meaning matches the current meaning. if python_words[guessed_word] == meaning: print("You got it!") correct = True else: print("Sorry, that's just not the right word.") python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } def show_words(python_words): # A simple function to show the words in the dictionary. display_message = "" for word in python_words.keys(): display_message += word + ' ' print display_message # Print each meaning, one at a time, and ask the user # what word they think it is. for meaning in python_words.values(): print("\n%s" % meaning) # Assume the guess is not correct; keep guessing until correct. correct = False while not correct: print("\nWhat word do you think this is?") show_words(python_words) guessed_word = raw_input("- ") # The guess is correct if the guessed word's meaning matches the current meaning. if python_words[guessed_word] == meaning: print("You got it!") correct = True else: print("Sorry, that's just not the right word.") # This program stores people's favorite numbers, and displays them. favorite_numbers = {'eric': [3, 11, 19, 23, 42], 'ever': [2, 4, 5], 'willie': [5, 35, 120], } # Display each person's favorite numbers. print("Eric's favorite numbers are:") print(favorite_numbers['eric']) print("\nEver's favorite numbers are:") print(favorite_numbers['ever']) print("\nWillie's favorite numbers are:") print(favorite_numbers['willie']) # This program stores people's favorite numbers, and displays them. favorite_numbers = {'eric': [3, 11, 19, 23, 42], 'ever': [2, 4, 5], 'willie': [5, 35, 120], } # Display each person's favorite numbers. for name in favorite_numbers: print("\n%s's favorite numbers are:" % name.title()) print(favorite_numbers[name]) # This program stores people's favorite numbers, and displays them. favorite_numbers = {'eric': [3, 11, 19, 23, 42], 'ever': [2, 4, 5], 'willie': [5, 35, 120], } # Display each person's favorite numbers. for name in favorite_numbers: print("\n%s's favorite numbers are:" % name.title()) # Each value is itself a list, so we need another for loop # to work with the list. for favorite_number in favorite_numbers[name]: print(favorite_number) # This program stores people's favorite numbers, and displays them. favorite_numbers = {'eric': [3, 11, 19, 23, 42], 'ever': [2, 4, 5], 'willie': [5, 35, 120], } # Display each person's favorite numbers. for name in favorite_numbers: print("\n%s's favorite numbers are:" % name.title()) # Each value is itself a list, so let's put that list in a variable. current_favorite_numbers = favorite_numbers[name] for favorite_number in current_favorite_numbers: print(favorite_number) # This program stores information about pets. For each pet, # we store the kind of animal, the owner's name, and # the breed. pets = {'willie': {'kind': 'dog', 'owner': 'eric', 'vaccinated': True}, 'walter': {'kind': 'cockroach', 'owner': 'eric', 'vaccinated': False}, 'peso': {'kind': 'dog', 'owner': 'chloe', 'vaccinated': True}, } # Let's show all the information for each pet. print("Here is what I know about Willie:") print("kind: " + pets['willie']['kind']) print("owner: " + pets['willie']['owner']) print("vaccinated: " + str(pets['willie']['vaccinated'])) print("\nHere is what I know about Walter:") print("kind: " + pets['walter']['kind']) print("owner: " + pets['walter']['owner']) print("vaccinated: " + str(pets['walter']['vaccinated'])) print("\nHere is what I know about Peso:") print("kind: " + pets['peso']['kind']) print("owner: " + pets['peso']['owner']) print("vaccinated: " + str(pets['peso']['vaccinated'])) # This program stores information about pets. For each pet, # we store the kind of animal, the owner's name, and # the breed. pets = {'willie': {'kind': 'dog', 'owner': 'eric', 'vaccinated': True}, 'walter': {'kind': 'cockroach', 'owner': 'eric', 'vaccinated': False}, 'peso': {'kind': 'dog', 'owner': 'chloe', 'vaccinated': True}, } # Let's show all the information for each pet. for pet_name, pet_information in pets.items(): print("\nHere is what I know about %s:" % pet_name.title()) print("kind: " + pet_information['kind']) print("owner: " + pet_information['owner']) print("vaccinated: " + str(pet_information['vaccinated'])) # This program stores information about pets. For each pet, # we store the kind of animal, the owner's name, and # the breed. pets = {'willie': {'kind': 'dog', 'owner': 'eric', 'vaccinated': True}, 'walter': {'kind': 'cockroach', 'owner': 'eric', 'vaccinated': False}, 'peso': {'kind': 'dog', 'owner': 'chloe', 'vaccinated': True}, } # Let's show all the information for each pet. for pet_name, pet_information in pets.items(): print("\nHere is what I know about %s:" % pet_name.title()) # Each animal's dictionary is in 'information' for key in pet_information: print(key + ": " + str(pet_information[key])) # This program stores information about pets. For each pet, # we store the kind of animal, the owner's name, and # the breed. pets = {'willie': {'kind': 'dog', 'owner': 'eric', 'vaccinated': True}, 'walter': {'kind': 'cockroach', 'owner': 'eric', 'vaccinated': False}, 'peso': {'kind': 'dog', 'owner': 'chloe', 'vaccinated': True}, } # Let's show all the information for each pet. for pet_name, pet_information in pets.items(): print("\nHere is what I know about %s:" % pet_name.title()) # Each animal's dictionary is in pet_information for key in pet_information: if key == 'owner': # Capitalize the owner's name. print(key + ": " + pet_information[key].title()) elif key == 'vaccinated': # Print 'yes' for True, and 'no' for False. vaccinated = pet_information['vaccinated'] if vaccinated: print 'vaccinated: yes' else: print 'vaccinated: no' else: # No special formatting needed for this key. print(key + ": " + pet_information[key])