Updated On : Jun-26,2020 Tags numpy, basics
Learning Numpy - Simple Tutorial For Beginners - NumPy - Array Creation Routines Part 5

Learning Numpy - Simple Tutorial For Beginners - NumPy - Array Creation Routines Part 5

A new ndarray object can be constructed by any of the following array creation routines or using a low-level ndarray constructor.

numpy.empty

It creates an uninitialized array of specified shape and dtype. It uses the following constructor −

numpy.empty(shape, dtype = float, order = 'C')
Parameter Description
Shape Shape of an empty array in int or tuple of int
Dtype Desired output data type. Optional
Order 'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Example 1

In [2]:
import numpy as np
x = np.empty([3,2], dtype = int)
print (x)
[[93899461514432             16]
 [            16             16]
 [             0             97]]

numpy.zeros

Returns a new array of specified size, filled with zeros.

numpy.zeros(shape, dtype = float, order = 'C')



Parameter Description
Shape Shape of an empty array in int or tuple of int
Dtype Desired output data type. Optional
Order 'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Example 1

In [2]:
# array of five zeros. Default dtype is float 
import numpy as np
x = np.zeros(5)
print (x)
[0. 0. 0. 0. 0.]

Example 2

In [3]:
import numpy as np
x = np.zeros((5,), dtype = np.int)
print (x)
[0 0 0 0 0]

Example 3

In [5]:
# custom type 
import numpy as np
x = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])
print (x)
[[(0, 0) (0, 0)]
 [(0, 0) (0, 0)]]

numpy.ones

Returns a new array of specified size and type, filled with ones.

numpy.ones(shape, dtype = None, order = 'C')



Parameter Description
Shape Shape of an empty array in int or tuple of int
Dtype Desired output data type. Optional
Order 'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Example 1

In [6]:
# array of five ones. Default dtype is float 
import numpy as np
x = np.ones(5)
print (x)
[1. 1. 1. 1. 1.]

Example 2

In [7]:
import numpy as np
x = np.ones([2,2], dtype = int)
print (x)
[[1 1]
 [1 1]]
Dolly Solanki  Dolly Solanki

  Support Us

Thank You for visiting our website. If you like our work, please support us so that we can keep on creating new tutorials/blogs on interesting topics (like AI, ML, Data Science, Python, Digital Marketing, SEO, etc.) that can help people learn new things faster. You can support us by clicking on the Coffee button at the bottom right corner. We would appreciate even if you can give a thumbs-up to our article in the comments section below.

 Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to let us know in the comments section below (Guest Comments are allowed). We appreciate and value your feedbacks.