0% found this document useful (0 votes)
3 views31 pages

Tuples and Dictionary Operations Guide

The document contains multiple Python programs demonstrating various operations and functions on tuples and dictionaries. It covers topics such as tuple concatenation, repetition, indexing, membership, and dictionary operations like accessing elements, updating, and deleting. Each program includes user input and outputs the results of the operations performed.

Uploaded by

souldream625
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

Tuples and Dictionary Operations Guide

The document contains multiple Python programs demonstrating various operations and functions on tuples and dictionaries. It covers topics such as tuple concatenation, repetition, indexing, membership, and dictionary operations like accessing elements, updating, and deleting. Each program includes user input and outputs the results of the operations performed.

Uploaded by

souldream625
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tuples And Dictionary

Q1WAP to demonstrate the following in tuples: Concatenation , Repetition ,


#Traversal,indexing , counting,slicing,membership, deletion
Sol: -
Input: -
#WAP to demonstrate the following in tuples: Concatenation , Repetition ,
#Traversal, indexing , counting ,slicing, membership, deletion
t= (1,2,3,4,5,6,7,8,9)
print("This is the tuple we are working with: ",t)
#Conactenation
t2=("a",'b','c','d')
print("We will add this tuple",t2,"to the tuple we are working with."
"\nThis is called concatenation")
print("This is concatinated tuple: ",t+t2)
#Repetition
print("This is repeted tuple 2 times: ",t*2)
#Traversal
print("Traversing this tuple: ",t)
for i in range(0,len(t)+1):
print(i)
#Indexing
print("Indexing")
print("To get only 1 number from tuple we use indexing."
"\nFor eg we want 8 we will use x=[Link](value)")
x=[Link](9)
print(x)
#Counting
print("For counting we will take another tuple")
t3=(1,1,2,3,5,5,6,7,7,7,2,0)
print(t3)
print("For counting a number we use [Link](number)")
a=[Link](7)
print(a)
#Slicing
print("Slicing")
t4=t[0:10:2]
print(t4)
#Membership
print("we use 'in' or 'not in', in membership")
print('1 in t: ',1 in t)
print("11 in t: ",11 in t)
print("11 not in t: ", 11 not in t)
#Deletion
print("We delete the whole tuple""\nThis is called Deletion")
del t
print("after using 'del t' it doesnt exist and gives error if we try to print")
print(t)
Output:-

Q2 Wap to demonstrate the following functions on tuples:


Len,Count,Any,min,Max,sum,sorted,index
Sol :-
Input :-
Wap to demonstrate the following functions on tuples:
#Len,Count,Any,min,Max,sum,sorted,index
t=(1,2,3,4,5,6,7,8,9)
print(t,"This is our tuple")
#Len
print("Length of tuple is: ",len(t))
#Count
t2=(1,2,2,3,4,5,6,6,6,7,0)
print(t2,"This is the tuple we are using to perform count function")
print("The no. 6 occurs: ",[Link](6),"times")
#Any
print("Any functions returns true if we have atleast 1 item inside a tuple")
t3=(1,2)
t4=()
print(t3,"A tuple")
print(t4,"An empty tuple")
print(any(t3))
print(any(t4))
#Min
print(t,"This is the function we will use to show minimum value in it")
print(min(t),"is the minimum value in this tuple")
#Max
print(t,"This is the function we will use to show maximum value in it")
print(max(t),"is the maximum value in this tuple")
#Sum
print(t,"This is the function we will use to show the sum function")
print(sum(t),"is the sum of this tuple")
#Sorted
t5=(1,32,45,59,209,321,439)
print(t5,"We will use this tuple to show Sorted function")
print(sorted(t5))
print("The tuple is in ascending form now")
#Index
print(t,"We will show index function in this")
print([Link](8))
print("It shows the index of the item 8 in tuple")
Output:-

Q3 WAP to find repeated element in the tuple entered by the user


Sol :-
Input :-
#Wap a to find repeated element entered by the user
t=()
N=int(input("Enter the length of the tuple: "))
for i in range(0,N):
x=int(input('Enter the element: '))
t+=(x,)
print(t,'This is your tuple')
count=0
a=int(input("Enter element u want to find if repeated: "))
for j in range(0,len(t)):
if a==t[j]:
count+=1
else:
pass
if count>1:
print("the number",a,"is repeated",count,"times")
elif count==1:

print("The number",a,"is not repeated")


Output :-

Q4 Wap to create a tuple of integers and find wether all elements are in ascending order or
not.
Sol :-
Input:-
#Wap to create a tuple of integers and find wether all elements are in
#ascending order or not.
t=()
N=int(input("Enter the length of the tuple: "))
for i in range(0,N):
x=int(input('Enter the element: '))
t+=(x,)
print(t,'This is your tuple')
a=1
for j in range(0,len(t)-1):
if t[j]>t[j+1]:
a=2
else:
pass
if a==1:
print("Tuple is in ascending order")
else:
print("Tuple is not in ascending order")

Output:-
#WAP to create a nested tuple of n elements where each element of nested tuple
contains (code, name, age).Create Dynamic Tuple
t = []
n = int(input("Enter number of elements: "))
for i in range(0,n):
code = int(input("Enter code: "))
name = input("Enter name: ")
age = int(input("Enter age: "))
t2 = (code, name, age)
[Link](t2)
t= tuple(t)
Q5 WAP to create a nested tuple of n elements where each element of nested tuple contains
(code, name, age).Create Dynamic Tuple.
Sol:-
Input: -
print(t)

Output :-
Q6 Wap to create a tuple of heterogenous elements. Take elements from the user. Now search
for a particular string in the tuple and show its index .Also show a message “Search
unsuccessful” if element is not found
Sol :-
Input :-
#Find a given string from the user generated tuple
t=()
N=int(input("Enter the length of the tuple: "))
for i in range(0,N):
x=eval(input('Enter the element: '))
t+=(x,)
print("Tuple: ",t)
s=input("Enter the string you want to find: ")
a=0
for i in range(0,len(t)):
if t[i]==s:
a=1
break
else:
pass
if a==1:
print("The string",s,"is found at index: ",i)
else:
print('Search unsuccessful')

Output :-
Q7 Wap to create a tuple of heterogenous elements. Take elements from user. Now find sum
of all such elements which are inetgers and are ending in 2.
Sol :-
Input :-
t=()
n=int(input("Enter number of elements :"))

for i in range (n):


x=eval(input("Enter elements :"))
t+=(x,)
print(t)
s=0
for j in range (n):
if type(t[j])==int and t[j]%10==2:
s+=t[j]
print(s)
Output :-
Q8Wap to create a tuple having cubes of numbers entered by the user. The user should be
prompted to enter N number of numbers, cubes to be calculated for each number and added
to tuple. Show Final tuple
Sol :-
Input :-
#Enter cubes of element given by user in the tuple
t=()
t2=()
n = int(input("Enter the length: "))
for i in range(0,n):
x = int(input("Enter element: "))
cube = x**3
t+=(x,)
t2+=(cube,)
print("Original tuple: ",t)
print("Cube list: ",t2)

Output:-
Q9WAP to demonstrate the use of the following built in functions on a
dictionary :len(),items(),keys(),vales(),get(), fromkeys(),copy(),
setdefault(),update(),pop(),popitem(),
Clear(),sorted on keys, sorted on values, sorted on items, sum(),min(),max()
Sol :-
Input:-
#perform following
#len()
print("len")
d={'R':'Rainy','S':'Summer','W':'winter','A':"Autumn"}
print("This is our dictionary: ",d)
print(len(d))
#Items
print("Item")
print([Link]())
#Keys()
print("Keys")
print([Link]())
#Values()
print("Values")
print([Link]())
#get()
print("get")
print([Link]('W'))
#From Keys
print("Fromkeys")
K=[1,2,3,4]
V1=1000
print("Our keys will be: ",K,"\nOur value is: ",V1)
print([Link](K,V1))
#copy
print("copy")
d2={}
print("d2 is our empty dictionary which will get values from d1 after copy")
d2=[Link]()
print('d2=', d2)
#setdefault()
print("setdefault")
d3={'name':'Shaurya','Gender':"Male"}
print(d3," we will use this for setdefault")
name=[Link]('name','Name not available')
dob=[Link]('dob',"Date not available")
gender=[Link]('Gender')
print("Name: ",name)
print("DOB: ",dob)
print("Gender: ",gender)
#Update
print("update")
[Link](d3)
print(d)
#pop
print("pop")
print([Link]('name'), "we used pop on d3 on the key","name")
#popitem
print("popitem")
s={'roll no':10,"name":"harsh","Stream":'Science'}
print(s," new dictionary")
a=[Link]()
print(a,"removed from",s)
#clear
print("clear")
print("we will clear d3 entirely by using clear")
[Link]()
#sorted on keys
print("sorted on keys,values,items")
d4={'Student1' :80, 'Student2' : 78, 'Student3' :76}
L1=sorted(d4)
print(L1)
#sorted on values
L2=sorted([Link]())
print(L2)
#Sorted on items
L3=dict(sorted([Link]()))
print(L3)
#Sum
print("sum")
d5={'x': 25, 'y':18, 'z':45}
print("THis is out dictionary", d5)
print(sum(list([Link]())))
#Min
print("Min")
print("Lowest key with its value: " ,min([Link]()))
#Max
print("Max")
print("Highest key with its value; ", max([Link]()))
OUTPUT :-
Q10 Create a dynamic dictionary where keys are names of competitions and the values are
no of medal won in that competition. Now search for a particular competition and show the
number of medals, if found, else display an error message :’competition not found’
Sol :-
Input:-
#Find competition and medals in it.
d={}
n=int(input("Enter no of competitions: "))
i=1
while i<=n:
a=input("Competiton name: ")
b=int(input("Enter number of medals won: "))
d[a]=b
i=i+1
print(d)
c=input("Which competition you want to search: ")
if c in d:
print(d[c])
else:
print("Competiton not found")
Output:-
Q11 Wap to input names of ‘n’ employees along with their basic details like department,
basic salary ,HRA,DA ,Allowance store all in a dictionary “EMP”. Now calculate total
salary as sum of basic salary, HRA, DA and allowance and show for all employees
Sol:-
Input:-
#total salary of all employees
EMP = {}
n=int(input("Enter number of elements :"))
for i in range (n):
name=str(input("Enter name :"))
department=str(input("Enter department :"))
HRA=eval(input("Enter HRA :"))
DA=eval(input("Enter DA :"))
basic_salary=eval(input("Enter basic salary :"))
allowance=eval(input("Enter allowance :"))
total_salary=HRA+DA+basic_salary+allowance
EMP[name]=(department,HRA,DA,basic_salary,allowance)
print("Total salary of",name,"is",total_salary)
print(EMP)
Output :-
Q12 WAP to input ‘n’ names and phone numbers, store all in a dictionary “PHONE” .Now
search for a particular name and show all details.
Sol :-
Input:-
#Phone number
PHONE={}
n=int(input("Enter no of people: "))
i=1
while i<=n:
a=input("Name: ")
b=int(input("Phone No. : "))
PHONE[a]=b
i=i+1
print(PHONE)
c=input("Which person's phone no. you want: ")
if c in PHONE:
print("Here is the phone number of this person: ",PHONE[c])
else:
print("phone number not found")
Output:-

Q13 Demonstrate the following in a dictionary :


[Link] elements [Link] [Link] [Link] [Link] [Link]
Sol: -
Input: -
#Demonstrate the follwoing
#Accessing elements
print("Accessing elements")
d={'mon':'monday','tue':'tuesday','wed':'wednessday'}
print(d)
print([Link]('mon'))
#Appending
print("Appending")
d['thu']='thursday'
print(d)
#Traversal
print("Traversal")
print("shortname",'\t',"Fullname")
for i in d:
print(i,'\t','\t',d[i])
#Updating
print("Updating")
d2={'fri':'friday'}
print(d2)
[Link](d2)
print(d)
print("d2 was added to d")
#Membership
print("Membership")
print("thu in d will print true")
print('thu' in d)
#Deletion
print("Deletion")
print("Now i will delete d2 and it will show error after i try to print it")
del d2
print(d2)
Output:-

You might also like