Array Creation Routines: ======================== 1) zeros() ========== ==> built-in function which we can use to create an array with only zeros. Syntax: numpy.zeros(shape, dtype = float) import numpy a = numpy.zeros(6) print(a) print(a.dtype) b = numpy.zeros(10,dtype = 'i4') print(b) print(b.dtype) c = numpy.zeros(5,dtype = 'c8',order = 'F') print(c) print(c.dtype) d = numpy.zeros((3,4),dtype = 'i4', order = 'C') print(d) e = numpy.zeros((3,4,5),dtype = 'i2') print(e) ======================================== ones() ====== ==> built-in function creating the array with only '0nes' Syntax: numpy.ones(shape, dtype, order) import numpy a = numpy.ones(6) print(a) print(a.dtype) b = numpy.ones(10,dtype = 'c8') print(b) print(b.dtype) c = numpy.ones((3,3),dtype = 'i4') print(c) print(c.dtype) d = numpy.ones([2,3,4],dtype = numpy.int8) print(d) print(d.dtype) =============================================== In Python: range() ==> can generate the range of values ex: range(10) ==> generate values from 0 to 9 range(1,20) ==> generate values from 1 to 19 range(1,100,10) ==> generate values from 1 to 100 with the difference of 10. arange(): ========= ==> built-in function which we can use to create an array with range of values. Syntax: numpy.arange(start, stop, step, dtype) import numpy a = numpy.arange(10) print(a) print(a.dtype) b = numpy.arange(6,dtype = 'f8') print(b) print(b.dtype) c = numpy.arange(1,20) print(c) print(c.dtype) d = numpy.arange(1,100,10) print(d) # e = numpy.arange([(10,),(1,)]) # print(e) ======================================= linspace() ========== ==> built-in function we can use to create an array with range values. Syntax: numpy.linspace(start, stop, num, endpoint = True) import numpy a = numpy.linspace(1,10, num = 6) # endpoint ==> optional # endpoint = True # endpoint = True ==> linspace() can consider the stop value also in array creation print(a) print(a.dtype) b = numpy.linspace(1,10,num = 7, endpoint = False,dtype = 'i4') # endpoint = False ==> no need to consider the stop value in array creation print(b) print(b.dtype) ========================================================= In Python: Random Module random() ==> generate random floating-point values from 0 to 1 dice ==> when we can roll a dice: generate: 1 | 2 | 3 | 4 | 5 | 6 rand(): ====== ==> a built-in function in NumPy with "random" module use to create an array with random float numbers (0 to 1). Syntax: numpy.random.rand(d0, d1, d2, d3, .....) import numpy a = numpy.random.rand() # can generate a value (float) from 0 to 1 print(a) # one dimensional array b = numpy.random.rand(6) print(b) print(b.dtype) # two dimensional array c = numpy.random.rand(3,3) print(c) # four dimensional array d = numpy.random.rand(1,3,4) print(d) ================================================= empty() ======= ==> built-in function can use to create an array with random values of any specified datatype Syntax: numpy.empty(shape, dtype) import numpy a = numpy.empty(10) print(a) b = numpy.empty((3,4)) print(b) c = numpy.empty([3,3,3],dtype = 'i1') print(c) print(c.dtype)