Write a Python program to print an array in reverse order

How to write a Python program to print the array in reverse order? Before start the program let’s understand what is an array and reverse order in a Python array.

What is an array?

An array is a collection of items stored at contiguous memory locations. It stores multiple items of an equivalent data type. This makes it simpler to figure the position of each component. The variables in the array are ordered and the indexing begins from 0 and hence the last element will be n-1 where n is the number of elements.

What is a reverse order?

Input: Array elements = [12, 15, 18, 21, 24, 27]
Output: Array in reverse order = 27, 24, 21, 18, 15, 12

Algorithm to print an array in reverse order:

  • Initialize start and end indexes of the array as start = 0, end = n-1.
  • Start a loop in reverse order i.e. starting from n-1, till 0, and decrement by 1.
  • Print the loop.

Python program to print an array in reverse order.

from array import *
arr = array('i', [])
n = int(input("enter number of elements"))
for i in range(n):
   arr.append(int(input("enter the array elements")))

print("entered array is:")
for i in range(len(arr)):
   print(arr[i])
print("Array in reverse order: ")
for i in range(len(arr)-1, -1, -1):
   print(arr[i])

Output: Reversing an array of elements