Python Code for Reverse an Array

Python code fo reverse an array

Code for Reverse an Array in Python

Here in this program will learn how to write , Python Code for Reverse an Array . Reverse an array means the last element should be displayed first, followed by second last element and so on.In Python there are 3 ways to reverse an array such:

1) Reversing a list in-place with the list.reverse() method
2) Using the “[::-1]” list slicing trick to create a reversed copy
3) Creating a reverse iterator with the reversed() built-in function

Here we applied list slicing trick to reverse an array
For Example:
Enter the size of array: 6
Enter the elements: 8
4
91
34
10
7
Reversed array is: 7
10
34
91
4
8

Algorithm for reverse an array using Python

STEP 1: Declare and initialize an array.
STEP 2: Iterate the loop through the array in reverse order.
STEP 3: The loop will start from for i in range(size).
STEP 4: Print the element arr[i] in each iteration.

Python Program for reverse an array or list

size=int(input("ENTER ARRAY SIZE: "))
arr=[]
print("ENTER THE ELEMENTS: ")
for i in range(size):
    element=int(input())
    arr.append(element)
print("THE REVERSED ARRAY IS: ",*arr[::-1],sep="\n") 
Output
ENTER ARRAY SIZE: 5
ENTER THE ELEMENTS:
12
91
23
43
9
THE REVERSED ARRAY IS:
9
43
23
92
12