Test Index

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

© examsnet.com
Question : 73 of 102
Marks: +1, -0
(a) Write the definition of a method/function SearchOut(Teachers, TName) to search for TName from a list Teachers, and display the position of its presence.
For example :
If the Teachers contain ["Ankit", "Siddharth", "Rahul", "Sangeeta", "rahul"]
and TName contains "Rahul"
The function should display
Rahul at 2
rahul at 4
OR
(b) Write the definition of a method/function Copy_Prime(lst) to copy all the prime numbers from the list lst to another list lst_prime.
[CBSE 2024 Compartment]
Solution:  
(a)
def SearchOut(Teachers, TName):
for i in range(len(Teachers)):
if Teachers[i].lower() == TName.lower():
print(Teachers[i], "at", i)
Explanation: The loop visits every index of the list. Both names are converted to lowercase before comparing, so "Rahul" and "rahul" both match, as the example shows. Each match is printed with its index: Rahul at 2 and rahul at 4.
OR (b)
def Copy_Prime(lst):
lst_prime = []
for n in lst:
if n > 1:
for d in range(2, int(n**0.5) + 1):
if n % d == 0:
break
else:
lst_prime.append(n)
return lst_prime
Explanation: A number greater than 1 is prime if no number from 2 to its square root divides it. The for...else runs the else part only when the loop ends without break, that is when no divisor was found; then the number is appended to lst_prime.
Example: Copy_Prime([4, 7, 1, 11, 15, 2]) returns [7, 11, 2].
© examsnet.com
Advertisements
Go to Question: