Page 511 - Computer Science V2.0 Class 11
P. 511

num = int(input("Enter the number of elements that you want of add to the list
                     : "))
                     for x in range(num):
                         num1 = int(input("Enter number : "))
                         myList.append(num1)

                     print("The list formed is : ", myList)
                     resList = swappOddEvenIndex(myList)
                       print("The list after swapping odd indices with those at even indices is: ",
                     resList)
                 Program 15: Write a function search that takes a list myList and a value key to be searched in myList as
                 arguments. When the key is present in myList, the function search returns True and the index where the
                 search key is found. If search reaches the end of the list without finding the key, search returns False with
                 None as the index. Invoke search to search for a key in a list provided by the user.


                 Ans. def search(myList, key):
                         '''
                         Objective: To search the key in myList
                         Input Parameters:

                             myList: list
                             key   : element to be searched
                         Return Value:
                             True: if key is found in the list, False otherwise

                             index:
                                 if search succeeds, index of search key found
                                 None, if search fails
                         '''
                         found = False
                         for index in range(len(myList)):

                             if myList[index] == key:
                                 found = True
                                 return found, index
                         return found, None



                     myList = []
                     numElements = int(input("Enter size of the list: "))
                     print("Enter each element and press enter: ")
                     for i in range(0, numElements):   #list input element-wise
                         num = int(input())

                         myList.append(num)
                     print("Contents of list:", myList)
                     while True:
                         key = int(input("Enter the number to be searched:"))

                                                                                                           Practical  497
   506   507   508   509   510   511   512   513   514   515   516