All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python For Loop Counter Variable

Last Updated : Mar 11, 2024

Python For Loop Counter Variable

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():

  1. Iterable an object that can exist looped.
  2. 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)
  1. In the first line of code we create list named as mylist in which we provide some programming language’s names.
  2. After that we create the first for loop which does not have an index value.
  3. That’s why it takes default value i.e. 0.
  4. After that we create print function for printing the names in our list.
  5. Then we create a print statement.
  6. After that we create second for loop. In second for loop we gives the index value startIndex as 11
  7. 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.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪