w3resource logo
Python Exercises

Python NumPy: Create a 5x5x5 cube of 1's

Python NumPy: Array Object Exercise-72 with Solution

Write a Python program to create a 5x5x5 cube of 1's.

Sample Solution:-

Python Code:

import numpy as np
x = np.zeros((5, 5, 5)).astype(int) + 1
print(x)

Sample Output:

[[[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 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 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]]]   

Python Code Editor:

import numpy as np
x = np.zeros((5, 5, 5)).astype(int) + 1
print(x)

Improve this sample solution and post your code through Disqus