Test Index

CBSE Class 12 Computer Science 2026 Paper Compartment Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 24 of 37
Marks: +1, -0
(I) Consider the following tuple named PRICE :
PRICE = (100, 55, 72, 65, 85, 90)
(a) Write a single line statement in Python that displays the contents of the tuple PRICE in ascending order of the values.
OR
(b) Write a single line statement in Python to find and display the average PRICE of all the values in the given tuple.
(II) (a) Write a single line Python statement using built-in functions/methods to display the content of a string S such that every occurrence of the alphabet 'i' of the string S is replaced with an underscore, i.e '_'.
For example, if the string S is :
S = "time is precious"
then the statement should display the output as :
t_me _s prec_ous
OR
(b) Write a Python statement using built-in function/method to delete the first occurrence of element 25 from the list named myList.
Solution:  
(I)(a)
sorted() works on any iterable (a tuple has no sort() method because it is immutable) and returns a new sorted list:
print(sorted(PRICE))
Output:
[55, 65, 72, 85, 90, 100]
OR (I)(b)
Average = sum of the values / number of values:
print(sum(PRICE) / len(PRICE))
sum = 100 + 55 + 72 + 65 + 85 + 90 = 467 and len = 6, so the output is:
77.83333333333333
(II)(a)
The string method replace(old, new) returns a copy of the string with every occurrence of old replaced by new:
print(S.replace('i', '_'))
Output for S = "time is precious":
t_me _s prec_ous
OR (II)(b)
The list method remove(value) deletes the first occurrence of the given value:
myList.remove(25)
For example, if myList = [10, 25, 30, 25], after this statement myList becomes [10, 30, 25] (only the first 25 is removed).
© examsnet.com
Advertisements
Go to Question: