Python Programs: Tuples and
Dictionaries
Tuple Programs
1. #WAP to create a tuple by accepting elements from the user using a for
loop
Input:
t = tuple()
n = int(input("Enter any number: "))
for i in range(n):
a = input("Enter number: ")
t = t + (a,)
print("Output is:", t)
Output:
2. WAP to create a tuple by accepting elements from the user using a while
loop
Input:
t = tuple()
n = int(input("Enter number of elements: "))
i=1
while i <= n:
a = input("Enter number: ")
t = t + (a,)
i += 1
print("Output is:", t)
Output:
3. WAP to store 'n' numbers of subjects in a tuple
Input:
t = tuple()
n = int(input("How many subjects do you want to add? "))
print("Enter all subjects one after another:")
for i in range(n):
a = input("Enter subject: ")
t += (a,)
print("Output is:", t)
Output:
4. WAP to create a nested tuple to store roll number, name, and marks of
students
Input:
st = ((200, "Harmeet", 88), (201, "Deepika", 98), (202, "Radhika", 78), (204, "Shaurya", 90))
print("S_No\tRoll_no\tName\tMarks")
for i in range(len(st)):
print(i+1), '\t', st[i][0], '\t', st[i][1], '\t', st[i][2])
Output:
5. WAP to print all elements of a tuple in reverse order.
Input:
tup = (10, 20, 30, 40, 50)
print("Original tuple:", tup)
print("Tuple elements in reverse order:")
for i in range(len(tup) - 1, -1, -1):
print(tup[i])
Output:
Dictionary Programs
1. WAP to input total number of sections in streams in 11th class and display
all information.
Input:
classxi = dict()
n = int(input("Enter total number of sections in XI class: "))
i=1
while i <= n:
a = input("Enter section: ")
b = input("Enter stream name: ")
classxi[a] = b
i += 1
print("Class\tSection\tStream Name")
for i in classxi:
print("XI\t", i, "\t", classxi[i])
Ouput:
2. Write a program to store students' information like admission number, roll
number, name, and marks in a dictionary.
Input:
SCL = dict()
n = int(input("Enter number of entries: "))
for i in range(n):
Adm = input("Enter admission number of a student: ")
nm = input("Enter name of a student: ")
section = input("Enter class and section: ")
per = float(input("Enter percentage of a student: "))
SCL[Adm] = (nm, section, per)
print("\nAdmno - Details")
for i in SCL:
print("\nAdmno -", i, ":")
print("Name\tClass\tPercentage")
print(SCL[i][0], "\t", SCL[i][1], "\t", SCL[i][2])
Output:
3. Program to create a dictionary with names of employees and their
salaries.
Input:
num = int(input("Enter the number of employees whose data to be stored: "))
employee = dict()
for _ in range(num):
name = input("Enter the name of the employee: ")
salary = int(input("Enter the salary: "))
employee[name] = salary
print("\nEMPLOYEE_NAME\tSALARY")
for k, v in employee.items():
print(k, "\t", v)
Output:
4. Program to count the number of times a character appears in a given
string.
Input:
string = input("Enter a string: ")
dict1 = {}
for ch in string:
dict1[ch] = dict1.get(ch, 0) + 1
print("Character Frequency:")
for key, value in dict1.items():
print(key, ":", value)
Output:
5. Write a Python program to copy a dictionary and display both the original
and copied dictionaries with their memory locations."
Input:
D1 = {'Name': 'Radhika', 'DOB': '2002-03-11', 'Marks': '98'}
print("D1:", D1)
D2 = D1.copy()
print("D2:", D2)
print("Location of D1:", id(D1))
print("Location of D2:", id(D2))
Output:
THANK
YOU