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

●  On execution of lines 28–29, the user enters the elements [14, 6, 1, 8, 14, 50, 61, 89, 37, 109,
                   3, 21, 89, 90, 60] to the list myList and displays the list entered by the user.
                 ● Next, on the execution of line 31, the user is prompted to enter the key to be searched (say, 37).
                 ●  In line 32, the program invokes the function linearSearch (defined in lines 1–18) with the list myList as the first
                   argument and the search key (37) as the second argument.
                 ● In line 13, the function initially sets the variable found as False indicating that the key is not yet found.
                 ●  In line 15, compares the key with the elements at index 0, 1, 2, … until the key is found or we reach the end of the
                   list at line 18.

                 ●  If the search is successful (i.e., the condition in line 15 yields True), the function returns True along with the index
                   of the search key.

                 ● Thus, when we searched for key 37 (Fig. 13.1(a)), the function returned True along with the index 8.
                 ●  Now control is back to the global frame in the script and the program displays the following message at line 34.
                   Element 37 is present in the list at index 8.
                 ●  However, if on examining the entire list, the control comes out of the loop without finding the key, we conclude that
                   the search key does not appear in the list, and execution of line 18 in the function linearSearch returns False along
                   with a None.
                 Thus, when we search for key 10 (Fig. 13.2), the following message is displayed (line 36):

                 10 is not present in the list
                 Fig 13.2: An execution of Program 13.1
                 Sample output:
                  >>> Enter a list:
                      [14, 6, 1, 8, 14, 50, 61, 89, 37, 109, 3, 21, 89, 90, 60]
                      List: [14, 6, 1, 8, 14, 50, 61, 89, 37, 109, 3, 21, 89, 90, 60]
                  >>> Enter the number to be searched:37
                      37 is present in the list at index 8
                      Continue another search? say Y/y for yes, N/n for no:Y
                  >>> Enter the number to be searched:10
                      10 is not present in the list
                      Continue another search? say Y/y for yes, N/n for no:n
                 13.1.6 List Assignment





                 A list assignment does not create a new copy of the list. Instead, on the execution of the assignment statement, the
                 variable on the left-hand side of the assignment statement refers to the list on the right-hand side of the assignment
                 statement; for example,

                  >>> colors
                      ['red', 'green', 'blue']
                  >>> colorsRef = colors
                  >>> id(colors)
                      1777056884928
                  >>> id(colorsRef)
                      1777056884928
                 Note that the variables colors and colorsRef refer to the same list of objects having the id: 1777056884928.
                 Variables that refer to the same object are called aliases of each other. Thus, each of the variables colors and
                 colorsRef is an alias of the other.



                                                                                                     Lists and Tuples  373
   382   383   384   385   386   387   388   389   390   391   392