Python implements the Fibonacci recursive function.

Source: Internet
Author: User

Python implements the Fibonacci recursive function.

This article uses a simple example to describe how python implements the Fibonacci series recursive function. The code is concise and easy to understand. Share it with you for your reference.

The main function code is as follows:

Def fab (n): if n = 1: return 1 if n = 0: return 0 else: result = int (fab (n-1 )) + int (fab (n-2) return result

The test code is as follows:

For I in range (10): print fab (I)

I hope this article will help you learn Python programming.


Python problems. For recursive functions that run recursively, there is return in the function. The function I wrote is as follows:

Why not return? The last return value is the number of different characters.

Recursive problems in python

Pthon has two procedures in recursion. First, you input X, for example, X = 5, X = 5 without an initial value. You can only obtain x = 4 because age (5) = age (4) + 2, x = 4 is not initialized, and so on until x = 1, x = 1 has an initial value age (1) = 10, so age (2) can be calculated, age (2) has a value, continue to calculate age (3) until age (5), there are two processes to go to the bottom layer and then return to the top layer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.