Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 4, 2020 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/2343b3465436ccd996006ad893f5d69a to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/2343b3465436ccd996006ad893f5d69a to your computer and use it in GitHub Desktop.
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