-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsampledicts.py
31 lines (24 loc) · 1.27 KB
/
sampledicts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# SAMPLE CODE TO UNDERSTAND THE CONCEPT OF DICTIONARIES IN PYTHON
#The pythond Dictionaries have two things,
#1st one is the key and secon one is the value
#The function of the dictionary is when calling the key in dic. it returns the value in the key
#This is a Dictionary
cities = {'CA': 'San Francisco', 'MI': 'Detroit','FL': 'Jacksonville'} #Each key and value is separated by ':'
#Calling the key inside the dictionary and it returns the value belongs to the key
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
#This is a function 'themap' is the dictionary and 'state' is the key
def find_city(themap, state):
if state in themap: #if the 'state' is in the dictionary return the key's value
return themap[state]
else:
return "Not found." #if the requested key is not in the dictionary it prints 'Not Found'
# ok pay attention!
cities['_find'] = find_city #Assign the function in to a variable 'cities['_find']'
while True: #This is a loop wich run until user press enter
print "State? (ENTER to quit)",
state = raw_input("> ") # save user inputs into the 'state' variable
if not state: break
# this line is the most important ever! study!
city_found = cities['_find'](cities, state) #call the funtion written before
print city_found #print the returned value