Acomputer program consists of line-by-line instructions. The computer performs those instructions line-by-line. However, some instructions may be repetitive with a common pattern. Recursion or iteration helps one to write a few lines of codes to perform such repetitive tasks. Suppose a Python list with five-string elements. We wish to print the elements one in a line. This operation needs five lines of codes.

 flowers = ['lily', 'tulip', 'rose', 'lavender', 'dandelion'] 
 print(flowers[0])
 print(flowers[1])
 print(flowers[2])
 print(flowers[3])
 print(flowers[4]) 

Output:

It can be observed that the five lines of codes follow the same pattern. The only difference in each line is the index of the list elements. What if this list contains 100 or 1000 elements? Coding will become a tedious task. These kinds of problems are resolved through either iteration or recursion. Here, the iterative form of the above codes is as follows.

 for flower in flowers:
   print(flower) 

Output:

iteration

These two lines are sufficient to achieve the task even if the list contains a million elements!

#developers corner #backtracking #dynamic programming #factorial #iteration #iterative #python #python programming #quicksort #recursion #recursive #sorting

Recursion And Iteration In Python Programming
1.40 GEEK