First, recursion
Inside the function, other functions can be called;
If a function calls itself internally, that function is a recursive function.
Case: Traverse all files in the current directory
1. Recursive traversal
1 ImportOS2 defGCI (filepath):3 #traverse all files under FilePath, including subdirectories4Files =Os.listdir (filepath)5 forFiinchFiles:6Fi_d =Os.path.join (FILEPATH,FI)7 ifOs.path.isdir (fi_d):8 GCI (fi_d)9 Else:Ten Print(Os.path.join (filepath,fi_d)) One A #recursively traverse all files in the current directory -Gci"./")
2. Other methods
1 Import OS 2 for in Os.walk ('./'):3for in FS: 4 Print (Os.path.join (FILEPATH,F))
Second, Fibonacci sequence
Fibonacci numbers refer to such a sequence of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368
In particular, the No. 0 item is 0, and the 1th item is the first 1. This sequence starts with the 2nd item and each item is equal to the sum of the first two items.
1 the first is to realize the series of the wave by recursion.2 deffunc (arg1,arg2,stop):3Arg3 = Arg1 +arg24 Print(Arg3,"', end="')5 ifArg3 <Stop:6 func (arg2,arg3,stop)7Func (0,1,46368)8 9 the second, implemented through the while LoopTen deffunc (n): OneA, B = 0,1 A whileb <N: -A, B = b,a+b - Print(b,"', end="') theFunc (46368)
Recursion and Fibonacci sequence