Python Program to print the elements in even position of array

In this simple python program, we have to print the even elements of an array. It's a list-based python program.

To understand this example, you should have knowledge of the following Python programming topics:

How to print the even elements in an array?

In this simple python program, we need to print the elements present on an array's even positions. So we have to understand how an array works in python. In an array, elements are stored in the sequence of memory locations, which can be indexed by a variable. To get the first element, we use the index a[0], which is stored in position one.

For example, let us take an example array A = [0, 1, 2, 3, 4, 5]; here, the element 0 is stored in position one and can be accessed by A[0]. So for each element position is one greater than the index.

Position  1  2  3  4  5
array A=[ a, b, c, d, e ]
Index =   0  1  2  3  4 

 

To solve the beginner python program, we use the for loop in python starting from the position of the array one to the length of the array to reach the end of the array by incrementing by 2 to get the next even index of the array, So we use the element b in the first iteration because array[1] is position 2. We get the d as it's incremented by two. 

ALGORITHM

STEP 1: Initialize the array X using some predefined elements.

STEP 2: Use a print statement to print " the elements in even position."

STEP 3: Use a for loop starting from one till the array X's length by incrementing by 2. We use one to begin the For loop because X[1] will be in position two.

STEP 4: Print even elements in array X using print statements in the python programming language.

Python Source Code

                                               
arr = [1, 2, 3, 4, 5];     
     
print("Array elements in even position : ");    

for i in range(1, len(arr), 2):    # use the for loop to get the even index position elements. 
    print(arr[i]);   
                                      

OUTPUT

Array elements in even position : 
2
4