Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is recursion in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what is recursion in Python", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "what is recursion in Python" this article.

Concept: a function that calls itself directly or indirectly requires a boundary condition for recursion. Recursive forward segment. Recursive return segment recursion must have boundary conditions (otherwise there will be infinite recursive advance) when the boundary conditions are not satisfied, recursive forward when the boundary conditions are satisfied, recursive recursive requirements: recursion must have exit conditions, recursive calls must be executed to this exit condition. A recursive call without exit condition means that the depth of an infinite call should not be too deep. Python limits the depth of a recursive call to protect the parser from exceeding the recursive depth limit, throwing RecursionError:maxinum recursion depth exceeded beyond the maximum depth, and sys.getrecursionlimit () to view the maximum depth boundary value: the return value that does not terminate the recursion. Instead, it is the value called as the last operation of a recursive operation. When the boundary is triggered, it means that the boundary value is called into the recursive operation, and the result pops up, thus interrupting the recursion. 1. The loop is a little more complex, but as long as it is not a dead loop, it can be iterated many times until the result 2.fib function code is very easy to understand, but only the outermost function call can be obtained, and the internal recursive results are all intermediate results. And a given n has to do nearly 2n times of recursion, the deeper the depth, the lower the efficiency. In order to obtain the Fibonacci series, we need to set a cycle for n times, which is even less efficient. Recursion also has a depth limit. If the recursion is complex and the function repeatedly presses the stack, the stack memory will soon overflow 1. 5%. Indirect recursion calls the function itself 2. 5 through other functions. However, if it constitutes a circular recursive call, it is very dangerous, but often in the case of code, such a call can still occur. Use code specifications to avoid such recursive calls. This is all of the article "what is Recursion in Python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report