In [ ]:
# Dictionary
# perl - hashes,hash
# apple => red color fruit.
# cherry => red color fruit.
# key(unique) => value(duplicated)
# dictionary is not index based its key based.
In [1]:
my_fruits = {'a':'apple','b':'banana','c':'cherry','d':'dates'}
In [2]:
print my_fruits,type(my_fruits)
In [3]:
my_empty = dict()
print my_empty,type(my_empty)
In [4]:
my_empty = {}
print my_empty,type(my_empty)
In [13]:
# know the value for a key.
print my_fruits['a']
In [5]:
# cheat sheets
# list -> ['apple','banana'],list(),[]
# tuple -> ('apple','banana'),tuple(),()
# dict -> {'a':'apple','b':'banana'},dict(),{}
In [6]:
# tasks
my_string = "python"
print my_string,type(my_string)
In [7]:
my_string1 = ('python')
print my_string1,type(my_string1)
In [8]:
my_string1 = ('python',)
print my_string1,type(my_string1)
In [9]:
my_string2 = 'linux','python','jython','perl'
print my_string2,type(my_string2)
In [10]:
my_string3 = 'linux,python,jython,perl'
print my_string3,type(my_string3)
In [12]:
# assignment
print my_fruits
my_fruits['g']='guava'
print my_fruits
In [14]:
# replace
my_fruits['g'] = 'grapes'
print my_fruits
In [16]:
# task
# 'g' => 'grapes','guava'
my_fruits['g'] = ['grapes','guava']
print my_fruits
In [19]:
# in operation
# its gives True only if you key is there.
# in operator on dictionaries work on keys.
print 'a' in my_fruits
print 'apple' in my_fruits
In [20]:
# function
print dir(my_fruits)
In [ ]:
# TODO: fromkeys,update,setdefault
# https://www.tutorialspoint.com/python/python_dictionary.htm
In [22]:
# get
print help(my_fruits.get)
print my_fruits.get('a')
# or
print my_fruits['a']
In [25]:
# has_key
print help(my_fruits.has_key)
print my_fruits.has_key('a')
print my_fruits.has_key('apple')
#or
print 'a' in my_fruits
In [26]:
# key,iterkeys,viewkeys
# keys
print help(my_fruits.keys)
print my_fruits.keys()
In [28]:
# iterkeys
print help(my_fruits.iterkeys)
print my_fruits.iterkeys()
for key in my_fruits.iterkeys():
print key
In [29]:
# viewkeys
print help(my_fruits.viewkeys)
print my_fruits.viewkeys() # template values to pass as an argument.
In [30]:
# values,itervalue,viewvalues
print my_fruits.values()
print my_fruits.itervalues()
print my_fruits.viewvalues()
In [32]:
# items,iteritems,viewitems
print my_fruits.items()
print my_fruits.iteritems()
print my_fruits.viewitems()
In [34]:
# copy
# soft,deep and shallow copy.
a = ['ant','ball','call']
b = ['den','egg','fin']
print id(a),id(b)
# complex object
Cc = [a,b]
print Cc,id(Cc)
print id(Cc[0]),id(Cc[1])
In [35]:
# soft copy
Soc = Cc
print Cc,id(Cc),id(Cc[0]),id(Cc[1])
print Soc,id(Soc),id(Soc[0]),id(Soc[1])
In [37]:
# hard copy
import copy
print dir(copy)
In [39]:
Dc = copy.deepcopy(Cc)
print Cc,id(Cc),id(Cc[0]),id(Cc[1])
print Dc,id(Dc),id(Dc[0]),id(Dc[1])
In [40]:
# shallow copy
# the new object has a new memory address
# the interal elements are still refering to old objects.
# ex:
# sc[0] => a
# sc[1] => b
Sc = copy.copy(Cc)
print Cc,id(Cc),id(Cc[0]),id(Cc[1])
print Sc,id(Sc),id(Sc[0]),id(Sc[1])
In [41]:
#pop
print my_fruits
In [42]:
print help(my_fruits.pop)
In [43]:
print my_fruits.pop('g')
print my_fruits
In [44]:
# popitem
print help(my_fruits.popitem)
In [45]:
print my_fruits.popitem()
In [46]:
print my_fruits.popitem()
In [47]:
print my_fruits
In [49]:
# clear
print my_fruits
print my_fruits.clear()
print my_fruits
In [ ]:
# homework
# task 1
# output
# {1:1,2:4,3:9,4:16,...,100:10000}
# task2
# my_values = [1,2,3,4,5]
# output => (1,2,3,4,5)