Definition dictionary dic = {' A ': ' Hello ', ' B ': ' How ', ' C ': ' You '}
Method One:
For key in DIC:
Print Key,dic[key]
Print key + str (Dic[key])
Results:
A Hello
Ahello
C You
Cyou
b How
bhow
Details:
Print Key,dic[key], followed by a comma, automatically generates a space
Print key + str (Dic[key]), connecting two strings, with a plus sign, direct output, without commas in the middle
Method Two:
for (K,V) in Dic.items ():
Print "dic[%s]="%k,v
Results:
dic[a]= Hello
dic[c]=
dic[b]= How
Method Three:
For k,v in dic.items (): #Python3中已经废除iteritems, use items instead
Print "dic[%s]="%k,v
Results:
dic[a]= Hello
dic[c]=
dic[b]= How
Items () returns a Iterator object. For example:
Print Dic.items () #<dictionary-itemiterator object at 0x020e9a50>
Drill Down: Iteritor is the meaning of iterators, one time to back up a data item, know no so far
For I in Dic.items ():
Print I
Result: (' A ', ' hello ')
(' C ', ' you ')
(' B ', ' how ')
Three ways to access the Python dictionary