Program to left rotate the elements of an Array in Python

Program to left rotate the elements of an Array in Python. This program uses inputs, for loop, print commands and Array initialisation. This is for Beginners, class 11,12 and B Tech computer science students.

Program to left rotate the elements of an Array in Python

What should happen ?

In this program, rotate the elements of an array towards the left by the specified number of times. In the left rotation, each element of the array will be shifted to its left by one position and the first element of the array will be added to end of the list. This process will be followed for a specified number of times.

Python program to left rotate the elements of an array

Consider above array, if n is 1 then, all elements of the array will be moved to its left by one position such that second element of the array will take the first position, the third element will be moved to the second position and so on. The first element of the array will be added to the last of the array

Source code :

#Enter the length of Array
n= int(input("Enter the length of Array"))
#Intialising a array
a=[None]*n
s=[None]*n


#running a for loop for the enter of array
for i in range (0,n):
    m= int(input( " Enter the content of Array")) 
    a[i]=m
p=int(input("rotate the n.o array to left with this n.o of rotation"))
t=[None]*p
for j in range (0,p):
    t[j]=a[j]
    
        
#Moving the number to left        
for i in range (0,n):   
    if ((n-p)>0 and (i+p)<n):
        a[i]=a[i+p]
        #print (a[i])
        s[i]=a[i]
    
for i in range (0,p):
    s[n-p+i]=t[i]
print(s)

Source code explanation :

step 1: Enter the length of Array
n= int(input(“Enter the length of Array”))
step 2:Intialising a array
a=[None]n s=[None]n
step 3: running a for loop for the enter of array
for i in range (0,n):
m= int(input( ” Enter the content of Array”))
a[i]=m
p=int(input(“rotate the n.o array to left with this n.o of rotation”))
t=[None]*p
for j in range (0,p):
t[j]=a[j]
step 4:Moving the number to left
for i in range (0,n):
if ((n-p)>0 and (i+p)<n):
a[i]=a[i+p]
s[i]=a[i]
for i in range (0,p):
s[n-p+i]=t[i]
print(s)

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\Python program to left rotate the elements of an array.py 
Enter the length of Array5
 Enter the content of Array1
 Enter the content of Array2
 Enter the content of Array3
 Enter the content of Array4
 Enter the content of Array5
rotate the n.o array to left with this n.o of rotation4
[5, 1, 2, 3, 4]
>>> 

Output Explanation :

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\Python program to left rotate the elements of an array.py
Enter the length of Array5
Enter the content of Array1
Enter the content of Array2
Enter the content of Array3
Enter the content of Array4
Enter the content of Array5
rotate the n.o array to left with this n.o of rotation 4
[5, 1, 2, 3, 4]

  1. Total length of Array is entered.
  2. Array is entered.
  3. then the rotations to the left is asked.
  4. After rotation the output is published.

Conclusion:

The program is running fine.

Thank you for learning, reading and downloading Program to left rotate the elements of an Array in Python. This program uses inputs, for loop, print commands and Array initialisation. This is for Beginners, class 11,12 and B Tech computer science students. For more programs check the below links below.
Python Programs Archives – CS Study
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.