Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 155 - 2615_KEC
P. 155

    To create a two-dimensional (2-D) arrays by passing nested lists to the array() function:
                         Program.py

                      File  Edit  Format   Run    Options   Window    Help

                      import numpy as np
                      a = np.array([[15,'Pranay',96],[16,'Pranya',92]])
                      print(a)

                         Output

                      [['15' 'Pranay' '96']
                      ['16' 'Pranya' '92']]

                     USING MATHEMATICAL OPERATORS ON INTEGER ARRAY
                     Following are the mathematical operators on integer array:
                       The ** (Exponential) operator: This operator is used to raise array elements to a given power.

                         Program.py
                      File  Edit  Format   Run    Options   Window    Help

                      import numpy as np
                      marks1 = np.array([1,2,3,4])
                      marks2=np.array([2,1,2,1])
                      print(marks1**marks2)
                      print("marks1 to the power of 3 is:", marks1**3)

                         Output

                      [1 2 9 4]
                      marks1 to the power of 3 is: [ 1 8 27 64]

                       The + (Addition) operator: This operator is used to add values of array elements or to add a
                       constant to each element.

                         Program.py
                      File  Edit  Format   Run    Options   Window    Help

                      import numpy as np
                      marks1 = np.array([7,8,5,4])
                      marks2=np.array([5,6,4,7])
                      print(marks1+marks2)
                      print("2 marks extra:",marks1+2)

                                                                                   NumPy-Creation, Selection & Operation  153

| 150 151 152 153 154 155 156 157 158 159 160 |