Write a Python Program to Print Numpy Array Items. We can use the print statement to print the numpy array items.
import numpy as np arr = np.array([15, 20, 40, 78, 50, 99, 248, 122]) print("*** Numpy Array Items ***") print(arr) strarr = np.array(['India', 'USA', 'Cananda', 'Japan']) print("\n*** Numpy Array Items ***") print(strarr)
Print Python Numpy Array items output
*** Numpy Array Items ***
[ 15 20 40 78 50 99 248 122]
*** Numpy Array Items ***
['India' 'USA' 'Cananda' 'Japan']
Python Program to Print Numpy Array Items using the For Loop
The for loop (for num in arr) iterates from the first numpy array item to the last array item, and the print statement prints the array items.
import numpy as np arr = np.array([15, 20, 40, 78, 50, 99, 248, 122]) print("*** Numpy Array Items ***") for num in arr: print(num) print("\n*** Numpy Array Items ***") for num in arr: print(num, end = " ")
Print Python Numpy Array items using a for loop output
*** Numpy Array Items ***
15
20
40
78
50
99
248
122
*** Numpy Array Items ***
15 20 40 78 50 99 248 122
In this Python example, we used the for loop range (or i in range(len(arr))) to iterate the numpy array using the index positions. The print statement prints the numpy array item at each index position.
import numpy as np arr = np.array([12, 20, 50, 18, 33, 50, 99]) print("*** Numpy Array Items ***") for i in range(len(arr)): print("Array Item at %d Position = %d" %(i, arr[i]))