#!/usr/bin/python3 # -*- coding: utf-8 -*- """ Countdown """ # %% Countdown loop def countdown_for_loop(i: int) -> None: """ countdown from i using iteration """ for counter in range(i, 0, -1): print(counter, " ") countdown_for_loop(5) # %% Countdown tail def countdown_tail(i: int) -> None: """ countdown from i using recursion """ def loop(counter: int) -> None: if 0 < counter: print(counter, " ") loop(counter - 1) loop(i) countdown_tail(5) # %% Countdown loop def countdown_while_loop(i: int) -> None: """ countdown from i using iteration """ while 0 < i: print(i, " ") i -= 1 countdown_while_loop(5) # %% Countdown tail def countdown_tail_natural(i: int) -> None: if i > 0: print(i, " ") countdown_tail_natural(i - 1) countdown_tail_natural(5) """ Concluding remarks: Unfortunately, Python is not a good language in this regard, but the language Scheme is. When the value of a recursive call is immediately returned (i.e., not combined with some other value), a function is said to be tail recursive. The Scheme programming language optimizes tail recursive functions so that they are just as efficient as loops both in time and in space utilization. """