w3resource

NumPy: Create a 5x5x5 cube of 1's

NumPy: Array Object Exercise-72 with Solution

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

Pictorial Presentation:

Python NumPy: 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:

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

Previous: Write a NumPy program to create and display every element of an numpy array in Fortran order.
Next: Write a NumPy program to create an array of (3, 4) shape, multiply every element value by 3 and display the new array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]