Page 354 - Information_Practice_Fliipbook_Class11
P. 354

item = int(input("Enter number : "))

                 myList.append(item)

             print("The list formed is : ", myList)


             for x in range(0,len(myList)-1,2):
                 myList[x], myList[x+1] = myList[x+1],myList[x]

              print("The list after swapping odd indices with those at even indices is: ",
             myList)
         Program 26: Write a program that takes a list myList and a value key to be searched in myList as inputs. When
         the key is present in myList, the program prints the index where the search key is found. If the program reaches
         the end of the list without finding the key, it prints an appropriate message indicating absence of the key in the list.


        Ans. '''
             Objective: To search for a key in a list

             Input: list and key to be search
             Output: message indicating the presence or absence of key in list

             '''


             myList = eval(input('Enter a list:\n'))
             print('List:', myList)

             while True:
                 key = int(input('Enter the number to be searched:'))

                 found = False

                 for index in range(len(myList)):
                     if myList[index] == key:

                         found = True
                         break

                 if found:
                     print(key,' is present in the list at index', index)

                 else:

                     print(key,' is not present in the list')
                  searchAgain = input('Continue another search? say Y/y for yes, N/n for
                  no:')

                 if searchAgain != 'Y' and searchAgain != 'y':
                     break





          340  Touchpad Informatics Practices-XI
   349   350   351   352   353   354   355   356   357   358   359