w3resource
NumPy Exercises

NumPy: Create an array of 10 zeros, 10 ones, 10 fives

NumPy: Basic Exercise-13 with Solution

Write a NumPy program to create an array of 10 zeros, 10 ones, 10 fives.

Sample Solution :

Python Code :

import numpy as np
array=np.zeros(10)
print("An array of 10 zeros:")
print(array)
array=np.ones(10)
print("An array of 10 ones:")
print(array)
array=np.ones(10)*5
print("An array of 10 fives:")
print(array)

Sample Output:

An array of 10 zeros:
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
An array of 10 ones:
[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
An array of 10 fives:
[ 5.  5.  5.  5.  5.  5.  5.  5.  5.  5.]     

Pictorial Presentation:

NumPy: Create an array of 10 zeros, 10 ones, 10 fives.
NumPy: Create an array of 10 zeros, 10 ones, 10 fives.
NumPy: Create an array of 10 zeros, 10 ones, 10 fives.

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to create an array with the values 1, 7, 13, 105 and determine the size of the memory occupied by the array.
Next: Write a NumPy program to create an array of the integers from 30 to70.

What is the difficulty level of this exercise?