Question 54 of 97
Q.(A) Write the Python statement for each of the following tasks using built-in functions/methods only:
(i) To remove the item whose key is "NISHA" from a dictionary named Students. For example, if the dictionary Students contains {"ANITA":90, "NISHA":76, "ASHA":92}, then after removal the dictionary should contain {"ANITA":90, "ASHA":92}
(ii) To display the number of occurrences of the substring "is" in a string named message. For example if the string message contains "This is his book", then the output will be 3.
(OR)
(B) A tuple named subject stores the names of different subjects. Write the Python commands to convert the given tuple to a list and thereafter delete the last element of the list.
Odisha ChseCBSE Class XII Board 2024Subjective· 2mImportance★★★★★
56% · 54/97 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Part (a) uses the dictionary pop/del to remove a key and str.count() to count a substring (3 here); Part (b) converts a tuple to a list with list() and deletes the last element with del L[-1]/L.pop().
Part (a)
(i) Remove the item whose key is "NISHA". A dictionary item is deleted by key with pop() (also returns the value) or the del statement.
Students.pop("NISHA")
# or
del Students["NISHA"]
Starting from {"ANITA":90, "NISHA":76, "ASHA":92} the result is {"ANITA":90, "ASHA":92}.
(ii) Count occurrences of the substring "is". The string method count() returns the number of non-overlapping occurrences.
print(message.count("is"))
``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.