Page 424 - Ai_417_V3.0_C9_Flipbook
P. 424

9.  How can we modify elements in a list?
                Ans.  List is mutable so data can be easily modified by overwriting a new value to an existing value in a given list by using an
                    assignment operator (=).
                     Syntax:

                     list[index] = newvalue
                     For example,
                    l1 = [10, 20, 40, 50]
                     l1[3] = 100
                     print(l1)
                 10.  Find the output of the following codes:


                      a. l1=[10,20,40,50]                            [10, 'abc', 'xyz', 50]
                         l1[1:3]=["abc","xyz"]
                         print(l1)


                       b. names=["yash","amit","rhea","mayank"]      ['amit', 'mayank', 'rhea', 'yash']
                          names.sort()
                          print(names)

                      c. A=[10,20,34,56,"abc","xyz",89.5,67]         [10, 20, 34, 56, 'xyz', 89.5]
                          A.remove("abc")
                          A.pop()
                          print(A)

                      d. l1=[1,2,3,4,5,6]                            [2, 3, 4, 5, 6, 1]
                          print(l1[1:]+l1[:1])


                      e. x=2                                         [1, 1][2, 2][3, 3][4, 4]
                         for i in range(1,5):
                           l=[i]*x
                           print(l,end='')

                 11.  Write an algorithm to check if a number is an Armstrong number.
                Ans.  Find out whether the number is Armstrong or not.
                     Step 1   Start

                     Step 2   input num
                     Step 3   sum=0, n=num
                     Step 4   If n>0 goto Step 4.1 else Goto Step 7
                     Step 4.1       d=n%10
                     Step 4.2       sum=sum+(d*d*d)
                     Step 4.3       n=n//10

                     Step 5   if sum==num  then
                                 Print "Armstrong Number"
                     Step 6     else


                    422     Touchpad Artificial Intelligence (Ver. 3.0)-IX
   419   420   421   422   423   424   425   426   427   428   429