# np.ones() in Python | numpy.ones() in Python

Numpy `ones()` is also referred to as np.ones or numpy.ones. This function creates a Numpy array whose each element is having a value of 1. Even `array()` function can be used for creating Numpy array whose each element's value is 1. Then the next question that comes to your mind is why do we need to use `ones()` function? Just understand the reason behind using `ones()` function with the help of a simple example:

Suppose, you want to create a Numpy array having 3 rows and 10 columns, and each element's value is one. With `array()` function, you can do it like this:

```import numpy as np

np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])```

As you can see creating an array like this is a cumbersome task and it is error-prone. With `ones()` function, you can achieve the same task very easily. Let's look at it how:

`np.ones(shape=(3, 10), dtype=int)`

As you can see how simple is it to create array with ones() function.

Finally, the syntax of numpy ones() function is explained below:

```np.ones(shape, dtype=None)
OR
numpy.ones(shape, dtype=None)```

`shape` parameter is used to specify the dimensions of the Numpy array. Here, you specify the value in the form of tuple or list.

`dtype` is used to specify the data type of array and this parameter is optional.

## How to use Numpy ones function?

Python program to create a one-dimensional array with ones() function.

```import numpy as np

a = np.ones(7)
print(a)```

Output of the above program

`[1. 1. 1. 1. 1. 1. 1.]`

Python program to create numpy ones array with the specified data type

```import numpy as np

a = np.ones(7, dtype=int)
print(a)```

Output of the above program

`[1 1 1 1 1 1 1]`

Python program to create numpy ones array with the specific dimension or shape

```import numpy as np

a = np.ones(shape=(3, 6), dtype=int)
print(a)
b = np.ones((3, 6), dtype=int)
print(b)
c = np.ones([3, 6], dtype=int)
print(c)```

Output of the above program

```[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]
[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]
[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]```

As you can see in the above example, there are basically three different methods of specifying `shape` to Numpy `ones()` function.