LISTS
Q1)List the steps to input elements into a list and find the sum of all elements
using Python.
INPUT:
n = int(input("Enter number of elements: "))
lst = [ ]
for i in range(n):
element = int(input("Enter element: "))
[Link](element)
total = sum(lst)
print("List:", lst)
print("Sum of elements:", total)
OUTPUT:
Q2)WAP to input a sentence and count the number of vowels in each word. Store the vowel count of
each word in a list.
INPUT:
sentence = input("Enter a sentence: ")
words = [Link]()
vowel_list = []
vowels = "aeiouAEIOU"
for word in words:
count = 0
for char in word:
if char in vowels:
count += 1
vowel_list.append(count)
print("Words:", words)
print("Vowel count list:", vowel_list)
OUTPUT:
Q3). WAP to input a sentence and find the length of each word. Store the length of each word in a
list.
INPUT:
sentence = input("Enter a sentence: ")
words = [Link]()
length_list = [ ]
for word in words:
length = 0
for ch in word:
length = length + 1
length_list.append(length)
print("Sentence:", sentence)
print("Words:", words)
print("Length of each word:", length_list)
OUTPUT:
Q4)Program to Search an Element in a List.
INPUT:
# Creating a list of fruits
fruits = ["apple", "banana", "mango", "grapes", "orange"]
print("List of fruits:", fruits)
item = input("Enter the fruit to search: ")
for fruit in fruits:
if fruit == item:
print(item, "is found in the list!")
break
else:
print(item, "is not found in the list.")
OUPUT:
TUPLES
Q1)Find the Maximum, Minimum and Sum of a Tuple
INPUT:
numbers_list = [ ]
print("Enter 5 numbers:")
for i in range(5):
num = int(input("Enter number "))
numbers_list.append(num)
numbers = tuple(numbers_list)
print("\nThe tuple you entered is:", numbers)
maximum = max(numbers)
minimum = min(numbers)
total = sum(numbers)
print("\n----- RESULTS -----")
print("Total elements in tuple:", len(numbers))
print("Maximum value in tuple:", maximum)
print("Minimum value in tuple:", minimum)
print("Sum of all numbers in tuple:", total)
average = total / len(numbers)
print("Average of numbers:", average)
print("\nDisplaying all elements of tuple one by one:")
for val in numbers:
print(val)
OUTPUT:
Q2) Accept Elements from User and Display Even Numbers
INPUT:
n = int(input("Enter number of elements: "))
elements = []
for i in range(n):
num = int(input("Enter element "))
[Link](num)
# Convert list to tuple
numbers = tuple(elements)
print("Created Tuple:", numbers)
print("Even numbers in the tuple:")
for num in numbers:
if num % 2 == 0:
print(num)
OUTPUT:
Q3)Write a Python program to input two tuples, concatenate them, repeat the first tuple twice,
find the length of each, count the occurrence of a user-given element, and display all elements
of the combined tuple.
INPUT:
print("------Program to concatenate, repeat and Analyze tuples-----")
# Input for first tuple
tuple1_list = [ ]
for i in range(3):
val = input("Enter element for the first tuple: ")
tuple1_list.append(val)
tuple1 = tuple(tuple1_list)
# Input for second tuple
tuple2_list = []
for i in range(3):
val = input("Enter element for the second tuple: ")
tuple2_list.append(val)
tuple2 = tuple(tuple2_list)
print("\nFirst Tuple:", tuple1)
print("\nSecond Tuple:", tuple2)
# Concatenate tuples
together = tuple1 + tuple2
print("\nAfter Concatenation, the new tuple is:", together)
# Repeat first tuple
repeated = tuple1 * 2
print("First tuple repeated twice is:", repeated)
# Lengths
print("\nLength of first tuple:", len(tuple1))
print("Length of second tuple:", len(tuple2))
print("Length of concatenated tuple:", len(together))
# Count occurrence of element
element = input("\nEnter an element to count its occurrence in the combined tuple: ")
count = [Link](element)
print("The element", element, "occurs", count, "times in the combined tuple")
# Display all elements
print("\nDisplaying all elements of the combined tuple:")
for item in together:
print(item)
print("\n--------PROGRAM ENDED------------")
OUTPUT:
.
Q4) Write a Python program to count how many times a specific element occurs in a tuple and find the index
of its first occurrence.
INPUT:
fruits = ("apple", "banana", "mango", "banana", "orange", "banana")
print("Fruits Tuple:", fruits)
item = input("Enter the fruit name to check: ")
count_item = [Link](item)
print("\nThe fruit", item, "occurs", count_item, "time(s) in the tuple.")
if item in fruits:
index_item = [Link](item)
print("The first occurrence of", item, "is at index:", index_item)
else:
print("The fruit", item, "is not present in the tuple.")
OUTPUT:’
DICTIONARY
Q1)Add, Update,delete and find percentage of marks of students from a Dictionary.
INPUT:
print("---- Program to Add, Update, Delete and Find Percentage ----")
students = {
"ANANYA": [80, 78, 85],
"SIMRAN": [90, 88, 92],
"YASHVI": [85, 83, 80]
}
print("Initial Student Records:", students)
students["TASHVI"] = [85, 80, 87]
print("\nAfter adding a new student:", students)
students["ANANYA"] = [95, 90, 92]
print("After updating ANANYA's marks:", students)
del students["SIMRAN"]
print("After deleting SIMRAN's record:", students)
print("\nFinal Student Records:")
for name, marks in [Link]():
print(name, ":", marks)
print("\n---- Percentage of Each Student ----")
for name, marks in [Link]():
total = sum(marks)
percentage = total / len(marks)
print(name, "=> Total:", total, "Percentage:", round(percentage, 2), "%")
print("\n-------- PROGRAM ENDED SUCCESSFULLY --------")
OUTPUT:
Q2) Find Total, Highest, and Lowest Marks from a Dictionary
INPUT:
print("---- Program to Know about Marks Dictionary ----")
marks = {"ashish": 78, "aadi": 89, "ram": 65, "Simran": 92, "abhi": 81}
print("Marks Dictionary:", marks)
total = sum([Link]())
highest = max([Link]())
lowest = min([Link]())
print("\nTotal of all marks:", total)
print("Highest marks:", highest)
print("Lowest marks:", lowest)
OUTPUT:
Q3)Count Frequency of Words in a Sentence:
INPUT:
print("---- Program to Count Word Frequency ----")
sentence = input("Enter a sentence: ")
words = [Link]()
freq = {}
for word in words:
if word in freq:
freq[word] = freq[word] + 1
else:
freq[word] = 1
print("\nWord Frequency Dictionary:")
for key, value in [Link]():
print(key, ":", value)
OUTPUT:
Q4)Write a python program to input names of n countries and their capital and currency store it in
dictionary and display it in tabular format also search and display for a particular country.
INPUT:
n = int(input("Enter number of countries: "))
countries = {}
for i in range(n):
name = input("Enter country name: ")
capital = input("Enter capital: ")
currency = input("Enter currency: ")
countries[name] = [capital, currency]
print("\nCountry\tCapital\tCurrency")
for c in countries:
print(c, "\t", countries[c][0], "\t", countries[c][1])
search = input("\nEnter country to search: ")
if search in countries:
print("\nCountry:", search)
print("Capital:", countries[search][0])
print("Currency:", countries[search][1])
else:
print("Country not found!")
OUTPUT:
MENU DRIVEN CODES
Q1)Write a menu-driven Python program to manage student records using a dictionary —
allowing the user to add, display, search, update, delete records, show only names or marks,
list key–value pairs, find highest and lowest marks, and exit.
INPUT:
students = {} # empty dictionary
while True:
print("\n========== STUDENT DICTIONARY MENU ==========")
print("1. Add a New Student Record")
print("2. Display All Student Records")
print("3. Search for a Student")
print("4. Update Marks of a Student")
print("5. Delete a Student Record")
print("6. Display Only Student Names")
print("7. Display Only Marks")
print("8. Display All Key-Value Pairs (items)")
print("9. Find Student with Highest and Lowest Marks")
print("10. Exit")
print("=============================================")
choice = int(input("Enter your choice (1-10): "))
# 1. Add a new record
if choice == 1:
name = input("Enter student name: ").upper()
marks = int(input("Enter marks of " + name + ": "))
students[name] = marks
print("Record added successfully!")
# 2. Display all records
elif choice == 2:
if len(students) == 0:
print("No records found!")
else:
print("\n--- Student Records ---")
for name, marks in [Link]():
print(name, ":", marks)
# 3. Search for a student
elif choice == 3:
name = input("Enter name to search: ").upper()
if name in students:
print(name, "has scored", students[name], "marks.")
else:
print("Record not found!")
# 4. Update marks
elif choice == 4:
name = input("Enter student name to update marks: ").upper()
if name in students:
new_marks = int(input("Enter new marks of " + name + ": "))
students[name] = new_marks
print("Marks updated successfully!")
else:
print("Student not found!")
# 5. Delete record
elif choice == 5:
name = input("Enter student name to delete: ").upper()
if name in students:
del students[name]
print("Record deleted successfully!")
else:
print("Student not found!")
# 6. Display only keys (names)
elif choice == 6:
print("Student Names:", list([Link]()))
# 7. Display only values (marks)
elif choice == 7:
print("Student Marks:", list([Link]()))
# 8. Display key-value pairs (items)
elif choice == 8:
print("All Key-Value Pairs:", list([Link]()))
# 9. Highest & lowest marks
elif choice == 9:
if len(students) == 0:
print("No records found!")
else:
highest = max([Link]())
lowest = min([Link]())
print("Highest Marks:", highest)
print("Lowest Marks:", lowest)
# 10. Exit
elif choice == 10:
print("Exiting program... Thank you!")
break
# Invalid choice
else:
print("Invalid choice! Please enter between 1 and 10.")
OUTPUT:
.
Q2) Write a menu-driven Python program to perform basic operations on tuples such as
creation, display, searching, counting, concatenation, and finding maximum and minimum
values.
INPUT:
my_tuple = ()
while True:
print("\n========== NUMERIC TUPLE MENU ==========")
print("1. Create a Tuple")
print("2. Display the Tuple")
print("3. Search for an Element")
print("4. Count Occurrence of an Element")
print("5. Concatenate Two Tuples")
print("6. Find Maximum, Minimum, Sum and Average")
print("7. Exit")
print("========================================")
choice = int(input("Enter your choice (1-7): "))
if choice == 1:
n = int(input("Enter number of elements: "))
elements = []
for i in range(n):
val = int(input("Enter number: ")) # numeric input only
[Link](val)
my_tuple = tuple(elements)
print("Tuple created successfully!")
elif choice == 2:
if len(my_tuple) == 0:
print("Tuple is empty!")
else:
print("Current Tuple:", my_tuple)
elif choice == 3:
if len(my_tuple) == 0:
print("Tuple is empty!")
else:
item = int(input("Enter number to search: "))
if item in my_tuple:
print(item, "found at position", my_tuple.index(item))
else:
print(item, "not found in the tuple.")
elif choice == 4:
if len(my_tuple) == 0:
print("Tuple is empty!")
else:
item = int(input("Enter number to count: "))
print("The number", item, "occurs", my_tuple.count(item), "time(s).")
elif choice == 5:
if len(my_tuple) == 0:
print("Create a tuple first!")
else:
n = int(input("Enter number of elements for second tuple: "))
temp_list = []
for i in range(n):
val = int(input("Enter number: "))
temp_list.append(val)
new_tuple = tuple(temp_list)
combined = my_tuple + new_tuple
print("After concatenation:", combined)
elif choice == 6:
if len(my_tuple) == 0:
print("Tuple is empty!")
else:
maximum = max(my_tuple)
minimum = min(my_tuple)
total = sum(my_tuple)
average = total / len(my_tuple)
print("Maximum value:", maximum)
print("Minimum value:", minimum)
print("Sum of all numbers:", total)
print("Average:", round(average, 2))
elif choice == 7:
print("Exiting program... Thank you!")
break
else:
print("Invalid choice! Please enter a number between 1 and 7.")
OUTPUT:
Q3)Write a menu driven code to find the following:
● Area of Circle
● Area of Rectangle
● Area of square
● Exit
INPUT:
while True:
print("Menu Driven Program")
print("[Link] of Circle")
print("[Link] of Rectangle")
print("[Link] of Square")
print("[Link]")
choice=int(input("Enter your choice:"))
if choice==1:
radius=int(input("Enter radius of Circle:"))
print("Area of Circle",3.14*radius*radius)
elif choice==2:
length=int(input("Enter length of Rectangle:"))
breadth=int(input("Enter breadth of Rectangle:"))
print("Area of Rectangle:",length*breadth)
elif choice==3:
side=int(input("Enter side of Square:"))
print("Area:",side*side)
elif choice==4:
Break
OUTPUT