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))
# 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