In this post, we’ll learn what Python for loop is and how to properly use it in different code situations.
We’ll be explaining it step by step through proper practical examples for better understanding.
After reading this article, you’ll have a complete grip on how to properly use Python for loop.
What is Python For Loop?
As the names suggest, it is used for looping(repetition). It’s used to iterate over iterable objects in Python. It can also iterate over a sequence like string, list or tuple etc.
We’ll first see what the syntax of for loop is. Then we’ll see how to practically implement it using easy but proper code examples.
Syntax of For Loop
for val in sequenceName: body of loop
As you can see, it starts from the for keyword.
After that, a variable is used which takes the value of each item of a list, tuple, string etc. and the loop continues to execute until it reaches the last item.
We then need to pass the name of sequence which can be a name of list, tuple, string etc. This line ends with a colon(:).
The body of for loop is specified using the indentation.
Implementing Python For Loop (Multiple Examples)
Let’s look at some practical code examples to better understand how for loop works.
Example 1: Looping through a String
name='Zeeshan' for val in name: print(val) print('The loop is completed.')
Z e e s h a n The loop is completed.
You can see that we have a string variable and we have passed it to the for loop. Now the loop will take the first item of that string and pass it to the variable.
You can see in the above output that all the items of a string are printed successfully. When all the items are passed, then the loop finishes and the print statement that is after this Python for loop gets printed.
Example 2: Looping through a List
listOfColors=['white','green','blue','purple','grey'] for val in listOfColors: print(val) print('The loop is completed.')
white green blue purple grey The loop is completed.
You can see that all the items of list are printed using for loop. This loop will start by taking the first item of list, then the second one and so on until it reaches the last item. After that, the loop will stop and the statement that comes after the loop will get executed.
Example 3: For Loop with Range Function
We can use the range() function to specify the number of items. See below code:
for val in range(5): // 0,1,2,3,4 print(val) print('The loop is completed.')
0 1 2 3 4 The loop is completed.
You can see that the range function has a specific value and it created the sequence starting from 0 to this value -1.
You can specify the starting point of range like this:
for val in range(1,10): // 1,3,....9 print(val) print('The loop is completed.')
2 3 4 5 6 7 8 9 The loop is completed.
You can see that 1 specifies the second item of range. The reason is that range starts from 0. So 0,1,2,….
You can specify the steps like this:
for val in range(1,10,2): print(val) print('The loop is completed.')
1 3 5 7 9 The loop is completed.
Pass some other values as a step(3rd argument of range function) to see what result is generated from it.
Example 4: For Loop with Else Statement
listOfColors=['white','green','blue','purple','grey'] for val in listOfColors: if val=='red': break else: // this else is with the for loop print('Color not found') print('Color found')
Color not found // else block is executed as the item is not found Color found // this statement is outside the loop which will execute anyways
You can see that we can use else with Python for loop. Break is used to come out of the loop if the condition is satisfied.
So this is how you can easily use Python for loop in your own code as well.
Feel free to ask if you still have any questions related to the implementation of Python for loop. I’ll be very glad to answer all your questions.
As a conclusion of this post, hope you now have a clear in-depth practical understanding of how to use Python for loop. I would love to have your feedback on this post. Thank you for reading it.