Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created Jul 4, 2020
Embed
What would you like to do?
import itertools
c=itertools.count()
#next() function returns next item in the iterator.By default starts with number 0 and increments by 1.
print (next(c))#Output:0
print (next(c))#Output:1
print (next(c))#Output:2
print (next(c))#Output:3
#Returns an infinite iterator starting with number 5 and incremented by 10. The values in the iterator are accessed by next()
c1=itertools.count(5,10)
print(next(c1))#Output:5
print(next(c1))#Output:10
print(next(c1))#Output:15
#accessing values in the iterator using for loop.step argument can be float values also.
c2=itertools.count(2.5,2.5)
for i in c2:
#including terminating condition, else loop will keep on going.(infinite loop)
if i>25:
break
else:
print (i,end=" ") #Output:2.5 5.0 7.5 10.0 12.5 15.0 17.5 20.0 22.5 25.0
#step can be negative numbers also.negative numbers count backwards.
c3=itertools.count(2,-2.5)
print (next(c3))#Output:2
print (next(c3))#Output:-0.5
print (next(c3))#Output:-3.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment