Page 311 - Ai_C10_Flipbook
P. 311

15.  Write a program to input a number and display whether it is a prime number or not.

                  Ans.  [1]:  num = int(input("Enter a number: "))
                             if num > 1:
                                 for i in range(2, int(num / 2) + 1):
                                     if (num % i) == 0:
                                         print(num, 'is not prime')
                                         break
                                 else:
                                     print(num, "is a prime number")
                             else:
                                 print(num, "is not a prime number")
                             Enter a number: 12
                             12 is not a prime number

                   16.  Write a program to input two numbers and swap both the numbers without using the third number.

                  Ans.  [1]:  x = int(input("Enter the value of x: "))
                             y = int(input("Enter the value of y: "))
                             print("Numbers before swapping: %d   %d\n" %(x,y))
                             x = x + y
                             y = x - y
                             x = x - y
                             print("Numbers after swapping: %d   %d\n"%(x,y))
                             Enter the value of x: 22
                             Enter the value of y: 14
                             Numbers before swapping: 22 14
                             Numbers after swapping: 14 22

                   17.  Using Numpy Package:
                        • Create a 4×2 array with random integer.
                        • Create 3×3 array with all zeros.

                  Ans.  [1]:  import numpy as np
                             array1 = np.random.randint(0, 10, (4, 2))
                             print("4x2 Array with Random Integers:")
                             print(array1)
                             import numpy as np
                             array2 = np.zeros((3, 3))
                             print("\n3x3 Array with Zeros:")
                             print(array2)
                             4x2 Array with Random Integers:
                             [[5 1]
                              [7 8]
                              [0 1]
                              [1 5]]
                             3x3 Array with Zeros:
                             [[0. 0. 0.]
                              [0. 0. 0.]
                              [0. 0. 0.]]

                   18.  Using Matplotlib and the given data, plot a bar chart:
                       No of people voted = [23,45,31,40,35]

                       Area covered = [‘a1’,’a2’,’a3’,’a4’,’a5’]



                                                                                    Python Practical Questions  309
   306   307   308   309   310   311   312   313   314   315   316