What does .shape[] do in “for i in range(Y.shape[0])” using Matplotlib?


The shape property is usually used to get the current shape of an array, but it may also be used to reshape the array in-place by assigning a tuple of array dimensions to it.

Steps

  • Get an array Y using np.array method.

  • Y.shape would return a tuple (4, ).

  • Y.shape[0] method would return 4, i.e., the first element of the tuple.

Example

import numpy as np

Y = np.array([1, 2, 3, 4])
print("Output of .show method would be: ", Y.shape, " for ", Y)
print("Output of .show[0] method would be: ", Y.shape[0], " for ", Y)
print("Output for i in range(Y.shape[0]): ", end=" ")
for i in range(Y.shape[0]):
   print(Y[i], end=" ")

Output

Output of .show method would be: (4,) for [1 2 3 4]
Output of .show[0] method would be: 4 for [1 2 3 4]
Output for i in range(Y.shape[0]): 1 2 3 4

Updated on: 17-Mar-2021

835 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements