Program to print the elements of an Array in Reverse order in Python

Program to print the elements of an Array in Reverse order in Python. This program contains array, for loop and print statement. This program is for Beginners, class 11, 12 and B Tech computer science students.

Program to print the elements of an Array in Reverse order

What is an Array?

An array is a collection of similar data elements stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number.

What to do in this program :

In this program, we need to print the elements of the array in reverse order that is; the last element should be displayed first, followed by second last element and so on.

Python program to print the elements of an array in reverse order

Above array in reversed order:

Python program to print the elements of an array in reverse order

Source code :

     
arr = [1, 2, 3, 4, 5];     
print("Original array: ");    
for i in range(0, len(arr)):    
    print(arr[i]),     
print("Array in reverse order: ");    
    
for i in range(len(arr)-1, -1, -1):     
    print(arr[i]),

Source code explanation:

step 1 :Initialize array
arr = [1, 2, 3, 4, 5];
print(“Original array: “);
for i in range(0, len(arr)):
print(arr[i]),
print(“Array in reverse order: “);
step 2 :Loop through the array in reverse order
for i in range(len(arr)-1, -1, -1):
print(arr[i]),

Output :

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/csstudy/source code file 2/Program to print the elements of an Array in Reverse order in Python.py 
Original array: 
1
2
3
4
5
Array in reverse order: 
5
4
3
2
1
>>> 

Output Explanation :

RESTART: C:/csstudy/source code file 2/Program to print the elements of an Array in Reverse order in Python.py
Original array:
1
2
3
4
5
Array in reverse order:
5
4
3
2
1

First the program print the original array.
Then by applying loop reverses it.

Conclusion :

The program is running fine.

Thank you, for reading, learning and downloading source code of Program to print the elements of an Array in Reverse order. This program contains array, for loop and print statement. This program is for Beginners, class 11, 12 and B Tech computer science students. For more programs click below :
csstudy.in/category/python-programs/
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study
Python Projects for Class 12 with Source Code (Mysql Connectivity) – CS Study

Leave a Comment

Your email address will not be published.