I was experimenting with various ways of creating an infinite loop in Python (other than the usual while True), and came up with this idea:
x = {0: None}
for i in x:
    del x[i]
    x[i+1] = None  # Value doesn't matter, so I set it to None
    print(i)
On paper, I traced out the way this would infinitely loop:
- I loop through the key's value in the dictionary
- I delete that entry.
- The current counter position in the loop + 1will be the new key with valueNonewhich updates the dictionary.
- I output the current counter.
This, in my head, should output the natural numbers in a sort of infinite loop fashion:
0
1
2
3
4
5
.
.
.
I thought this idea was clever, however when I run it on Python 3.6, it outputs:
0
1
2
3
4
Yes, it somehow stopped after 5 iterations. Clearly, there is no base condition or sentinel value in the code block of the loop, so why is Python only running this code 5 times?
 
     
     
     
    