Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 153 - 2615_KEC
P. 153

    Feature                      NumPy Array                                Python List
                      Syntax            import numpy                                  marks=[34,23,41,42]

                                        marks=numpy.array([34,23,41,42])

                     CREATING AN ARRAY USING NUMPY

                     Different ways to create an array using NumPy are as follows:
                        np.array() – Creates a NumPy array from a list or tuple.
                       For example, to create dimensional array:

                         Program.py

                      File  Edit  Format   Run    Options  Window     Help

                      import numpy                                                     Output
                      rollno = numpy.array([1, 2, 3])
                      print(rollno)                                                 [1 2 3]

                        np.arange() – Generates evenly spaced numbers within a given range.

                        For example, to create a sequential one-dimensional array with values starting from 5, ending
                       before 51, with a step of 10.

                         Program.py
                      File  Edit  Format   Run    Options   Window    Help

                      import numpy as np                                               Output
                      a = np.arange(5,51,10)
                      print(a)                                                      [ 5 15 25 35 45]

                        np.random()- Generates random numbers.

                       For example, to create one-dimensional array with 4 random values:
                         Program.py
                      File  Edit  Format   Run    Options   Window    Help

                      import numpy as np
                      a = np.random.random(4)
                      print(a)

                         Output

                      [0.4141628 0.1035279 0.05137008 0.98002355]

                                                                                   NumPy-Creation, Selection & Operation  151

| 148 149 150 151 152 153 154 155 156 157 158 |