✅ Python Practice – Corrected Mini Programs (List, Tuple, Set, Dictionary)
🔢 List Programs
1. Create a list of numbers; calculate sum and average (and remove duplicates):
listB = [21, 33, 10, 56, 10, 45, 20, 33]
print("Original List:", listB)
print("Sum:", sum(listB))
print("Average:", sum(listB) / len(listB))
unique_listB = list(set(listB))
print("Unique List:", unique_listB)
2. Accept and print numbers in reverse order:
listA = []
n = int(input("Enter length of listA: "))
for i in range(n):
listA.append(int(input(f"Enter number {i + 1}: ")))
print("Reversed List:", listA[::-1])
3. Find max and min in a tuple:
tup1 = (132, 220, 300, 200, 50)
print("Maximum:", max(tup1))
print("Minimum:", min(tup1))
4. Count occurrences of a specific element in a tuple:
tup2 = (20, 60, 40, 14, 32, 10, 89, 10, 30, 40, 10)
print("Count of 10:", tup2.count(10))
5. Accept numbers, calculate sum and average:
listA = []
s=0
n = int(input("Enter length of listA: "))
for i in range(n):
num = int(input("Enter a number: "))
listA.append(num)
s += num
a=s/n
print("Sum:", s)
print("Average:", a)
🔁 Set Programs
1. Perform union, intersection, and difference between sets:
set1 = {1, 30, 55, 40, 20, 41, 86, 12, 10}
set2 = {21, 36, 12, 51, 55, 98, 41, 63, 45}
print("Union:", set1.union(set2))
print("Intersection:", set1.intersection(set2))
print("Difference (set1 - set2):", set1.difference(set2))
print("Difference (set2 - set1):", set2.difference(set1))
2. Convert list to set to remove duplicates:
list_a = [12, 30, 66, 20, 50, 10, 80, 14, 23, 60, 40, 32, 11, 80]
unique_list = set(list_a)
print("Unique elements:", unique_list)
3. Check if one set is a subset/superset of another:
set1 = {10, 20, 65}
set2 = {56, 30, 45, 10, 78, 20, 69, 65}
print("set1 is subset of set2:", set1.issubset(set2))
print("set2 is subset of set1:", set2.issubset(set1))
print("set1 is superset of set2:", set1.issuperset(set2))
print("set2 is superset of set1:", set2.issuperset(set1))
4. Find symmetric difference between sets:
print("Symmetric Difference:", set1.symmetric_difference(set2))
Dictionary Programs
1. Create a dictionary of students and marks; print the topper:
dict_stud = {'stud1': 89, 'stud2': 98, 'stud3': 96, 'stud4': 84, 'stud5': 91,
'stud6': 99}
topper = max(dict_stud, key=dict_stud.get)
print("Topper:", topper, "with marks:", dict_stud[topper])
2. Count the frequency of each word in a sentence:
sentence = input("Enter a sentence: ")
words = sentence.split()
word_freq = {}
for word in words:
word_freq[word] = word_freq.get(word, 0) + 1
for word, count in word_freq.items():
print(word, ":", count)
3. Merge two dictionaries:
dicta = {1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five"}
dictb = {6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", 10: "Ten"}
dictc = {**dicta, **dictb}
print(dictc)
4. Update dictionary with new key-value pairs:
dicta.update(dictb)
print(dicta)
5. Display all keys and values using loops:
for key, value in dict_stud.items():
print(key, ":", value)