numpy.ones() in Python

Python numpy.ones() function returns array of given size and shape filled with ones (1’s). In another words it will return array which is filled with 1’s.

Basic Syntax

Following is the basic syntax for numpy.ones() function in Python:

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

And the parameters are:

Parameter Description
shape This parameter specifies integer of sequence of integers.
dtype Optional. The data type of array elemnts that will be returned.
order Optional. For row major provide C or for column major provide F (Fortron style) to store multi-dimensional data in memory.

Return Value

numpy.ones() function returns array filled with 1’s.


Following is the example of numpy.ones() function

# Python program for demonstration of numpy.ones() function
import numpy as np 
first = np.ones(4, dtype = int) 
print("Array first : \n", first) 
second = np.ones([3, 3], dtype = int) 
print("\nArray second : \n", second) 
third = np.ones([2, 2]) 
print("\nArray third : \n", third) 

The output for the above program is as given below:

Array first :
[1, 1, 1, 1]Array second :
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]

Array third :
[[ 1., 1.],
[ 1., 1.]]