Search code examples
pythoninfinity

Print numbers from 1 to x where x is infinity in python


Possible Duplicate:
Python Infinity - Any caveats?
python unbounded xrange()

I have a question regarding Python where you code a simple script that prints a sequence of numbers from 1 to x where x is infinity. That means, "x" can be any value.

For example, if we were to print a sequence of numbers, it would print numbers from 1 to an "if " statement that says stop at number "10" and the printing process will stop.

In my current code, I'm using a "for" loop like this:

for x in range(0,100):
    print x

I'm trying to figure how can "100" in "range" be replaced with something else that will let the loop keep on printing sequences continuously without specifying a value. Any help would be appreciated. Thanks


Solution

  • With itertools.count:

    import itertools
    for x in itertools.count():
        print x
    

    With a simple while loop:

    x = 0
    while True:
        print x
        x += 1