Page 336 - Computer Science Class 11 With Functions
P. 336

1.  lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
                   For each of the following lists, give a slice that would extract it from the list lst.

                   a.  ['c', 'd', 'e', 'f']
                   b.  ['a', 'b', 'c']
                   c.  ['e', 'f', 'g']

                   d.  ['a','d']
                   e.  ['b','d']
                    f.  Second to last element of the list
                 2.  Suppose we have the following assignment statements:

                   s1 = 'I wish to score highest marks.'
                   s2 = ['I', 'wish', 'to', 'score', 'highest', 'marks', '.']
                   Which of the following statements will execute without any errors?

                   a.  s2[0]= 'You'
                   b.  s1[29]='!'
                   c.  s2[6]='!'



        13.1.4 Membership Operator in

        The membership operator is used to check the membership of an element in a list. It returns True if the specific
        element being looked for is present in the list,  otherwise, False.
        Example:
         >>> colors=['red', 'green', 'blue', 'yellow', 'orange', 'white', 'black']
         >>> 'yellow' in colors
              True
         >>> 'pink' in colors
              False
         >>> 'pink' not in colors
              True
        13.1.5 Linear Search
        Often, we are required to check whether a given value appears in a list. The value to be searched is called the key.
        Given a key to be searched in a list, we successively compare it with the elements at index 0, 1, 2, … until the key is
        ultimately found, or we reach the end of the list. In the former case, we announce that the search was successful and
        report its position (i.e., index) in the list. If the search fails, we report that the key is not present in the list. As the search
        key is sequentially compared with the elements in the list, this method of searching for a key is called sequential search
        or linear search.
        Fig 13.1(a) shows a successful linear search for the key 37 in the  list [14, 6, 1, 8, 14, 50, 61, 89, 37,
        109, 3, 21, 89, 90, 60]. Fig 13.1(b) shows a failed linear search for the key 10 in the same list.
        (a)  Searching for 37














         334   Touchpad Computer Science-XI
   331   332   333   334   335   336   337   338   339   340   341