For loops are used mainly to iterate over a collection of integer or character literals in python. It is useful when we have a list of items, and we have to go through them one by one. For loops are one of the most important parts of any computer programming languages. It is also an important keyword.
Inside a for loop we can perform actions or pass arguments as required for each iteration. We can iterate through sets, dictionaries, tuples, lists and even strings using for loop in python.
Mostly “for” loops are used for going over from the very first index through to the last, we can also use it to iterate in the reverse order.
Syntax of For Loop in Python
For loops are written in the following way
for element in dataset:
In this case, the for loop iterates through every individual item in the “dataset” array, and the variable “element” gets assigned the value of the items one by one from the list. When nothing is mentioned, the for loop will by default, run from the very first position of the element to the last. Hence, the loop will move from the 0th position to the (N-1)th position, where N is the length of the dataset such as array, list, or tuple.
Suggested: Control flow tools in python.
For loop range() function
Using the range() function we can specify the position from where the iteration should start and where it should end. We can even specify the “step” or the length of the jump from one element to the next one.
The syntax of the range function is as follows:
for i in range(start, stop, step):
For example, if the value of start = 0 and stop = length of the required list and step= 2, then the for loop will iterate over every second element in the entire list.
Here, “i” stands for the index of the elements, that is, their positions and not their values. Since in computer science every position starts from zero, the last element will also be situated at the (length-1)th position.
If the step is not mentioned, then the default value assigned to that variable is always 1. Hence, it will iterate through every element in a data set, one by one, if not explicitly mentioned otherwise.
The range() function can also just be written as: for i in
range(N) where N is the length of the list or string. In this case, the for loop will by default start from the 0th position all the through to the end, one by one. Here “i” denotes the same as before.
Creating a Decreasing/Decrementing For Loop in Python
range() function can be used in addition with the for loop in python, we can use it to iterate through a data set backwards. Hence, string reversals can be easily done using these two statements in Python.
The syntax for this purpose will have the following changes:
for i in range(N-1,-1,-1)
In this case, the value of i will start from the very last position. N is the length of the data set (say, for example, a list), hence the last element will be situated at the (N-1)th position.
The first element is situated at the 0th position, hence we had to mention -1 as the end because the for loop stops looping at the (stop+1)th position. The “step” is set to a negative one because we are moving in a reverse order from the very last to the very first one by one.
Decreasing in a for loop in Python – Example 1
Let us look at examples for better understanding. In this example, let us take an arbitrary list from the user and print all its elements in a decreasing order using a for loop.
# initializing original list lst =  # creating an empty list # determining the size of the list N = int(input("Enter the size of list=")) for i in range(N): # taking user input in a forward order from 0 to N-1 ele = input("Enter " + str(i + 1) + "th element of the list= ") # input for items lst.append(ele) # creating the list # displaying the list print("The original list is=", lst) # displaying the decrementing list print("The list in the decreasing order is=") print("[", end="") for i in range(N - 1, -1, -1): print(lst[i], end=",") print("]") print()
The output would be:
Enter the size of list=4 Enter 1th element of the list= Hi Enter 2th element of the list= Bye Enter 3th element of the list= Why Enter 4th element of the list= How The original list is= ['Hi', 'Bye', 'Why', 'How'] The list in the decreasing order is= [How,Why,Bye,Hi,]
Reversing a string- Example 2.
Let’s reverse a string using a for loop in the decrementing order in python.
# initializing empty string S = input("Enter the required string= ") # displaying the string print("The original string is=", S) # displaying the reverse of the string print("The reverse is= ", end="") N = len(S) # determining the length of the string # USing a decreasing for loop for i in range(N - 1, -1, -1): print(S[i], end="") print()
The output would be:
Enter the required string= Hello world! The original string is= Hello world! The reverse is= !dlrow olleH
In this article, we have gone over the various features of “for” loops in python. Not only can we go through datasets from the first position to the last, but we can also do the reverse. Python allows us to iterate over elements from the very end to the front step by step as required using the range() function. Reversing strings, lists and tuples are extremely easy using this method. It is one of the most common control flow tool in computer programming. To know more about for loops visit the official python documentation.