Python recursive functions and anonymous functions

Source: Internet
Author: User

    • Recursive functions

Python has a limit on the depth of recursion, which will cause an error. So be sure to pay attention to jumping out of conditions.

#Fibonacci Sequence#a series, the first number is 1, the second number is also 1, starting with the third number, each number is the sum of the first two numbers#formula: F (1) =1, f (2) = 1, f (3) = f (1) + F (2), ..., f (n) = f (n-2) + f (n-1)#For example: 1, 2, 3, 5, 8, ...deffib (n):ifn = = 1:        return1elifn = = 2:        return1Else:        returnFIB (n-2) + fib (n-1)Print(FIB (6))
    • anonymous functions
# an anonymous function that calculates n of the n-th square Lambda n:n**nprint# output is 4print# output result is

Syntax: function name = lambda parameter 1, parameter 2, Parameter 3: return value
Attention:

1. Functions can have more than one argument, separated by commas

2. Anonymous functions no matter how complex, can only write one line , and the logic is finished directly return data

3. The return value, like a normal function, can be any data type




Python recursive functions and anonymous functions

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.