Python For Loop Counter Variable
Last Updated : Mar 11, 2024
In this article we will show you the solution of Python for loop counter variable, a for loop is applied for repeating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
In simple terms, a counter variable is a variable that keeps trace of the number of times a special piece of code is implemented.
Step By Step Guide On Python For Loop Counter Variable :-
In Python, a for loop is generally written as a loop over an iterable object. This means you don’t require a counting variable to enter details in the iterable.
Occasionally, still, you do require to have a variable that changes on each loop iteration. preferably than creating and incrementing a variable yourself, you can utilize Python’s enumerate() to get a counter and the value from the iterable at the same occasion!
Python Enumerate() is a buit- in function accessible with the Python library.
It takes the contributed input as a collection or tuples and replies it as an enumerate object.
The Python Enumerate() command adds a balance to each detail of the iterable object and returns an enumerate object as an output string.
Following are the parameters of enumerate():
- Iterable an object that can exist looped.
- StartIndex(elective) The count will jump with the value given in the startIndex for the original detail in the loop and increase it for the nextitem till it reaches the ending of the loop. Still, If startIndex isn't defined, the count will start from 0.
Enumerate allows you to loop through a list, tuple, dictionary, string, and gives the valuations along with the index.
To pick up index value applying for- loop, you can form operation oflist.index(n). still,list.index(n) is actually high as it'll transit the for- loop doubly.
Enumerate is truly useful in such a case as it gives the index and details at one mode.
mylist = ['C','Python','Java','SQL'] for i in enumerate(mylist): print(i) print("Using startIndex as 11") for i in enumerate(mylist, 11): print(i)
- In the first line of code we create list named as mylist in which we provide some programming language’s names.
- After that we create the first for loop which does not have an index value.
- That’s why it takes default value i.e. 0.
- After that we create print function for printing the names in our list.
- Then we create a print statement.
- After that we create second for loop. In second for loop we gives the index value startIndex as 11
- Then we print the second loop by using print function.
Conclusion :-
Hence, we successfully learnt about the concept of counter loop variable.
We learnt about the enumerate() function it is nothing but Enumerate is actually useful when you require the index and the valuation when looping over list, tuple, dictionary, etc.
In Enumeration in Python, you can define the startIndex, i.e., the counter you require the values to start from.
I hope this article on Python for loop counter variable helps you and the steps and method mentioned above are easy to follow and implement.