Page 63 - PYTHON-11
P. 63

Section C (3 × 3 = 9 Marks)
              29.  (a)  Write a Python program to read a list of elements and modify it by deleting duplicate elements.
                     Note: Only the first occurrence should be displayed in the final list.                (3)
                                                           OR
                  (b)  Write a Python program to enter the names of teachers and their salaries as input and store them in
                     a dictionary.
             Ans.  (a)  list=eval(input("enter a list"))
                     for j in range(len(list)):
                         for k in list:
                          if list.count(k) > 1:
                             list.remove(k)
                        print(list)
                                                           OR
                  (b)  n=int(input("Enter the number of teachers whose details you want to store"))
                     count=1
                     teacher = dict()
                     while count <= n:
                         tname=input("Enter the name of teacher")
                         salary=int(input("enter the salary"))
                        teacher[tname]= salary
                         count = count + 1
                     print("\n\n TeacherName\t Salary")
                     for i in teacher:
                        print(i,'\t\t', teacher[i])
              30.  (a)  Explain syntax error and runtime errors in Python.                                 (3)
                                                           OR
                  (b)  Write different ways of creating tuples with one item and print the same.
             Ans.  (a)  Syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be
                     written in a particular programming language. These types of errors are generated when we violate
                     the syntax or the grammatical rules of a programming language. They are the most common type
                     of errors which are easily traceable. For example, print "Hello" will result in syntax error as it
                     should be print("Hello").
                      Runtime error occurs after the Python interpreter interprets the source code and the computer begins
                     to execute it. It usually results in abnormal program termination during execution. For example,
                     division by zero, performing an operation on incompatible types, etc.
                                                           OR
                  (b)  Different ways of creating tuples with one item are as follows:
                      (i)  t=7,
                         print(t)
                     (ii)  t=(7,)

                         print(t)
                     (iii)  t=tuple((7,))
                         print(t)
              31.  Predict the output of the following code.                                               (3)
                 keys = ['a', 'b', 'c']
                 values = 0
                 my_dict = dict.fromkeys(keys, values)
                 my_dict['a'] = 35
                 print(my_dict)
                                                           OR
                 items = ["apple", "banana", "cherry", "dragonfruit"]
                 for i in range(len(items) -1, -1, -1):
                    print(items[i], end="")
             Ans.  {'a': 35, 'b': 0, 'c': 0}
                      OR
                 dragonfruitcherrybananaapple


                                                              M.12
   58   59   60   61   62   63   64   65   66   67   68