In [ ]:
# dictionaries,hashes,dict
# key => value
# keys should not be duplicated, values can be duplicate.
# ex: apple => red color fruit
# ex: cherry => red color fruit
# ex: ssn,uin,
In [1]:
# creation of a dictionary
my_fruits = {'a':'apple','b':'banana','c':'cherry','d':'dates',2:'twenty'}
print my_fruits,type(my_fruits)
# empty dictionaries
my_empty={}
print my_empty,type(my_empty)
# empty dictionaries
my_empty=dict()
print my_empty,type(my_empty)
In [3]:
# cheat sheet
# list - [apple,banana],[],list()
# tuple - (apple,banana),(),tuple()
# dict - {'a':'apple','b':'banana'},{},dict()
In [4]:
# add new value to a dictionary
my_fruits['g'] = 'guava'
print my_fruits
In [27]:
# modify already existing values
my_fruits['g'] = ['grapes','guava']
print my_fruits
In [8]:
# how to get a value from a key
print my_fruits['a']
print my_fruits['g'][0]
In [10]:
# in operation
print 'a' in my_fruits
print 'apple' in my_fruits
In [11]:
# functions
print dir(my_fruits)
In [12]:
# get
print help(my_fruits.get)
In [15]:
print my_fruits.get('a')
print my_fruits.get('h')
print my_fruits['a']
In [16]:
# has_key
print help(my_fruits.has_key)
In [19]:
print my_fruits.has_key('a')
print my_fruits.has_key('apple')
print 'a' in my_fruits
print 'apple' in my_fruits
In [21]:
# keys,iterkeys,viewkeys
# keys
print help(my_fruits.keys)
print my_fruits.keys()
In [23]:
# iterkeys
print help(my_fruits.iterkeys)
print my_fruits.iterkeys()
for key in my_fruits.iterkeys():
print key
In [24]:
# viewkeys
print help(my_fruits.viewkeys)
print my_fruits.viewkeys()
In [28]:
# values,itervalues,viewvalue
# values
print my_fruits
print help(my_fruits.values)
print my_fruits.values()
In [29]:
# itervalues
print help(my_fruits.itervalues)
print my_fruits.itervalues()
In [30]:
# viewvalues
print help(my_fruits.viewvalues)
print my_fruits.viewvalues()
In [31]:
# items,iteritems,viewitems
In [32]:
# items
print help(my_fruits.items)
print my_fruits.items()
In [33]:
# iteritems
print help(my_fruits.iteritems)
print my_fruits.iteritems()
In [34]:
# viewitems
print help(my_fruits.viewitems)
print my_fruits.viewitems()
In [2]:
#'clear', 'copy', 'fromkeys','pop', 'popitem', 'setdefault', 'update'
In [3]:
#update
print help(my_fruits.update)
In [4]:
print my_fruits
In [5]:
my_fruits.update({3:'thirty',4:'fourty','5':'fifty'})
In [6]:
print my_fruits
In [10]:
# 'setdefault'
print help(my_fruits.setdefault)
print my_fruits.setdefault??
In [ ]:
print my_fruits.setdefault
In [13]:
my_fruits.setdefault('a',['apple','apricot'])
# my_fruits['a']=['apple','apricot']
Out[13]:
In [9]:
print my_fruits
In [11]:
my_fruits.setdefault('j','jackfruit')
print my_fruits
In [12]:
my_fruits.setdefault('j')
# my_fruits['j'] = 'jackfruit'
Out[12]:
In [14]:
my_fruits.setdefault('j','jamun')
Out[14]:
In [15]:
print my_fruits
In [18]:
# user
my_employee={'labor':500,'manager':50000}
my_oracle={}
my_oracle.setdefault('kumar',my_employee.setdefault('labor'))
my_oracle.setdefault('sai',my_employee.setdefault('labor'))
my_oracle.setdefault('ansh',my_employee.setdefault('manager'))
print my_oracle
In [19]:
#'fromkeys'
print help(my_fruits.fromkeys)
In [21]:
my_new={}
print my_fruits
my_new.fromkeys(my_fruits,10)
Out[21]:
In [22]:
#'pop', 'popitem'
print help(my_fruits.pop)
In [24]:
print my_fruits
my_fruits.pop(2)
Out[24]:
In [25]:
print my_fruits
my_fruits.pop(2)
In [28]:
# del
del(my_fruits[3])
In [29]:
print my_fruits
In [30]:
del(my_fruits[3])
In [31]:
# popitem
print help(my_fruits.popitem)
In [32]:
my_fruits.popitem()
Out[32]:
In [33]:
# copy - shallow copy
In [34]:
print help(my_fruits.copy)
In [42]:
new_fruits = my_fruits.copy()
print new_fruits
print new_fruits is my_fruits
In [35]:
# shallow copy
In [36]:
a = [1,2,3]
b = [4,5,6]
print id(a),id(b)
print a,b
In [37]:
# complex object
cob = [a,b]
print cob,cob[0],cob[1]
print id(cob),id(cob[0]),id(cob[1])
In [38]:
# soft copy
soc = cob
print id(cob),id(cob[0]),id(cob[1])
print id(soc),id(soc[0]),id(soc[1])
In [39]:
# deep copy
import copy
doc = copy.deepcopy(cob)
print id(cob),id(cob[0]),id(cob[1])
print id(doc),id(doc[0]),id(doc[1])
In [41]:
# shallow copy
import copy
shc = copy.copy(cob)
print id(cob),id(cob[0]),id(cob[1])
print id(shc),id(shc[0]),id(shc[1])
print "-" * 15,id(a),id(b)
In [43]:
# clear
print help(my_fruits.clear)
In [44]:
my_fruits.clear()
print my_fruits
In [ ]: