Python Tuple and Dictionary





1       Write a program to accept elements in the form of a tuple and display its minimum, maximum,
sum and average.

tpl=(1,2,3,4)
print("Original tuple is " , tpl)
print("The minimum value is " , min(tpl))
print("The maximum value is ", max(tpl))
print("The sum value is ",sum(tpl))
avg=sum(tpl)/len(tpl)
print("The avarage value is  ",avg)
2

           Create a program to sort tuple with nested tuples. '''student tuple is created with roll no and
name and it is nested tuple'''


student = ( ( 101, 'abc'), (103, 'def') , ( 102, 'pqr' ) )
print ("Original nested tuple", student)

print("Sorted student tuple according roll no is")
print ( sorted (  student ))

print("Sorted student tuple according name is")
print ( sorted (student, key=lambda student: student [1] ) )

3       Write a program to create a dictionary from the user and display the elements.

student={'rollno':101,'name':'abc'}
print("The student dictionary is",student)

x=int(input("Enter emp id"))
y=input("Enter emp name")

emp={'id':x,'name':y}
print("The employee dictionary is ",emp)

4.Create a dictionary that will accept cricket players name and scores in a match. Also we are
retrieving runs by entering the player’s name.

cricket={'ply1':100,'ply2':101,'ply3':103}
name=input("enter player name")
if name in cricket:
    print("Run of ", name ,"player is ",cricket[name])
else:
    print("Cricket players details not available")
'''Write a program to convert the elements of two lists into
key-value pairs of a dictionary.'''

subject=['eng','maths','science']
marks=[40,45,60]
z=zip(subject,marks)
d=dict(z)
for k in d:
    print(k,d[k])



5. Create a python function to accept a dictionary and display its elements

def printdict(d):
    print(d)
d={'no':1,'name':'abc'}
printdict(d)