Monday, 14 May 2012

Dictionaries -- Idle Shell Output -- Online Tutor


Idle Shell Output
Crib_Dist4_3.py

# filename: Crib_Dist4_3.py
# 4.3 ---   Dictionaries

section = "4.3 ---   Dictionaries"
print '\n' + section +'\n'


print 'Dictionaries are lists of Key:Value pairs'

Flintstones = {} # creates empty dictionary .. Flintstones
print "Flintstones  # dictionary is empty"
print Flintstones 
Flintstones_neighbours ={'Barney Rubble':'sidekick', \
'Betty Rubble':'wife', 'Bamm-Bamm':'son'}
print 'Flintstones neighbours'
print Flintstones_neighbours

print '# Add Fred & Wilma to the Flintstones dictionary'





Flintstones['Fred'] = 'star'
Flintstones['Wilma'] = 'wife'
print '# Add Pebbles'
Flintstones['Pebbles'] = 'daughter'

print 'Dictionaries are not ordered'

print 'Flintstones'
print Flintstones

# dictionaries are like hashes in Perl

print "# adding pets"
Flintstones.update({'Dino':'dino-dog'})
print Flintstones
Flintstones_neighbours.update({'Hoppy':'dino-roo'})
print Flintstones_neighbours
print '# Merging 2 dictionaries'
Flintstones.update(Flintstones_neighbours) 
print Flintstones














Highly recommend you enter the code into Online Tutor and try out the tool.

No comments:

Post a Comment