Page 230 - Information_Practice_Fliipbook_Class11
P. 230
e. print(len(stateCapital))
f. print("Maharashtra" in stateCapital)
g. print(stateCapital.get("Assam"))
h. del stateCapital["Assam"]
print(stateCapital)
Ans. a. Patna
b. dict_keys(['AndhraPradesh', 'Bihar', 'Maharashtra', 'Rajasthan'])
c. dict_values(['Hyderabad', 'Patna', 'Mumbai', 'Jaipur'])
d. dict_items([('AndhraPradesh', 'Hyderabad'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'),
('Rajasthan', 'Jaipur')])
e. 4
f. True
g. None
h. {'AndhraPradesh': 'Hyderabad', 'Bihar': 'Patna', 'Maharashtra': 'Mumbai'}
Programming Problems
1. Write a program to find the number of times an element occurs in the list.
Ans. myList = [1, 2, 3, 4, 2, 5, 2, 6, 2, 7]
print('List Elements:',myList)
targetElement = int(input("Enter the element to find occurrences: "))
countOccurrence = myList.count(targetElement)
print(f"The element {targetElement} occurs {countOccurrence} times in the list.")
2. Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the
other having all negative numbers from the given list. Print all three lists.
Ans. n = int(input("Enter the number of integers: "))
myList = []
for i in range(n):
num = int(input(f"Enter integer {i + 1}: "))
myList.append(num)
positiveNumbers = []
negativeNumbers = []
for num in myList:
if num > 0:
positiveNumbers.append(num)
elif num < 0:
negativeNumbers.append(num)
else:
continue
print("\nOriginal List:", myList)
print("Positive Numbers:", positiveNumbers)
print("Negative Numbers:", negativeNumbers)
3. Write a program to find the largest and the second largest elements in a given list of elements.
Ans. myList = [4, 7, 2, 9, 5, 1]
print('List content', myList)
if len(myList) < 2:
secondLargestElement = None # Return None if the list has fewer than two elements
216 Touchpad Informatics Practices-XI

