Array Creation from existing Data: ================================== 1) asarray(): ============= ==> built-in function which used to convert any python object like: list, tuple, array etc. to NumPy array. Syntax: numpy.asarray(object, dtype) # WAP TO CREATE AN ARRAY FROM THE LIST OF PYTHON import numpy ld = [1,3,5,7,9,11,13,15] a_list = numpy.asarray(ld) print("The Array = ",a_list) print("The Type = ",a_list.dtype) b_list = numpy.asarray(a_list,dtype = 'c8') print("The Array = ",b_list) print("The Type = ",b_list.dtype) ===================================================== # ARRAY CREATION WITH TUPLE OF HETEROGENEOUS ELEMENTS import numpy td = eval(input("Enter the tuple with heterogeneous elements:")) a_tuple = numpy.asarray(td) print("The Array = ",a_tuple) print("The Array Type = ",a_tuple.dtype) ========================================================== 2) frombuffer(): =============== ==> built-in function which can be used to create an array from bytes object or bytes array. ==> bytes object/bytes array ==> the object with values of: 0 to 255 Syntax: numpy.frombuffer(buffer, dtype) # Creation of an array from Bytes Object import numpy bobj1 = bytes([1,3,5,7,9]) # bobj2 = bytes('NumPy') bobj2 = b'NumPy' print(type(bobj1)) print(type(bobj2)) print("The Given Bytes Object are:") print(bobj1) print(bobj2) # from bytes object, array creation: a1 = numpy.frombuffer(bobj1,dtype = "S1") print("The Array1 = ",a1) print("The Type = ",a1.dtype) a2 = numpy.frombuffer(bobj2,dtype = 'S1') print("The Array2 = ",a2) print("The Type = ",a2.dtype) =============================================== 3) fromiter(): ============== ==> built-in function can use to create a one-dimensional array from iterable object. Syntax: numpy.fromiter(iterable_object, dtype) # WAP to create an array from an Iterable Object import numpy def my_function(n):#6 for i in range(n):# 0 to 5 yield i # creating the array from iterable object a = numpy.fromiter(my_function(6),dtype = 'f2') print("The Array from an iterable object = ",a) ================================================================ Creation of array from List data: ================================= import numpy ld = eval(input("Enter a list data:")) # array from list a_list = numpy.array(ld) print("The Array from list = ",a_list) nld = [[1,2,3],[4,5,6],[7,8,9]] # nested list data b_list = numpy.array(nld) print("The Array from the nested list = ",b_list) # if the nested list with heterogeneous elements nld1 = [[1,True,1.2],[22,False,0.001],[33,True,1.112]] c_list = numpy.array(nld1) print("The Array from the given nested list = ",c_list) ======================================================== Creation of array from Tuples: =============================== import numpy td = (1,2,3,4,5,6,7,8,9,10) sd = {1,3,5,7,9,1,3,5,7,9,1,2,3,4,5} # create the array from the tuple a1 = numpy.array(td) a2 = numpy.array(sd) print("The Array from the tuple = ",a1) print("The Array from the set = ",a2) ================================================== copy(): ====== built-in function can use to create a new array from another array Syntax: numpy.copy(array_name) import numpy source_array = numpy.array([1,10,100,1000,100,10,1]) copy_array = numpy.copy(source_array) print("The Original Array = ",source_array) print("The Copied Array = ",copy_array)