Page 187 - Robotics and AI class 10
P. 187

So if we compare NumPy-Arrays and Python-List then:
               • Array is a collection of homogeneous values whereas List as studied in class 9 - is a collection of heterogeneous
              values.

               • In Arrays data of one type does not support data of another type whereas in List it works perfectly by using data
              of one type by converting into another datatype.
               • Arrays can be accessed only through package -NumPy and occupies less memory space whereas List occupies
              more memory space and can be accessed directly in Python without any package support.
               • In arrays the mathematical operators can be directly used whereas in list the mathematical operators cannot be
              used directly on it instead need to be used separately on individual elements.

               • Arrays are mainly used for mathematical operations where Lists are mainly used for data management.

               • Syntax of creating array is: import numpy
                                  marks=numpy.array([34,23,41,42])
              Syntax of creating a list is:   marks=[34,23,41,42]
            Creating an Array using NumPy:
               • Creating one dimensional array:

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

                  [1 2 3]
               • Create a sequential 1 D array with values as multiples of 10 from 10 to 100:

                  import numpy as np

                  a = np.arange(10,101,10)
                  print(a)
              Output:

                   [ 10  20  30  40  50  60  70  80  90 100]
              Another example :

                  >>>import numpy as np
                  >>> student = np.array([11,'Amita',92.5])
                  >>> student
               • Creating 1 D array with 4 random values:

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

                  [0.9290503  0.89991053 0.7563874  0.89570953]
               • Creating a 3X4 array with random integer values less than 10:
                  import numpy as np
                  a = np.random.randint(10, size=(3,4))
                  print(a)

                                                         Introduction to Data and Programming with Python  185
   182   183   184   185   186   187   188   189   190   191   192