Python Program For Left Array Rotation
Published on October 21 2021
In this tutorial, you will learn a python program for left array rotation. You will understand how to shift elements towards the left in an array using loop by n elements.
Source code
array = [1,2,3,4,5,6,7,8,9,10]
shift = 4
def printArray(array):
for i in range(0, len(array)):
print(array[i], end=' ')
def leftRotation(array, shift):
for i in range(0, shift):
temp = array[0] # saving First element in temp variable
for j in range(0, len(array) - 1): # shift remaining array elements one by one
array[j] = array[j + 1]
array[len(array) - 1] = temp
return array
print("Array Before Rotation: ")
printArray(array)
rotatedArray = leftRotation(array, shift)
print("\nArray after left rotation: ")
printArray(rotatedArray)
Video
Next Tutorial
- Python Program For Left Array Rotation
- Python Program For Right Array Rotation