Program to right rotate the elements of an Array in python. This program contains For iterative loops, initialisation of Array and print statements. This is for beginners, class 11,12 and B Tech computer science students.
Program to right rotate the elements of an Array
What is an Array?
A Python Array is a collection of common type of data structures having elements with same data type.
What the program is meant to do :
we need to rotate the elements of array towards its right by the specified number of times. An array is said to be right rotated if all elements of the array are moved to its right by one position. One approach is to loop through the array by shifting each element of the array to its next position. The last element of the array will become the first element of the rotated array.

Source code :
arr = [1, 2, 3, 4, 5];
n = 3;
print("Original array: ");
for i in range(0, len(arr)):
print(arr[i]),
for i in range(0, n):
last = arr[len(arr)-1];
for j in range(len(arr)-1, -1, -1):
arr[j] = arr[j-1];
arr[0] = last;
print();
print("Array after right rotation: ");
for i in range(0, len(arr)):
print(arr[i]),
Source code Explanation :
step 1:Initialize array
arr = [1, 2, 3, 4, 5];
step 2:n determine the number of times an array should be rotated
n = 3;
step 3:Displays original array
print(“Original array: “);
for i in range(0, len(arr)):
print(arr[i]),
step 4:Rotate the given array by n times toward right
for i in range(0, n):
step 5: Stores the last element of array
last = arr[len(arr)-1];
for j in range(len(arr)-1, -1, -1):
step 6:Shift element of array by one
arr[j] = arr[j-1];
step 7:Last element of the array will be added to the start of the array.
arr[0] = last;
print();
step 8:Displays resulting array after rotation
print(“Array after right rotation: “);
for i in range(0, len(arr)):
print(arr[i]),
Output :
RESTART: C:/csstudy/source code file 2/Program to right rotate the elements of an Array in python.py Original array: 1 2 3 4 5 Array after right rotation: 3 4 5 1 2 >>>
RESTART: C:/csstudy/source code file 2/Program to right rotate the elements of an Array in python.py
Original array:
1
2
3
4
5
Array after right rotation:
3
4
5
1
2
First the original array is shown then calculation are started in the program and then output is printed as Array after right rotation.
Conclusion :
Everything is running fine.
Thank you for reading, learning and downloading the source code of Program to right rotate the elements of an Array in python. This program contains For iterative loops, initialisation of Array and print statements. This is for beginners, class 11,12 and B Tech computer science students. For more program, practical file of class 11,12 and Projects are given in the link below :
/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