states = { 'Oregon': 'OR', 'Florida': 'FL', 'California': 'CA', 'New york': 'NY', 'Michigan': 'MI' } cities = { 'CA' : 'San francisco', 'MI' : 'Detroit', 'FL' : 'Jacksonville' } cities['NY'] = 'New York' cities['OR'] = 'Portland' print("-" * 10) print("NY State has:", cities['NY']) print("OR Stare has:", cities["OR"]) print("-" * 10) print("Michigan's abbreviation is :", states['Michigan']) print("Florida's abbreviation is:", states['Florida']) print("-" * 10) print("Michigan has:", cities[states['Michigan']]) print("Florida has:", cities[states['Florida']]) print("-" * 10) for state, abbreviation in states.items(): print("%s is abbreviated %s" % (state, abbreviation)) print("-" * 10) for abbreviation, city in cities.items(): print("%s state is abbreviated %s and has city %s" % (state, abbreviation, cities[abbreviation])) print("_" * 10) state = states.get('Texas', None) if not state: print("Sorry, no Texas.") city = cities.get('TX', 'Does not exist') print("The city for the state 'TX' is: %s" % city)
输出结果: