In [ ]:
# dictionaries
# key => value
# adhar
# mom :<uin>(key-unique):home(value),lastname(value),bankaccount
# dad :<uin>(key):home,lastname,bankaccount
# kiddo:<uin>(key):home,lastname,bankaccount
In [ ]:
# cheatsheet
# list - ['apple','banana'],list(),[]
# tuple - ('apple','banana'),tuple(),()
# dict - {'a':'apple','b':'banana'},dict(),{}
In [1]:
my_fruits = {'a':'apple','b':'banana','c':'cherry'}
empty_dict = {}
empty_dict1 = dict()
In [4]:
print my_fruits,type(my_fruits)
print empty_dict,type(empty_dict)
print empty_dict1,type(empty_dict1)
In [5]:
# extraction of values
my_fruits['a']
Out[5]:
In [6]:
# inserting values into dict
my_fruits['d']='dates'
print my_fruits
In [7]:
# replace
my_fruits['a'] = 'apricot'
print my_fruits
In [3]:
#functions
print my_fruits
print dir(my_fruits)
In [ ]:
# TASKS
# update,setdefault,fromkeys
# tutorialpoint
In [6]:
# has_key => in
# in operator is only true for a key.
print 'a' in my_fruits
print 'apple' in my_fruits
print my_fruits.has_key('a')
In [8]:
# get
print my_fruits['a']
print my_fruits.get('a')
In [9]:
# items,iteritems,viewitems
print help(my_fruits.items)
In [10]:
print my_fruits
print my_fruits.items()
In [12]:
print help(my_fruits.iteritems)
In [13]:
print my_fruits.iteritems()
In [15]:
for key,value in my_fruits.iteritems():
print key,value
In [16]:
# viewitems
print help(my_fruits.viewitems)
In [17]:
# template data for framework templates
print my_fruits.viewitems()
In [18]:
# keys,iterkeys,viewkeys
print help(my_fruits.keys)
In [19]:
print my_fruits.keys()
In [20]:
print help(my_fruits.iterkeys)
In [21]:
print my_fruits.iterkeys()
In [22]:
for key in my_fruits.iterkeys():
print key
In [23]:
print help(my_fruits.viewkeys)
In [24]:
print my_fruits.viewkeys()
In [ ]:
# values,itervalues,viewvalues
In [ ]:
## copy
# -softcopy
# -deepcopy
# -shallowcopy
In [25]:
# shallowcopy -> complex objects
a = [1,2,3]
b = [4,5,6]
print a,id(a),b,id(b)
In [26]:
Cc = [a,b]
print Cc,id(Cc)
print Cc[0],id(Cc[0]),Cc[1],id(Cc[1])
In [27]:
# Softcopy
Sc = Cc
print Sc,id(Sc)
print Sc[0],id(Sc[0]),Sc[1],id(Sc[1])
In [28]:
# Deepcopy
import copy
Dc = copy.deepcopy(Cc)
print Dc,id(Dc)
print Dc[0],id(Dc[0]),Dc[1],id(Dc[1])
In [29]:
# shallow copy
import copy
Ss = copy.copy(Cc)
print Ss,id(Ss)
print Ss[0],id(Ss[0]),Ss[1],id(Ss[1])
In [ ]:
# page1 -> [traiings,timing]
# page2 -> [trainigs,cost]
In [31]:
# copy
print my_fruits
print help(my_fruits.copy)
In [32]:
Dc = my_fruits.copy()
In [33]:
print Dc,id(Dc)
print my_fruits,id(my_fruits)
In [35]:
# clear
Dc.clear()
print Dc
In [36]:
# pop,popitem
print help(my_fruits.pop)
print help(my_fruits.popitem)
In [37]:
print my_fruits.pop('a')
In [39]:
print my_fruits.pop('a')
In [38]:
print my_fruits
In [41]:
my_fruits.popitem()
Out[41]:
In [42]:
print my_fruits
In [43]:
print my_fruits.pop()
In [ ]: