Page 491 - AI Ver 3.0 class 10_Flipbook
P. 491

Python Practical Questions




                   1.  Write a program to add the elements of the two lists.

                  Ans.  [1]:  S1=[32,45,40]
                             S2=[35,30,42,38]
                             print("*** List S1 ***")
                             print(S1,len(S1))
                             print("*** List S2 ***")
                             print(S2,len(S2))
                             All=S1+S2
                             print("*** List All ***")
                             print(All,len(All))

                             *** List S1 ***
                             [32, 45, 40] 3
                             *** List S2 ***
                             [35, 30, 42, 38] 4
                             *** List All ***
                             [32, 45, 40, 35, 30, 42, 38] 7


                   2.   Write a program to calculate mean, median and mode using NumPy of the following list of distance travelled
                      by a car in a week:
                       [95,90,49,71,90,100,55]

                  Ans.  [1]:  import statistics
                             d = [95,90,49,71,90,100,55]
                             m1 = statistics.mean(d)
                             m2 = statistics.median(d)
                             m3 = statistics.mode(d)
                             print("The mean is: ", m1)
                             print("The median is: ", m2)
                             print("The mode is: ", m3)

                             The mean is: 78.57142857142857
                             The median is: 90
                             The mode is: 90

                   3.   Write a Python code to take the input of a number n and then find and display its factorial (n!). For example,
                      5! = 5x4x3x2x1 i.e., 120.

                  Ans.  [1]:  n = int(input("Enter a number: "))
                             factorial = 1
                             if n < 0:
                                 print("Factorial does not exist for negative numbers")
                             elif n == 0:
                                 print("The factorial of 0 is 1")
                             else:
                                 for i in range(1,n + 1):
                                     factorial = factorial*i
                             print("The factorial of ",n," is",factorial)

                             Enter a number: 7
                             The factorial of 7 is 5040

                                                                                    Python Practical Questions  489
   486   487   488   489   490   491   492   493   494   495   496