PYTHON
SETS & DICTIONARY
Fill in the blanks
1. Set will allow duplicate values .[T/F]. False
2.a={}. print(type(a))= <class 'dict'>
3.a={2,2,2,3,4,45,5,5,5,5}.print(type(a)) = <class 'set'>
4.a={1,3}.a.add(5) = {1,3,5}
5.a={1,2,3}.a.update([4,5],{1,6,8}) = {1,3,4,5,6,8}
6.N={1,2,3}.N.discard(3) = {1,2}
7.a={'A','E','I','O','U'}. print(a.pop()) = A
8.discard not raise any error if element not found in the set.[T/F]. True
9.Write a syntax of dictionary key:value
10.a={'name':'kavin','age':15}.a['age']=20. print(a) {'name':'kavin','age':'20'}
11.s={1:1,2:8,3:27,4:64}.print(s.pop(4)) = 64
12.a={'flower':1},b={'flower':1}.cmp(a,b) = 0
13.a={1:'apple',2:'mango'}.a.keys() = [1,2]
14. a={1:'apple',2:'mango'}.a.values() = ['apple','mango']
15. a={1:'apple',2:'mango'}.a.items() = [(1,'apple'),(2,'mango')]
16. num={1:1,2:4,3:9,4:16,5:25}.print(6 in num) = False
17. num={1:1,2:4,3:9,4:16,5:25}.print(4 in num) = True
18. a={1:'apple',2:'mango'}.a.len() = 2
19. a={1:'apple',2:'mango'}.a.has_key('flower') = 0
20.a={3,4,5,6,7}.b={3,6,8,9,1}. print(a&b) = set([3,6])
Answer the following questions
(4 marks)
1.Write a program for set union,set intersection,set difference,set symmetric difference
Python set Operations
> > >
SET_A = {3,4,5,6,7}
> >
>SET_B = {3,6,8,9,1}
Set Union
SA = {3,4,5,6,7}
SB = {3,6,8,9,1}
#Using / operator
print(SA|SB)
#Using union() method
print(SA.union(SB))
OUTPUT
set([1, 3, 4, 5, 6, 7, 8, 9])
set([1, 3, 4, 5, 6, 7, 8, 9])
Set Intersection
SA = {3,4,5,6,7}
SB = {3,6,8,9,1}
Using & operator
print (SA & SB)
Using intersection() method
print(SA.intersection(SB))
OUTPUT
set([3, 6])
set([3, 6])
Set Difference
SA = {3,4,5,6,7}
SB = {3,6,8,9,1}
Element unique to SA
print("Element only in SA set
")
Using - operator
print(SA - SB)
Using difference () method
print(SA.difference(SB))
Element unique to SB
print("Element only in SA set
")
print(SB - SA)
Print (SB.difference(SA))
OUTPUT
Element Only in SA set
set([4, 5, 6])
Element Only in SB set
set([8, 1, 9])
Set Symmetric
Difference
Element not common
in both sets
SA = {3,4,5,6,7}
SB = {3,6,8,9,1}
Using ^ operator
print(SA ^ SB)
Using symmetric_difference() method
print(SA. symmetric_difference(sb))
OUTPUT
set([1, 4, 5, 7, 8, 9])
set([1, 4, 5, 7, 8, 9])
2.Write a program for set built in function
Nset = {1,2,3,4,5,6,7,8,9,}
print("\n actual set value
=",Nset)
print("\n maximum of Nset ={}".format(max(Nset)))
print("\n minimum of Nset =
{}".format(min(Nset)))
print("\n sum of Nset =
{}".format(sum(Nset)))
print("\n any false value in Nset
={} is ".format(any(Nset)))
print("\n all true value in Nset =
{}", .format(all(Nset)))
print("\n sorted value of Nset =
{} ".format(sorted(Nset)))
print("\n length of Nset = {}".format(len(Nset)))
3.Write a program for dictionary built-in function
Built - in method
> > > dict2 = {'country':
'india','code':91 }
> > > dict2.has_key('country')
1
> > >
> > > dict2['country']
'india'
> > >
> > > dict2.has_key('number')
0
> > > dict2.keys()
['country', 'code']
> > >
> > > dict2.values()
[91, 'india']
> > >
> > > dict2.items()
(['code',91), ('country', 'india')]
> > >
> > > for eachkey in
dict2.keys():
... print 'dict2 key', eachkey, 'has
valus',
dict2[eachkey]
...
> > > dict2keys = dict2.keys()
> > > dict2keys.sort()
> > > for eachkey in
dict2keys:
... print 'dict2
key', eachkey, 'has value',
dict2 [eachkey]
...
dict2 key country has value india
dict2 key code has value 91
4.Write a program for dictionary method
marks = {}.fromkeys(['math','english','science'], 0)
print(marks)
Output: {'English': 0, 'math': 0,
'science': 0}
for item in marks.item():
print(item)
list(sorted(marks.keys()))
Output:
('science', 0)
('math', 0)
('english', 0)]
Answer the following questions
(10 marks)
1.Write a program to find the two team players details using SET
Team_A = set()
Team_B = set()
print("\n Enter playar name to add
in Team A/n")
While True:
str
= input()
Team_A.add(str)
ch
= input("Add[y/n]
if
ch in {'n', 'N'}:
break
print("\n Enter playar name to add
in Team B/n")
While True:
str
= input()
Team_B.add(str)
ch
= input("Add[y/n]
if
ch in {'n', 'N'}:
break
print ("/n Team A Players
List:",Team_A)
print ("/n Team B Players
List:",Team_B)
print ("/n Players only in Team A:
", Team_A:".difference(Team_B))
print ("/n Players only in Team B:
", Team_B:".difference(Team_A))
print ("/n Players in both Teams:
",Team_A.intersection (Team_B))
No comments:
Post a Comment