What are the advantages and disadvantages of while / for / and recursive loops? When do I use each?
def countdown_while(n):
while n > 0:
print(n)
n -= 1
print('Blastoff!')
countdown_while(3)
def countdown_for(n):
for count in reversed(range(1, n+1)):
print(count)
print('Blastoff!')
countdown_for(3)
def countdown_recursive(n):
if n > 0:
print(n)
countdown_recursive(n-1)
else:
print('Blastoff!')
countdown_recursive(3)
3 loops, 3 different ways of doing it. Are any of them more pythonic than the others? Which one is the cheapest? When would I use one over the others?
Posted Feb 21, 2016, 10:07 PM UTC
710