Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 64 of 102
Marks: +1, -0
(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.
[CBSE 2024 Set 4]
Solution:  
(A)(i)
Students.pop("NISHA")
(or del Students["NISHA"])
(A)(ii)
print(message.count("is"))
For "This is his book", "is" occurs in This, is and his, so the output is 3.
OR (B)
subject = list(subject)     # tuple converted to a list
subject.pop() # last element deleted
(del subject[-1] also deletes the last element.) A tuple is immutable, so it must be converted to a list before an element can be deleted.
© examsnet.com
Advertisements
Go to Question: