Page 232 - Information_Practice_Fliipbook_Class11
P. 232
inputList = []
for i in range(n):
element = int(input(f"Enter element {i + 1}: "))
inputList.append(element)
print("\nOriginal List:", inputList)
elementToInsert = int(input("Enter the element to insert: "))
positionToInsert = int(input("Enter the position to insert: "))
inputList.insert(positionToInsert, elementToInsert)
print("\nList after insertion:", inputList)
7. Write a program to read elements of a list and do the following.
a. The program should ask for the position of the element to be deleted from the list and delete the element at the desired position in
the list.
b. The program should ask for the value of the element to be deleted from the list and delete this value from the list.
Ans. myList = []
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
element = int(input(f"Enter element {i + 1}: "))
myList.append(element)
print("\nOriginal List:", myList)
choice = input("Do you want to delete by position (P) or by value (V)? ").upper()
if choice == "P":
position = int(input("Enter the position of the element to delete: "))
if 1 <= position <= len(myList):
del myList[position - 1]
print(f"Element at position {position} deleted. Updated list:", myList)
else:
print("Invalid position. Please enter a valid position.")
elif choice == "V":
value = int(input("Enter the value of the element to delete: "))
if value in myList:
myList.remove(value)
print(f"Element {value} deleted. Updated list:", myList)
else:
print(f"Element {value} not found in the list.")
else:
print("Invalid choice. Please enter 'P' or 'V'.")
8. Write a Python program to find the highest 2 values in a dictionary.
Ans. studentScores = {
'Alice': 85,
'Bob': 92,
'Charlie': 78,
'David': 95,
'Emma': 88
}
218 Touchpad Informatics Practices-XI

