Iteration vs Recursion in Python

Iteration vs Recursion in Python – in this post I’ll try to make you familiar with two of the most useful and important concepts in python. Plus, will give some differences and additional info to create a margin between them.
I’m assuming that you guys are familiar with Python Basic concepts and functionalities. If not then, I would recommend to explore Python Chapters for basic understanding.
Iteration
Sometimes in dealing with real life problems, we need some repetitive identical tasks. Repeating identical or similar tasks without making errors is something that computers do well but humans do not. Programming languages such as Python, C#, Java etc. gives us freedom to create such repetitive tasks, often called statements.
Repeated execution of a set of statements is called iteration. Because iteration is so common, Python provides several language features to make it easier.
-
Iteration in Natural Languages
Now let’s grasp the core of the Iteration, by seeing how it really looks and works in our natural language.
I think Python is the coolest programming language I have been so far.
Do I really think so?
What if I do like C# too?
Is C# better than python or python is better than C#?
But which one is the best?
What if there is a 3rd language which is better than both?
But do I’ll like that 3rd language too?
What if.. What if this.. What if that.. ??
….
and many more things.. I think now I had given you a glimpse of Iteration and how it works. At least a feel of it..
-
Code Snippet
Code snippet for solving base and exponent.
As we know-
(b)^a = b * b * b ….. (a times)
# Giving a function definition def iterPower(base, exp):
# Some iterative cases if (exp == 0): return 1 elif exp > 0: return (base * iterPower(base, exp-1))
-
Explanation
As we know-
(b)^a = b * b * b ….. (a times)
we can also write it as-
(b)^a = b * (b)^(a-1)
The above statement works as a logic for my code.
Recursion
Sometimes in dealing with real life problems, we need some repetitive identical tasks able to generate several sub cases from a general. Programming languages such as Python, C#, Java etc. gives us freedom to create such repetitive tasks, often called cases.
Recursion is a way of programming or coding a problem, in which a function calls itself one or more times in its body.
-
Recursion in Natural Languages
Now let’s grasp the core of the Recursion, by seeing how it really looks and works in our natural language.
- Python is the coolest programming language..
I think, Python is the coolest programming language..
Python is the coolest programming language I have been so far..
Yes, am sure about it. I think I know python..
They also think I know python and python is the coolest programming language I have been so far..
He doubts that they really think I know python.
But she is sure that I know python.
I know.. They know.. He think.. They doubt.. ..
…
and many more things.. I think now I had given you a glimpse of Recursion and how it works. At least a feel of it..
-
Code Snippet
Code snippet for solving base and exponent.
As we know-
(b)^a = b * b * b ….. (a times)
# Giving a function definition def recurPower(base, exp):
# Base Case if (exp == 0): return 1 # Recurring call elif (exp > 0): return (base * recurPower(base, exp-1))
-
Explanation
As we know-
(b)^a = b * b * b ….. (a times)
we can also write it as-
(b)^a = b * (b)^(a-1)
The above statement works as a logic for my code.
Iteration vs Recursion in Python
I hope now you guys have something in your pocket about Iteration and Recursion. So let’s quickly move forward and explore some basic differences.
- Iteration is repeated execution of a set of statements while Recursion is a way of programming in which function call itself until it reaches some satisfactory condition.
- In Iteration, we generally define some random cases to check verifiability of our assumptions while in Recursion we generate some base cases and a final call and make the function work and call again and again until it reaches the desired result.
Let’s go through another pair of example..
In this snippet, I’ll show how to find GCD / HCF of two numbers in Python using both of the approaches.
-
Iterative Call
def gcdIter(a, b): while (b): a, b = (b, a % b) return a
-
Recursive Call
def gcdRecur(a, b): if (b == 0): return a else: return gcdRecur(b, a % b)
When to Use Which?
- Iteration can be complex sometimes, where we have several possible random cases. In that cases, Recursion can be very beneficial.
- Time complexity of a program generally increases in the case of Recursion in comparison to Iteration.
- Space complexity of a program generally increases in the case of Iteration in comparison to Recursion.
So always keep in mind what you are going to do and what’s your priority.
I hope I made a margin between these two important concepts and you guys had fun going through this. But still if you think I had missed something then don’t forget to update me. Drop your valuable comments below.
Till some next, python stuff Keep Sharing & Tweaking! 💡 💡
I was wondering if you ever thought of changing the structure of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better.