Test Index

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

© examsnet.com
Question : 75 of 102
Marks: +1, -0
(a) Write the Python statement for each of the following tasks using BUILT-IN functions/methods only : (1+1=2)
(i) To delete an element 10 from the list lst.
(ii) To replace the string "This" with "That" in the string str1.
OR
(b) A dictionary dict2 is copied into the dictionary dict1 such that the common key’s value gets updated. Write the Python commands to do the task and after that empty the dictionary dict1.
[CBSE 2024 Compartment]
Solution:  
(a)(i)
lst.remove(10)
remove() deletes the first occurrence of the value 10 from the list.
(a)(ii)
str1 = str1.replace("This", "That")
replace() returns a new string with every "This" replaced by "That"; strings are immutable, so the result is assigned back to str1.
OR (b)
dict1.update(dict2)
dict1.clear()
update() copies all key:value pairs of dict2 into dict1; for a key present in both, the value in dict1 is replaced by the value from dict2.
clear() then removes all items, leaving dict1 as an empty dictionary {}.
© examsnet.com
Advertisements
Go to Question: