Python program to print array element present at even position

Python program to print array element present at even position

In this program, we'll see a Python program that prints the elements of an array that are in even positions. We can do this by iterating an array's elements and incrementing the value of i by 2 till the last element of an array. Since array indexing begins with 0, i will be initialized with 1 and then incremented to 3, 5, 7, 9, and so on. After the complete iteration, the element presented at an even position will be printed.

Example:

Input: arr1 = [1, 2, 3, 4, 5]

Output = [2, 4]

Algorithm:

  • Declare and initialize an array and find the length of the array.
  • Iterates the array by initializing the variable's value to 1 and increment the value of a variable by 2.
  • Print the element at even positions.
 #Python program to display element presented at even positions 
 #Initialize an array 
 arr1 = [2, 7, 8, 9, 10, 7, 2, 22, 25]  
 print("Elements presented at even position is:")
 #Iterates the array by incrementing the value by 2  
 for i in range(1, len(arr1), 2): 
print(arr1[i])

Output