Skip to content

Commit f6902f9

Browse files
committed
Sample code to illustrate dictionaries in python
1 parent 322df65 commit f6902f9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

sampledicts.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SAMPLE CODE TO UNDERSTAND THE CONCEPT OF DICTIONARIES IN PYTHON
2+
3+
cities = {'CA': 'San Francisco', 'MI': 'Detroit','FL': 'Jacksonville'}
4+
cities['NY'] = 'New York'
5+
cities['OR'] = 'Portland'
6+
def find_city(themap, state):
7+
if state in themap:
8+
return themap[state]
9+
else:
10+
return "Not found."
11+
# ok pay attention!
12+
13+
cities['_find'] = find_city
14+
15+
while True:
16+
print "State? (ENTER to quit)",
17+
state = raw_input("> ")
18+
if not state: break
19+
20+
# this line is the most important ever! study!
21+
22+
city_found = cities['_find'](cities, state)
23+
print city_found

0 commit comments

Comments
 (0)