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)


{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'dates'} <type 'dict'>

In [3]:
my_empty = dict()
print my_empty,type(my_empty)


{} <type 'dict'>

In [4]:
my_empty = {}
print my_empty,type(my_empty)


{} <type 'dict'>

In [13]:
# know the value for a key.
print my_fruits['a']


apple

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)


python <type 'str'>

In [7]:
my_string1 = ('python')
print my_string1,type(my_string1)


python <type 'str'>

In [8]:
my_string1 = ('python',)
print my_string1,type(my_string1)


('python',) <type 'tuple'>

In [9]:
my_string2 = 'linux','python','jython','perl'
print my_string2,type(my_string2)


('linux', 'python', 'jython', 'perl') <type 'tuple'>

In [10]:
my_string3 = 'linux,python,jython,perl'
print my_string3,type(my_string3)


linux,python,jython,perl <type 'str'>

In [12]:
# assignment
print my_fruits
my_fruits['g']='guava'
print my_fruits


{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'dates'}
{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': 'guava'}

In [14]:
# replace
my_fruits['g'] = 'grapes'
print my_fruits


{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': 'grapes'}

In [16]:
# task
# 'g' => 'grapes','guava'
my_fruits['g'] = ['grapes','guava']
print my_fruits


{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['grapes', 'guava']}

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


True
False

In [20]:
# function
print dir(my_fruits)


['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']

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']


Help on built-in function get:

get(...)
    D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.

None
apple
apple

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


Help on built-in function has_key:

has_key(...)
    D.has_key(k) -> True if D has a key k, else False

None
True
False
True

In [26]:
# key,iterkeys,viewkeys

# keys
print help(my_fruits.keys)
print my_fruits.keys()


Help on built-in function keys:

keys(...)
    D.keys() -> list of D's keys

None
['a', 'c', 'b', 'd', 'g']

In [28]:
# iterkeys
print help(my_fruits.iterkeys)
print my_fruits.iterkeys()
for key in my_fruits.iterkeys():
    print key


Help on built-in function iterkeys:

iterkeys(...)
    D.iterkeys() -> an iterator over the keys of D

None
<dictionary-keyiterator object at 0x7f4b697bd998>
a
c
b
d
g

In [29]:
# viewkeys
print help(my_fruits.viewkeys)
print my_fruits.viewkeys() # template values to pass as an argument.


Help on built-in function viewkeys:

viewkeys(...)
    D.viewkeys() -> a set-like object providing a view on D's keys

None
dict_keys(['a', 'c', 'b', 'd', 'g'])

In [30]:
# values,itervalue,viewvalues

print my_fruits.values()
print my_fruits.itervalues()
print my_fruits.viewvalues()


['apple', 'cherry', 'banana', 'dates', ['grapes', 'guava']]
<dictionary-valueiterator object at 0x7f4b697bdaa0>
dict_values(['apple', 'cherry', 'banana', 'dates', ['grapes', 'guava']])

In [32]:
# items,iteritems,viewitems

print my_fruits.items()
print my_fruits.iteritems()
print my_fruits.viewitems()


[('a', 'apple'), ('c', 'cherry'), ('b', 'banana'), ('d', 'dates'), ('g', ['grapes', 'guava'])]
<dictionary-itemiterator object at 0x7f4b697bdc00>
dict_items([('a', 'apple'), ('c', 'cherry'), ('b', 'banana'), ('d', 'dates'), ('g', ['grapes', 'guava'])])

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])


139961978541768 139961978548168
[['ant', 'ball', 'call'], ['den', 'egg', 'fin']] 139961978541912
139961978541768 139961978548168

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])


[['ant', 'ball', 'call'], ['den', 'egg', 'fin']] 139961978541912 139961978541768 139961978548168
[['ant', 'ball', 'call'], ['den', 'egg', 'fin']] 139961978541912 139961978541768 139961978548168

In [37]:
# hard copy
import copy
print dir(copy)


['Error', 'PyStringMap', '_EmptyClass', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_dispatch', '_copy_immutable', '_copy_inst', '_copy_with_constructor', '_copy_with_copy_method', '_deepcopy_atomic', '_deepcopy_dict', '_deepcopy_dispatch', '_deepcopy_inst', '_deepcopy_list', '_deepcopy_method', '_deepcopy_tuple', '_keep_alive', '_reconstruct', '_test', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']

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])


[['ant', 'ball', 'call'], ['den', 'egg', 'fin']] 139961978541912 139961978541768 139961978548168
[['ant', 'ball', 'call'], ['den', 'egg', 'fin']] 139961978547376 139961978547160 139961978544424

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])


[['ant', 'ball', 'call'], ['den', 'egg', 'fin']] 139961978541912 139961978541768 139961978548168
[['ant', 'ball', 'call'], ['den', 'egg', 'fin']] 139961869099736 139961978541768 139961978548168

In [41]:
#pop
print my_fruits


{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['grapes', 'guava']}

In [42]:
print help(my_fruits.pop)


Help on built-in function pop:

pop(...)
    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
    If key is not found, d is returned if given, otherwise KeyError is raised

None

In [43]:
print my_fruits.pop('g')
print my_fruits


['grapes', 'guava']
{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'dates'}

In [44]:
# popitem
print help(my_fruits.popitem)


Help on built-in function popitem:

popitem(...)
    D.popitem() -> (k, v), remove and return some (key, value) pair as a
    2-tuple; but raise KeyError if D is empty.

None

In [45]:
print my_fruits.popitem()


('a', 'apple')

In [46]:
print my_fruits.popitem()


('c', 'cherry')

In [47]:
print my_fruits


{'b': 'banana', 'd': 'dates'}

In [49]:
# clear
print my_fruits
print my_fruits.clear()
print my_fruits


{'b': 'banana', 'd': 'dates'}
None
{}

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)