w3resource

NumPy: Print all the values of an array

NumPy: Array Object Exercise-97 with Solution

Write a NumPy program to print all array values.

Pictorial Presentation:

Python NumPy: Print all the values of an array

Sample Solution:

Python Code:

import numpy as np
import sys
# Setting the print options to display the entire array without truncation
# Use sys.maxsize or np.inf for untruncated representation
np.set_printoptions(threshold=sys.maxsize)
# Creating a 4x4 NumPy array 'x' filled with zeros
x = np.zeros((4, 4))
# Printing the array 'x'
print(x)

Sample Output:

[[ 0.  0.  0.  0.]                                                     
 [ 0.  0.  0.  0.]                                                     
 [ 0.  0.  0.  0.]                                                     
 [ 0.  0.  0.  0.]]

Explanation:

In the above code -

np.set_printoptions(threshold=np.nan): Set the print options in NumPy so that the entire array will be printed without truncation, regardless of its size. The 'threshold' parameter is set to 'np.nan' to ensure that all elements are displayed.

x = np.zeros((4, 4)): This line creates a 4x4 NumPy array 'x' filled with zeros.

print(x): Print the 4x4 array 'x', with all elements displayed due to the print options set earlier.

Python-Numpy Code Editor:

Previous: Write a NumPy program to divide each row by a vector element.
Next: Write a NumPy program to convert the raw data in an array to a binary string and then create an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/python-numpy-exercise-97.php