Skip to content
Home » Python Programs » Python Program to Left Rotate the Elements of an Array

Python Program to Left Rotate the Elements of an Array

Simple python program to left rotate the elements of an array. In this program we can rotate the elements of an array in leftwards in python programming language for beginners.

Function Definition in Python

A function is a piece of code that runs only when it is called. Python functions return a value if a return statement is used. A function can be called from anywhere once it has been declared. In computer programming, a function is a named section of code that performs a specific task. Typically, this entails taking data, manipulating it, and then returning a result.

append() Function in Python

In Python, the append() method adds a single item to an existing list. It does not return a new list of items, but it does add the item to the end of the existing list. The size of the list grows by one after calling the append method on it. In this python program to left rotate the elements of an array we have executed with the append function.

Python Program to Left Rotate the Elements of an Array

def rotate(arr, n, d):
	temp = []
	i = 0
	while (i < d):
		temp.append(arr[i])
		i = i + 1
	i = 0
	while (d < n):
		arr[i] = arr[d]
		i = i + 1
		d = d + 1
	arr[:] = arr[: i] + temp
	return arr
arr = [10,20,30,40,50]
print("Array After Left Rotation", end=' ')
print(rotate(arr, len(arr), 2))

Output:

Array Left Rotation [30, 40, 50, 10, 20]