In [ ]:
# Learning Python the Hardway ( 2 weeks/3 weeks)
In [ ]:
# dictionaries
# dict,hashes.
# string,tuples,lists [indexed objects]
# Dictionaries are not indexed objects -> key,value
# apple => red color fruit
# cherry => red color fruit
# apple,cherry(keys) -> unique
# red colour fruit(values)
# SSN,UIN
# json,yaml,pickle..
In [20]:
# Dictionary
# pprint
# dictionary indexes
my_fruits = {'a':'apple','b':'banana','c':'chocolate','d':'dates'}
print my_fruits,type(my_fruits)
In [9]:
# how to read a value provided we have key
print my_fruits['d']
In [3]:
my_empty = {}
print my_empty,type(my_empty)
my_empty = dict()
print my_empty,type(my_empty)
In [4]:
# insert values into dictionries
my_fruits['f']='figs'
print my_fruits
In [6]:
# replace
my_fruits['a']=['apricot','apple']
print my_fruits
In [10]:
# recursion
# we get keys while running a for loop on a dictionary.
for key in my_fruits:
print "{} => {}".format(key,my_fruits[key])
In [12]:
# in operation
# in operator works only on keys in a dictionary.
print 'a' in my_fruits
print 'banana' in my_fruits
In [13]:
# functions
print dir(my_fruits)
In [15]:
# function
# get
print help(my_fruits.get)
print my_fruits.get('a')
print my_fruits['a']
In [16]:
# has_key
print help(my_fruits.has_key)
print my_fruits.has_key('a')
print 'a' in my_fruits
In [18]:
# keys,iterkeys,viewkeys
#keys
print help(my_fruits.keys)
print my_fruits.keys()
In [19]:
# iterkeys
print help(my_fruits.iterkeys)
print my_fruits.iterkeys()
In [20]:
for key in my_fruits.iterkeys():
print key
In [3]:
# viewkeys
print help(my_fruits.viewkeys)
print my_fruits.viewkeys()
In [6]:
# values,itervalues,viewvalues
# value
print my_fruits.values()
# itevalues
print my_fruits.itervalues()
for value in my_fruits.itervalues():
print value
# viewvalues
print my_fruits.viewvalues()
for value in my_fruits.viewvalues():
print value
In [8]:
# items,iteritems,viewitems
print my_fruits.items()
print my_fruits.iteritems()
print my_fruits.viewitems()
In [ ]:
# copy
# soft,hard,shallow copy(complex objects)
In [15]:
a = ["one","two","three"]
b = ["four","five","six"]
print id(a),id(b),a,b
Cc = [a,b]
print Cc,id(Cc)
print id(Cc[0]),id(Cc[1]) # the elements of complex objects are refering to a and b.
SOc = Cc
print id(SOc),id(Cc)
print id(Cc[0]),id(Cc[1]),id(SOc[0]),id(SOc[1])
print SOc is Cc
# Any modification to any one a and b should reflect in SOc and Cc.
# deep copy
# two different memory location. any modification in Cc does not refelect in Dc.
import copy
Dc = copy.deepcopy(Cc)
print id(Dc),id(Cc)
print id(Cc[0]),id(Cc[1]),id(Dc[0]),id(Dc[1])
# shallow copy
# its a mix of soft and deep copy.
# external object is deepcopy and internal are softcopy.
print help(copy.copy)
Sc = copy.copy(Cc) # shallowcopy
print id(Sc),id(Cc)
print Sc is Cc
print id(Cc[0]),id(Cc[1]),id(Sc[0]),id(Sc[1])
#
# traings = [['linux','python','Puppet','django'],['2:00','3:00','4:00','5:00']] - page1
# costing = [['linux','python','Puppet','django'],['20.00','30.00','40.00','50.00']] - page2
In [21]:
# clear
Dmy_fruits = copy.deepcopy(my_fruits)
print Dmy_fruits
print Dmy_fruits.clear()
print Dmy_fruits
print my_fruits
In [27]:
# pop
print help(my_fruits.pop)
my_fruits.pop('c')
print my_fruits
In [28]:
print my_fruits
my_fruits.pop('c')
In [29]:
# my_fruits.popitem
print my_fruits.popitem()
In [24]:
# Project
# https://drive.google.com/file/d/0Bxq4UvUJTuQPMWZKd2d5ZlBoY00/view
In [23]:
my_square = {}
for key in range(1,10):
my_square[key] = key * key
print my_square.values()
In [ ]: