0

Can someone explain this cycle?

19th May 2017, 6:25 PM
Eliza Howell
Eliza Howell - avatar
2 Réponses
+ 1
itertools itself is not a cycle it's a library with many useful iteration tools. You can read about it here in Python course
19th May 2017, 7:44 PM
Dinmukhamed Mailibay
Dinmukhamed Mailibay - avatar
+ 1
cycle lets you iterate through an iterable() infinitely. I have created this sample program that illustrates it: from itertools import cycle test_list = [11,22,33,44,55] test_string = "This is a test string" j = 0 k = 0 for i in cycle(test_list): j += 1 if(j > 10): break print(i) for i in cycle(test_string): k += 1 if(k> 42): break print(i) now if you run the sample code in an IDE you can see the output is dependent on the value of the integer variables j & k. These variable helps the sample code to terminate in finite steps.
12th Jun 2017, 9:20 AM
Sourabh Kr Mahata