Write a Python program to print array elements present on even position or even index position. In this example, we used the list slicing that will increment by two to print the array elements present in an even position.
import numpy as np arr = np.array([2, 4, 6, 8, 12, 14, 16, 18]) print('Printing the Array Elements at Even Position') print(arr[1:len(arr):2])
Python program to print array elements present on even index position using a for loop.
import numpy as np arr = np.array([22, 44, 66, 88, 122, 144, 166, 188]) for i in range(1, len(arr), 2): print(arr[i], end = ' ')
44 88 144 188
This program helps to print array elements present at the even position using a while loop
import numpy as np arr = np.array([11, 13, 15, 17, 19, 22, 39]) i = 1 while i < len(arr): print(arr[i], end = ' ') i = i + 2
13 17 22
In this example, we used the if statement to find the even index position and print the array elements present on the even position.
import numpy as np evarrlist = [] evarrTot = int(input("Total Array Elements to enter = ")) for i in range(1, evarrTot + 1): evarrvalue = int(input("Please enter the %d Array Value = " %i)) evarrlist.append(evarrvalue) evarr = np.array(evarrlist) print('\nPrinting the Array Elements at Even Position') for i in range(1, len(evarr), 2): print(evarr[i], end = ' ') print('\nPrinting the Array Elements at Even Position') for i in range(len(evarr)): if i % 2 != 0: print(evarr[i], end = ' ')
Total Array Elements to enter = 9
Please enter the 1 Array Value = 22
Please enter the 2 Array Value = 43
Please enter the 3 Array Value = 56
Please enter the 4 Array Value = 23
Please enter the 5 Array Value = 65
Please enter the 6 Array Value = 98
Please enter the 7 Array Value = 56
Please enter the 8 Array Value = 11
Please enter the 9 Array Value = 99
Printing the Array Elements at Even Position
43 23 98 11
Printing the Array Elements at Even Position
43 23 98 11