Page 16 - IPP-11
P. 16

(f)  Write a Python script to read an integer > 1000 and reverse the number.               (3)

                Ans.  n=int(input("Enter number: "))
                     rev=0
                     while(n>0):
                        dig=n%10
                        rev=rev*10+dig
                        n=n//10
                     print("Reverse of the number:",rev)
               QUESTION 6

                 (a)  What are list slices? What can we use them for?                                       (1)
                Ans.  Slice is a part of a list containing some contiguous elements from the list or sub-part of a list extracted out.
                      It is used to extract some contiguous parts or elements of list (sub-part) from main list.
                 (b)  What will the following code result in–                                               (1)

                    L1=[1,3,5,7,9]
                     print(L1==L1.reverse())
                     print(L1)
                Ans.  Output:
                    False
                     [9, 7, 5, 3, 1]
                 (c)  How is clear() function different from del<dict> statement?                           (1)
                Ans.  The clear() function removes all items from the dictionary and the dictionary becomes empty while
                    del<dict> statement deletes a dictionary element or dictionary entry, i.e., a key:value pair.
                 (d)  Predict the output of the following code—                                             (2)

                    d1={5:"number","a":"string",(1,2):"tuple"}
                     print("Dictionary contents")
                     for x in d1.keys():
                        print(x,':',d1[x],end=' ')
                        print(d1[x]*3)
                        print()
                Ans.  Output:

                    Dictionary contents
                     5 : number numbernumbernumber

                     a : string stringstringstring

                     (1, 2) : tuple tupletupletuple
                 (e)  Write a Python script to search an element in a given list of numbers.                (2)

                Ans.  lst=eval(input("Enter list:"))
                     length=len(lst)
                     element=int(input("Enter element to be searched for :"))
                     for i in range(0,length-1):
                        if element==lst[i]:
                          print(element,"found at index", i)
                          break
                     else:
                         print(element,"not found in given list")




                                                               6
   11   12   13   14   15   16   17   18   19   20   21