⬆️ ⬇️

Infinite loops

As you know, the easiest way to get a range of numbers in Python is to use range (). range returns a list, but when you need to go through a large range it is better to use xrange (), which instead of a list, returns an iterator. But how to be when the end of the range is not known, and using while True does not seem aesthetic.

Very simple:

from itertools import count

for i in count (1): DoSomeWith (i)



Of course, as soon as we get the result, we can safely make a break.


')

Source: https://habr.com/ru/post/23807/



All Articles