How To Count In A For Loop In Python

Count in a for loop in Python

Using count variable in a for loop can help you count in a for loop in Python. Below are specific steps to find it. Read on it.

Some ways to count in a for loop in Python

Method 1: Using count variable in a for loop

The simplest way to count in a for loop in Python is to initialize a count variable and set it to a number. 

After each iteration in a for loop, increase the value of the variable count by 1. At the end of the for loop, the variable count will calculate the number of iterations for that loop.

The program below will explain:

count = 0
my_cart = ['apple', 'snack', 'banana', 'paper']

for item in my_cart:
    count += 1
print(f'The number of items in the cart is: {count}')

Output:

The number of items in the cart is: 4

You can also print the value with the count variable like this:

count = 0
my_cart = ['apple', 'snack', 'banana', 'paper']

for item in my_cart:
    count += 1
    print(f’Count: {count}, Value: {item}’)

Output:

Count: 1, Value: apple 
Count: 2, Value: snack
Count: 3, Value: banana
Count: 4, Value: paper

Method 2: Using enumerate() method

Instead of using a count variable to count in a for loop, Python has an enumerate() method to support and make it easier.

Syntax: 

enumerate(iterable, start=0)

Parameters:

  • Iterable: any object can use an iterable like array or string.
  • Start: starting index value, default is 0.

This is an example used in for loop:

my_cart = ['apple', 'snack', 'banana', 'paper']
for count, item in enumerate(my_cart):
  print(count, item)

Output:

0 apple
1 snack
2 banana
3 paper

When you use this method, the program will return the following two variables:

  • count: count variable of the current iteration.
  • item: the value of the corresponding count variable.

The initial default value of the count variable is 0. However, Python has support to change the initial value for this method by setting the value of the start variable as follows:

my_cart = ['apple', 'snack', 'banana', 'paper']
for count, item in enumerate(my_cart, start = 1):
    print(count, item)

Output:

1 apple
2 snack
3 banana
4 paper

Note: count and value variables can change names. Otherwise, start cannot change the name, this is the default name of the method.

Besides arrays, this method can also be applied to strings as follows:

my_name = 'Edward'
for count, item in enumerate(my_name, start = 1):
  print(count, item)
print(f'your name has {count} characters')

Output:

1 E
2 d
3 w
4 a
5 r
6 d

your name has 6 characters

Summary

You can use manual or built-in python methods to count in a for loop in Python. For the manual way, you can apply other types like while loop or do while loop, etc.

Conversely, the enumerate() method is quite flexible, but this is usually only used in a for loop. Each way will have pros and cons, let’s customize it to the most optimal code.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *