# How to Count in Python Loop

This tutorial discusses about how to count in Python loop.

## Count in a Loop in Python

In Python, we have many objects that can be treated as iterable. An iterable is an object like a list, tuple, or any other sequence of elements that can be iterated using a simple `for` loop.

Counting in a loop means making a note of every iteration to find the total number of iterations after the loop has stopped running.

## How to Count in A Loop in Python

This tutorial will demonstrate ways how to count in a loop in Python.

### Using a Counter Variable to Count in A Loop in Python

This method is very straightforward. We will declare a variable outside the loop and assign it a value of zero. In every iteration, we will increment its value and print it.

See the code below.

Output:

1
2
3

In the above example, we create a list `lst` that contains three elements. The variable `c` is initialized with zero. We use the `for` loop to iterate over this list. In every iteration, we increment the variable `c` and print its value.

### Using the `enumerate()` Function to Count in A Loop in Python

The `enumerate()` function takes an iterable and assigns a counter variable to each element. This way, every element gets assigned a value that can act as the index of the element.

We can use this index variable to act as a counter variable for the loop.

See the code below.

Output:

0
1
2

In the above example, we display the index of the element after using the `enumerate()` function and iterating it. Note that the index starts from 0.

### Using the `zip()` Function to Count in A Loop in Python

The `zip()` function can be used to combine two iterables into one by combining the individual elements at corresponding positions together.

We can create a list of the length iterable with the `range()` function. The two can be then combined to create a new iterable. This method is just another way to emulate the `enumerate()` function discussed previously.

The code is demonstrated below.

Output:

0
1
2

The `range()` function returns an object containing a sequence to the length of the list. This is combined with the list and the counter value is displayed.

## Conclusion

To conclude, we discussed how to count in a loop in Python. In the first method, we initialize a variable and increment it in every iteration. The other methods involve the `enumerate()` and `zip()` functions to create an index for the iterable that can act as a counter variable.

That’s all about how to count in Python loop.

import_contacts

## Related Posts

• 25 June

### Convert Roman Number to Integer in Python

Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

• 11 June

### Remove Single Quotes from String in Python

Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

• 11 June

### Escape Backslash Character in Python

Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

• 11 June

### Break out of function in Python

Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

• 11 June

### Check if Object Is Iterable in Python

Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

• 11 June

### Save Numpy Array as Image in Python

Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.