It is essential for developers to know how to count in a for
loop in Python. The work helps you manage specific numbers of elements in a list you desire. In this article, we will go together to see the easiest tips to count in a for
loop. Scroll down to see!
How to count in a for loop in Python
Below are the top tips on how to count in a for loop in Python.
Using an int variable starting from 0
The way is to create an integer variable. You assign the value 0 to it. After that, add this variable by 1 and print the element every time the for
loop takes a turn. When the for
loop completes, the value of this integer variable will be the number of elements in the list
.
Here is the syntax for this tip:
[variable] = 0
for [loopTurn] in [list]:
[variable] += 1
Here is the sample code:
# A list of many different users lstUser = ["user1", "user2", "user3", "user4"] # The variable countUser is used for counting in the for loop countUser = 0 # Count in the for loop for user in lstUser: countUser += 1 print(countUser, ": ", user) print("The number of users: ", countUser)
The output will be:
1 : user1
2 : user2
3 : user3
4 : user4
The number of users: 4
If you don’t want the for
loop to count the entire list
, you can rely on the if
statement to break the loop. See the sample below:
# A list of many different users lstUser = ["user1", "user2", "user3", "user4"] # The variable countUser is used for counting in the for loop countUser = 0 # Count in the for loop for user in lstUser: countUser += 1 print(countUser, ": ", user) if countUser == 3: break print("The number of users: ", countUser)
The output will be:
1 : user1
2 : user2
3 : user3
The number of users: 3
Using the range()
If you don’t want to count the entire list
but just the specific number that you desire, then you can take advantage of the range()
.
The syntax is like this:
range(0, [parameter])
The parameter will take the number you desire. For example, if you choose the number 3 for the parameter, the for
loop will count from 0 to 3.
Here is the code sample:
# A list of many different users lstUser = ["user1", "user2", "user3", "user4"] # Count in the for loop for countUser in range(0, 3): countUser += 1 print(countUser, ": ", lstUser[countUser]) print("The number of users: ", countUser)
The output will be:
1 : user2
2 : user3
3 : user4
The number of users: 3
Using the enumerate()
The enumerate()
function returns both the count and the value. Therefore, you can rely on this function to count in the for
loop in Python.
The syntax will be:
for [variable1], [variable2] in enumerate([list])
This function returns the count number to the variable1, and the value in the list to the variable2.
See the code sample below:
# A list of many different users lstUser = ["user1", "user2", "user3", "user4"] for countUser, user in enumerate(lstUser): print(countUser + 1, ": ", user) print("Number of users: ", countUser + 1)
The output will be:
1 : user1
2 : user2
3 : user3
4 : user4
Number of users: 4
The enumerate()
considers the index of the first element as 0. Therefore, you need to add the count variable by 1 when printing it out.
Summary
Above is how to count in the for
loop in Python. Using the integer variable is the most basic way. Any developer can use this way to count in for
loop with ease. The others are also good. You can use them as long as they bring convenience to your Python programming!
Leave a Reply