pythoncount

How to get the count of output in python


I am trying to count the output in python. len seems to be not working with the output. Following is my code

for i in range(0, 5):
    print i

The output is

0
1
2
3
4

I want to print the count of i which should give me 5


Solution

  • There are many ways to do it but my two ways would be:

    Use len

    i = len(range(0, 5))
    print i
    

    Use for:

    j = 0
    for i in range(0, 5):
        # Other things you want to do
        j += 1
    print j