Array creation using array functions

array(data type, value list) function is used to create an array with data type and value list specified in its arguments.
np.zeros

Example : Python



# Python code to demonstrate the working of
# array()

# importing "array" for array operations
import array

# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3])

# printing original array
print ("The new created array is : ",end="")
for i in range (0,3):
print (arr[i], end=" ")

print ("\r")

Output:

The new created array is : 1 2 3 1 5

Numpy | Array Creation

In this article, we are going to explore numpy array creation technique.

Table of Content

  • Array creation using List :
  • Array creation using array functions
  • Methods for array creation in Numpy

Similar Reads

Array creation using List :

Arrays are used to store multiple values in one single variable.Python does not have built-in support for Arrays, but Python lists can be used instead....

Array creation using array functions

array(data type, value list) function is used to create an array with data type and value list specified in its arguments.np.zeros...

Array creation using numpy methods

NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. For example: np.zeros,np.empty etc....

Reshaping array

We can use reshape method to reshape an array. Consider an array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another array with shape (b1, b2, b3, …, bM).The only required condition is: a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM . (i.e original size of array remains unchanged.)...

Methods for array creation in Numpy

FunctionDescriptionempty()Return a new array of given shape and type, without initializing entriesempty_like()Return a new array with the same shape and type as a given arrayeye()Return a 2-D array with ones on the diagonal and zeros elsewhere.identity()Return the identity arrayones()Return a new array of given shape and type, filled with onesones_like()Return an array of ones with the same shape and type as a given arrayzeros()Return a new array of given shape and type, filled with zeroszeros_like()Return an array of zeros with the same shape and type as a given arrayfull_like()Return a full array with the same shape and type as a given array.array()Create an arrayasarray()Convert the input to an arrayasanyarray()Convert the input to an ndarray, but pass ndarray subclasses throughascontiguousarray()Return a contiguous array in memory (C order)asmatrix()Interpret the input as a matrixcopy()Return an array copy of the given objectfrombuffer()Interpret a buffer as a 1-dimensional arrayfromfile()Construct an array from data in a text or binary filefromfunction()Construct an array by executing a function over each coordinatefromiter()Create a new 1-dimensional array from an iterable objectfromstring()A new 1-D array initialized from text data in a stringloadtxt()Load data from a text filearange()Return evenly spaced values within a given intervallinspace()Return evenly spaced numbers over a specified intervallogspace()Return numbers spaced evenly on a log scalegeomspace()Return numbers spaced evenly on a log scale (a geometric progression)meshgrid()Return coordinate matrices from coordinate vectorsmgrid()nd_grid instance which returns a dense multi-dimensional “meshgridogrid()nd_grid instance which returns an open multi-dimensional “meshgriddiag()Extract a diagonal or construct a diagonal arraydiagflat()Create a two-dimensional array with the flattened input as a diagonaltri()An array with ones at and below the given diagonal and zeros elsewheretril()Lower triangle of an arraytriu()Upper triangle of an arrayvander()Generate a Vandermonde matrixmat()Interpret the input as a matrixbmat()Build a matrix object from a string, nested sequence, or array...

Contact Us