Page 289 - Ai_C10_Flipbook
P. 289

NumPy Arrays vs Python Lists

                 The difference between NumPy Arrays vs Python Lists is shown in the below table:

                  Feature              NumPy Array                              Python List
                  Data Type            Homogeneous (stores elements of the  Heterogeneous (can store elements of
                                       same type)                               different types)
                  Data Type            Does not support mixed types within the  Supports mixed types and allows implicit
                  Conversion           same array                               conversion
                  Memory Efficiency    More memory-efficient                    Occupies more memory
                  Accessibility        Requires importing NumPy                 Built-in  Python  feature  (does  not  require
                                                                                an additional package)
                  Mathematical         Supports  direct  mathematical  operations  Requires looping or applying operations to
                  Operations           on all elements                          individual elements
                  Usage                Mostly  used  for  scientific  computing,  Mainly used for data storage and
                                       numerical operations, and data analysis  management
                  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:

                    • Creating one dimensional array:
                    [1]:  import numpy
                          rollno = numpy.array([1, 2, 3])
                          print(rollno)

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

                    [1]:  import numpy as np
                          a = np.arange(10,101,10)
                          print(a)
                          [ 10  20  30  40  50  60  70  80  90 100]

                    • Creating one-dimensional array with 4 random values:

                    [1]:  import numpy as np
                          a = np.random.random(4)
                          print(a)

                          [0.4141628  0.1035279  0.05137008 0.98002355]

                    • Creating two-dimensional array of 3 rows and 4 columns with random integer values less than 10:

                    [1]:  import numpy as np
                          a = np.random.randint(10, size=(3,4))
                          print(a)
                          [[3 1 4 1]
                          [7 6 0 1]
                          [4 2 2 9]]

                                                                                    Advance Python (Practical)  287
   284   285   286   287   288   289   290   291   292   293   294