Creation of Array from Existing Data: ===================================== From List: ========== # WAP TO CREATE THE ARRAY FROM LIST DATA import numpy ld1 = [1,2,3,4,5,6,7,8,9,10] print(type(ld1)) # array from list a1 = numpy.array(ld1) print("The Array = ",a1) ld2 = [[3,2,1],[6,5,4],[9,8,7]] # Nested list print(type(ld2)) a2 = numpy.array(ld2) print("The Array = ",a2) ld3 = [12,True,1.23,12-23j,'python'] # list with heterogeneous elements a3 = numpy.array(ld3) print("The Array = ",a3) ld4 = [[True, 10, 1.12],[1-2j,12,1.23],['a',112,False]] # nested list with heterogeneous elements a4 = numpy.array(ld4) print("The Array = ",a4) ===================================================== from Tuple: =========== import numpy td = (1,11,111,1111,11111,1111,111,11,1) sd = {1,3,5,7,9,9,7,5,3,1,1,2,3,4,5} dd = {'a':111,'b':222,'c':333,'d':444,'e':555} print(type(td)) print(type(sd)) print(type(dd)) # array from the tuple a1 = numpy.array(td) a2 = numpy.array(sd) a3 = numpy.array(dd) print("The Array = ",a1) print("The Array = ",a2) print("The Array = ",a3) ===================================================== asarray(): ========== use to convert various python objects like: list, tuple, array etc. into array. Syntax: numpy.asarray(object, dtype) import numpy ld = [1,11,111,1111,11111,1111,111,11,1] ld1 = [111,True,1.12,False,12-23j,'python'] a1 = numpy.asarray(ld) a2 = numpy.asarray(ld1) print("The Array = ",a1) print("The Array = ",a2) print("The Type = ",a2.dtype) print("The Type = ",a1.dtype) ==================================== frombuffer(): ============= ==> a built-in function can use to create an array from bytes object or bytes array. bytes object ==> bytes() ==> bytes object Syntax: numpy.frombuffer(buffer_object, dtype) import numpy b1 = bytes([1,2,3,4,5]) # bytes object # b2 = bytes("Python") b2 = b'Python' a1 = numpy.frombuffer(b1,dtype = 'S1') a2 = numpy.frombuffer(b2,dtype = "S1") print("a1 = ",a1) print("a2 = ",a2) ============================================== fromiter(): =========== ==> built-in function can use to create an array from iterable object. Syntax: numpy.fromiter(iterable-object, dtype) import numpy def my_generator(n): for i in range(n): yield i a = numpy.fromiter(my_generator(7),dtype = 'i1') print("The Array = ",a) print("The Type = ",a.dtype) ================================================ copy(): ====== ==> built-in function can use to create a new array from another array. Syntax: numpy.copy(s-array) import numpy a = numpy.array([1,2,3,4,5,6,7,8,9,10]) a_copy = numpy.copy(a) print("The Arrays are = ") print(a) print(id(a)) print(a_copy) print(id(a_copy))