Python Program to print elements of an array in reverse order


May 12, 2022, Learn eTutorial
1341

In this simple python program, we need to print an array in reverse order. It's an intermediate-level python program.

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

How to print an array in reverse order in python?

An array is a set of elements of the same data type which is accessible by a common variable. In this basic python program, we need to print the elements in that python array in Reverse order. For example, we have an array A of elements A=[1, 2, 3, 4, 5] and then print that array in reverse order. We need to print the array as A =[5, 4, 3, 2, 1].

To print the elements in Reverse order, we have to accept an array with some elements. Then we print the real array and open a for loop in python starting from the end of the array to the beginning of the array decrementing the value by one. We use print statement in python language the print all the elements inside the for loop.

ALGORITHM

STEP 1: Initialize array A with predefined values.

STEP 2: Print the value of python array A, using a for loop from zero to length of an array using range() and len() methods used.

STEP 3; Using a print statement, print the elements in reverse order.

STEP 4: Iterate a for loop from the length of array -1 to zero using decrement by one.

STEP 5: Print the array using a print statement in the python programming language.

Python Source Code

                                           
A = [1, 2, 3, 4, 5];      # initialize the array A
  
print("Original array: "); 
   
for i in range(0, len(A)):   
 
    print(A[i]),     

print("Reverse order Array: ");    # printing the array A in reverse order
 
for i in range(len(A)-1, -1, -1):     

    print(A[i]),
                                      

OUTPUT

Original array: 
1
2
3
4
5
Reverse order Array: 
5
4
3
2
1