Agenda = '''
Dictionary
Dictionary Case Study
iterator, generator
Built in Functions
lambda,
filter,
map,
set,
enumerate,
sorted,
reversed,
range
zip,
sum,
max,
min
List Comprehension
Dictionary Comprehension
'''
Dict1 = {}
Dict1['name'] = 'Surendra'
Dict1
Dict1[['city','state']] = ['Pune','Maharashtra']
Dict1[('city','state')] = ['Pune','Maharashtra']
Dict1
Dict1.keys()
Dict1.values()
Dict1[41] = {'a' : 'apple','b' : 'banana'}
Dict1
Fruits = { 'o' : 'orange', 'g' : 'grape' }
Fruits.update(Dict1)
Fruits
Fruits.keys()
Fruits.get('o')
dir(Fruits)
Fruits.pop(('city', 'state'))
Fruits
Fruits.popitem()
Fruits
Fruits['name'] = 'Fruits_name'
Fruits
1. Create Library Application using Dictionary
2. Create Menu for Books, Publishers and Authors
3. Display Many to Many Relationship between Book and Authors.
4. Display One-to-One Relationship between Book and publisher.
5. Create Library Dictionary using Books Dictionary
6. Update Library Dictionary by updating Books keys and values.
7. Access Second Author First Name and Update with new name.
8. Display list of Authors, Publishers, Titles from
Library Dictionary
9. Take Input Data from User using Menu and Input statement and Update Library Dictionary.
Menu = '''
1. Create Library Application using Dictionary
2. Create Menu for Books, Publishers and Authors
3. Display Many to Many Relationship between Book and Authors.
4. Display One-to-One Relationship between Book and publisher.
5. Create Library Dictionary using Books Dictionary
6. Update Library Dictionary by updating Books keys and values.
7. Access Second Author First Name and Update with new name.
8. Display list of Authors, Publishers, Titles from
Library Dictionary
9. Take Input Data from User using Menu and Input statement and Update Library Dictionary.
'''
print(Menu)
while True:
op = int(input("Enter your choice:"))
if op == 1:
print("Create Library Application using Dictionary")
elif op == 2:
print("Create Menu for Books, Publishers and Authors")
elif op == 3:
print("Display Many to Many Relationship between Book and \
Authors.")
elif op == 4:
print("Exit")
break
else:
print("Select 1 - 9 option only")
run BooksApp.py
run Bookdict.py
# %load BooksApp.py
'''
Dictionary Application will consist of
Books Dictionay,
Authors Dictionary and
Publishers Dictionary. This will include relation of
Authors with Book ( Many to Many )
Relation of Publisher with Book ( One to One )
'''
# Step 1. Create Publisher Dictionary
print("*** Publishers Dictionary ****\n")
Publisher1 = { 'pid' : 1, 'pname' : 'Orielly Pub',
'pcity' : 'Chennai',
'pweb' : 'https://www.orielly.org' }
print(Publisher1)
Publisher2 = { 'pid' : 2, 'pname' : 'BPB Pub',
'pcity' : 'Delhi',
'pweb' : 'https://www.bpb.org' }
print(Publisher2)
Publisher3 = { 'pid' : 3, 'pname' : 'TataMcgraw Pub',
'pcity' : 'Mumbai',
'pweb' : 'https://www.tatamcgraw.org' }
print(Publisher3)
Publishers = {}
Publishers['P1'] = Publisher1
Publishers['P2'] = Publisher2
Publishers['P3'] = Publisher3
print("\n")
print("*** Publishers Dictionary ****\n")
print(Publishers)
# Step2 Create Authors Dictionary
print("\n")
print("*** Authors Dictionary ****\n")
Author1 = {'first_name' : 'Surendra',
'last_name' : 'Panpaliya',
'city' : 'Pune',
'email' : 'surendra@gktcs.com' }
print(Author1)
Author2 = {'first_name' : 'Narendra',
'last_name' : 'Panpaliya',
'city' : 'Pune',
'email' : 'narendra@gktcs.com' }
print(Author2)
Author3 = {'first_name' : 'Satish',
'last_name' : 'Panpaliya',
'city' : 'Chicago',
'email' : 'satish@gktcs.com' }
print(Author3)
Authors = { }
Authors['A1'] = Author1
Authors['A2'] = Author2
Authors['A3'] = Author3
print("\n")
print("*** Authors Dictionary ****\n")
print(Authors)
print("*** Books Dictionary ****\n")
print("\n")
Book1 = { 'title' : 'Bytes of Python',
'publisher' : Publishers['P1'],
'authors' : [ Authors['A1'], Authors['A2'] ],
'pub_date' : '21-09-2019' }
Book2 = { 'title' : 'Django',
'publisher' : Publishers['P2'],
'authors' : [ Authors['A2'], Authors['A3'] ],
'pub_date' : '20-09-2019' }
Book3 = { 'title' : 'Data Science',
'publisher' : Publishers['P3'],
'authors' : [ Authors['A1'], Authors['A3'] ],
'pub_date' : '23-09-2019' }
Books = { }
Books['B1'] = Book1
print(Books['B1'])
Books['B2'] = Book2
Books['B3'] = Book3
print("**** Books Library***")
Books
Books.keys()
Books['B2'].keys()
Books['B2']['authors'][1]
Books['B2']['authors'][1]['last_name']
Books['B2']['authors'][1]['last_name'] = 'Rathi'
Books['B2']['authors']
for var in ['data1','data2','data3']:
print(var)
list(range(1,25,2))
# Generator are functions which create iterators.
def gy(arg1, arg2):
'''Generator Function'''
yield arg1
yield arg2
yield arg1 + arg2
yield arg1 - arg2
yield arg1 * arg2
yield arg1 / arg2
yield arg1 % arg2
yield arg1 ** arg2
return
g1 = gy(10, 2)
g1
#dir(g1)
print(g1.__next__())
print(g1.__next__())
print(g1.__next__())
print(g1.__next__())
print(g1.__next__())
print(g1.__next__())
print(g1.__next__())
print(g1.__next__())
print(g1.__next__())
g2 = gy(20, 4)
for item in g2:
print(item)
g3 = gy(30, 4)
list(g3)
def fibonacci(maxnum):
'''Fibonacci of any number '''
first, second = 0, 1 # parallel assignment
for i in range(0,maxnum):
yield first
first, second = second, first + second
f1 = fibonacci(20)
list(f1)
Lambda is annonymous single line mathematical function.
It looks like mathematical expression.
add = lambda arg1, arg2 : arg1 + arg2
add
add(45, 23)
import this
filter is a function which takes two arguments
1. function itself
2. sequence ( list, tuple, str)
It return those values from sequence where function is True.
list(filter(lambda arg1 : arg1 % 2 !=0 and arg1 % 3 !=0,
range(2,25)))
fun1 = lambda arg1 : arg1 % 2 !=0 and arg1 % 3 !=0
fun1(2)
fun1(5)
list(range(2,25))
Salary = [200000, 300000, 250000, 400000, 500000,
600000, 100000, 50000, 20000 ]
Filter Salary Sequence with Salary greater than 20,000
( thousand) and
less than 300000 ( 3 lakh)
Salary = [200000, 300000, 250000, 400000, 500000,
600000, 100000, 50000, 20000 ]
fun2 = lambda arg : arg > 20000 and arg < 300000
print(fun2(50000))
list(filter(fun2, Salary))
map is a function which takes two or more arguments
1. function itself
2. One or more sequences ( list, tuple, str)
3. Number of arguments to function are the same as
number of sequences.
4. Apply functions to one or more sequences
cube = lambda arg : arg**3
cube(3)
list(map(cube, Salary))
Salary = [200000, 300000, 250000, 400000, 500000,
600000, 100000, 50000, 20000 ]
Salary1 = [100000, 400000, 350000, 400000, 500000,
600000, 100000, 50000, 70000 ]
list(map(lambda arg1, arg2 : arg1 + arg2 , Salary, Salary1))
Set is unorder collection of unique and immutable objects.
Salary = [200000, 300000, 250000, 400000, 500000,
600000, 100000, 50000, 20000, 300000, 250000, 400000 ]
Salary1 = [100000, 400000, 350000, 400000, 500000,
600000, 100000, 50000, 70000, 300000, 250000, 400000]
S1 = set(Salary)
print(S1)
S2 = set(Salary1)
print(S2)
print("****** Union of Set****** \n")
print(S1.union(S2))
print("****** Union of Set bit wise ****** \n")
S3 = S1 | S2
print(S3)
print("\n ****** Intersection of Set****** \n")
print(S1.intersection(S2))
print("\n ****** Intersection of Set bit wise ****** \n")
print(S1 & S2)
print("\n ***** Super Set ******* \n ")
print(S3.issuperset(S1))
print("\n ***** Sub Set ******* \n ")
print(S1.issubset(S3))
sorted(S1)
sorted(S3)
sorted(S3, reverse = True)
sorted(Salary)
list(reversed(Salary))
L1 = list(reversed('SUREDNRA'))
print(L1)
Str2 = ':'.join(L1)
print(Str2)
Str1 = ' '.join(L1)
print(Str1)
List2 = Str2.split(':')
print(List2)
Auto Indexing sequence
list(enumerate(Salary,1))
print(list(enumerate('SURENDRA PANPALIYA',1)))
print("\n")
print(list(enumerate('GKTCS Innovations',1)))
print(len(Salary))
print(sum(Salary))
print(max(Salary))
print(min(Salary))
list(zip(Salary, Salary1))
list(zip('SURENDRA', 'PANPALIYA'))
1. We write comprehensive script inside list
2. We can extract or apply on sequence without using filter or map or lambda.
[ num for num in Salary ]
[ num**2 for num in Salary ]
[ num for num in Salary if num > 20000 and num < 300000 ]
[ num for num in range(2, 25) if num % 2 != 0 and num % 3 != 0 ]
[ (num, num**3) for num in range(2, 25)
if num % 2 != 0 and num % 3 != 0 ]
Try Nested for loop inside list .
Words = [ 'abc','def', 'ghi', 'jkl', 'mno', 'pqr' ]
['a','b','c',......'r' ]
1. We write comprehensive script inside Dictionary
2. We can extract or apply on sequence without using filter or map or lambda.
Fruits
{ key : value for key, value in Fruits.items() }
{ value : key for key, value in Fruits.items() }
{ value.upper() : key.upper() for key, value in Fruits.items() }