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

4.  What will be the output of the following code segment:
                      a.  myList = [1,2,3,4,5,6,7,8,9,10]
                        del myList[3:]
                        print(myList)
                      b.  myList = [1,2,3,4,5,6,7,8,9,10]
                        del myList[:5]
                        print(myList)
                      c.  myList = [1,2,3,4,5,6,7,8,9,10]
                        del myList[::2]
                        print(myList)
                  Ans.  a.  [1, 2, 3]
                      b.  [6, 7, 8, 9, 10]
                      c.  [2, 4, 6, 8, 10]
                    5.  Differentiate between append() and extend() functions of list.
                  Ans.  Refer to 13.6 List Methods
                    6.  Consider a list:
                      list1 = [6,7,8,9]
                      What is the difference between the following operations on list1:
                      a.  list1 * 2
                      b.  list1 *= 2
                      c.  list1 = list1 * 2
                  Ans.   a.  list1 * 2: This operation creates a new list by repeating the elements of list1 twice. The original list remains unchanged.
                      b.  list1  *=   2:   This  operation  modifies  the original  list by repeating  its elements twice in  place.  New list1 is
                        [6, 7, 8, 9, 6, 7, 8, 9]
                      c.  This operation creates a new list by repeating the elements of list1 twice and then assigns this new list to list1, replacing the original
                        list. Output is: [6, 7, 8, 9, 6, 7, 8, 9]
                    7.  The record of a student (Name, Roll No., Marks in five subjects and percentage of marks) is stored in the following list:
                      stRecord = ['Raman','A-36',[56,98,99,72,69], 78.8]
                      Write Python statements to retrieve the following information from the list stRecord.
                      a.  Percentage of the student
                      b.  Marks in the fifth subject
                      c.  Maximum marks of the student
                      d.  Roll no. of the student
                      e.  Change the name of the student from 'Raman' to 'Raghav'
                  Ans.   a.  print("Percentage of the student : ", stRecord[3])
                      b.  print("Marks in the fifth subject : ", stRecord[2][4])
                      c.  print("Maximum marks of the student :", max(stRecord[2]))
                      d.  print("Roll no. of the student :", stRecord[1])
                      e.  stRecord[0] = "Raghav"

                                                         PROGRAMMING PROBLEMS
                    1.  Write a program to find the number of times an element occurs in the list.
                  Ans.  def countOccurrences(myList, targetElement):
                          count = myList.count(targetElement)
                          return count

                      def main():
                          myList = [1, 2, 3, 4, 2, 5, 2, 6, 2, 7]



                                                                                                        Dictionaries  431
   440   441   442   443   444   445   446   447   448   449   450