In [ ]:
# dictionaries,dict,hash,hashes
# ssn - Social Security Number
# uin - Unique Identification number
# me(santosh-UID) : address,se
# father(UID) : address,gs
# mother(UID) : address,hw
# key:value ( keys should always be unique)
In [3]:
# dictionaries are not index based.
# dictionaries need not be arranged the way they are inserted.
# if you want them to be printed the way you entered then you need to use pprint.
my_fruits = {'a':'apple','b':'banana','c':'choclate','d':'donut'}
print my_fruits,type(my_fruits)
empty_dict = {}
print empty_dict,type(empty_dict)
empty_dict = dict()
print empty_dict,type(empty_dict)
In [8]:
# we can extract the elements of a dictionary using the key and value pair.
print my_fruits['a']
print my_fruits['d']
In [6]:
# insert a new value into a dictionary
my_fruits['e'] = 'elephant'
print my_fruits
# replace
my_fruits['a'] = 'avacardo'
print my_fruits
In [17]:
# iteration on dictionaries
for key in my_fruits:
print key,my_fruits[key]
In [ ]:
# cheat-sheets
# list - ['apple','banana','cherry'],[],list()
# tuple - ('apple','banana','cherry'),(),tuple()
# dict - {'a':'apple','b':'balloon'},{},dict()
In [4]:
# in on a dictionary works only on keys.
print 'a' in my_fruits
print 'apple' in my_fruits
In [9]:
# functions
my_fruits = {'a':'apple','b':'banana','c':'choclate','d':'donut'}
print dir(my_fruits)
In [11]:
# get
print my_fruits.get('a')
print my_fruits['a']
In [12]:
# 'has_key'
print 'a' in my_fruits
print my_fruits.has_key('a')
In [13]:
# keys,iterkeys,viewkeys
# keys
print help(my_fruits.keys)
print my_fruits.keys()
In [18]:
# iterkeys
print help(my_fruits.iterkeys)
for key in my_fruits.iterkeys():
print key
In [19]:
# viewkeys - templated systems.
# for your frameworks - django,flask and bottle.
print help(my_fruits.viewkeys)
print my_fruits.viewkeys()
In [21]:
#values,itervalues,viewvalues
# values
print help(my_fruits.values)
print my_fruits.values()
In [23]:
# itervalues
print help(my_fruits.itervalues)
for value in my_fruits.itervalues():
print value
In [26]:
# viewvalues - templated systems.
# for your frameworks - django,flask and bottle.
print help(my_fruits.viewvalues)
print my_fruits.viewvalues()
for value in my_fruits.viewvalues():
print value
In [27]:
# items,iteritems,viewitems
print help(my_fruits.items)
print my_fruits.items()
# [('avinash', 'django'), ('azhar', 'ruby'), ('kumar', 'ansibel'), ('vipin', 'python')]
In [29]:
# iteritems
print help(my_fruits.iteritems)
print my_fruits.iteritems()
for value in my_fruits.iteritems():
print value
In [30]:
# viewitems
print help(my_fruits.viewitems)
print my_fruits.viewitems()
In [36]:
# update
print help(my_fruits.update)
my_fruits.update({'an':'anjeer'})
print my_fruits
my_fruits['ana']='anar'
print my_fruits
In [47]:
# setdefault - task to be looked into.
print help(my_fruits.setdefault)
print my_fruits.setdefault('a',None)
print my_fruits.setdefault('z','zebra')
#print my_fruits['a']
#print my_fruits['z']
In [55]:
# copy
# softcopy,deepcopy,shallowcopy
# shallowcopy always works on complex objects.
import copy
a = [1,2,3]
b = [4,5,6]
Cc = [a,b]
print a,id(a)
print b,id(b)
print Cc,id(Cc),id(Cc[0]),id(Cc[1])
# softcopy
Soc = Cc
print Soc,id(Soc),id(Soc[0]),id(Soc[1])
# deepcopy
Doc = copy.deepcopy(Cc)
print Doc,id(Doc),id(Doc[0]),id(Doc[1])
# shallowcopy
Shc = copy.copy(Cc)
print Shc,id(Shc),id(Shc[0]),id(Shc[1])
# if i modify a
a[0] = "one"
print a
print Shc,id(Shc),id(Shc[0]),id(Shc[1])
print Doc,id(Doc),id(Doc[0]),id(Doc[1])
# web1,web2
# web1(training,timing) # complext object
# web2(training,cost) # complex obejct
3
In [58]:
# copy
print help(my_fruits.copy)
D_fruits = my_fruits.copy()
print D_fruits
print D_fruits is my_fruits
In [63]:
# pop
print help(D_fruits.pop)
print D_fruits
print D_fruits.pop('b')
In [64]:
print D_fruits
print D_fruits.pop('b')
In [66]:
#popitem
print help(D_fruits.popitem)
D_fruits.popitem()
Out[66]:
In [67]:
print D_fruits
In [69]:
# clear'
print help(D_fruits.clear)
print D_fruits.clear()
print D_fruits
In [ ]:
# fromkeys => examples -> tutorialpoint