When to use what type of repetition in a program (Python3)

--Originally published at Elu's Blog

We have three repetitive functions in Python3:

  1. For loops. In Python3 ‘for loops’ are best used when you want to edit or analyze a string, list, tuple or dictionary which number of things in it is defined. In other languages like C, ‘for loops’ and ‘while loops’ are used by preference of the programmer, they are interchangeable.
  2. While loops. They are used when the times of the repetition vary.
  3. Recursion. They are used when the solution to a problem is defined by the solution to smaller instances of the same problem.

As easy as that, you want to analyze a list? Use a for loop. Want to print as many ‘Hello World”s as the user wants? Use a while loop. Want to get a fibonacci number in a series? Use a recursive function.

Here are links to explanation of every repetitive function:

For loops: https://elusblog.wordpress.com/2017/02/14/wsq04-for-loops-python3/

While loops: https://elusblog.wordpress.com/2017/02/14/wsq06-while-loops-python3/

Recursive functions: https://elusblog.wordpress.com/2017/04/20/recursion-python3/