Task:
Review 5 sessions (December 2)
1.8 Recursively list files in a directory
1.9 Anonymous functions
2.0-2.4 built-in functions
Notes:
Considerations for recursion
Must have the last default result
if n = = 0
Recursive parameters must converge to the default result:
Factorial (n-1)
Recursively list files in a directory
def print_files (path):
Isdir, isfile, join = Os.path.isdir, Os.path.isfile, Os.path.join
Lsdir = Os.listdir (path)
dirs = [I for I in Lsdir if Isdir (Join (path,i))]
Files = [I for I in Lsdir if Isfile (Join (path,i))]
If dirs:
For D in dirs:
Print_files (Join (PATH,D))
If files:
For f in Files:
Print Join (PATH,F)
Print_files (Sys.argv[1])
anonymous functions
Python uses lambda to create anonymous functions.
Lambda is just an expression, and the function body is much simpler than def.
The body of a lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.
The lambda function has its own namespace and cannot access parameters outside its own argument list or in the global namespace.
Although the lambda function appears to be only a single line, it is not equivalent to a C or C + + inline function, which is designed to call small functions without consuming stack memory to increase operational efficiency.
Example:
#!/usr/bin/python
#-*-Coding:utf-8-*-
# Writable Function Description
sum = lambda arg1, arg2:arg1 + arg2;
# Call the SUM function
Print "added value is:", SUM (10, 20)
Print "added value is:", SUM (20, 20)
Built-in functions:
Common functions:
ABS ()
Max ()
Min ()
Len ()
Divmod ()
POW ()
Round ()
Callable ()
Type ()
Isinstance ()
CMP ()
Range ()
Xrange ()
Type conversion function
Int ()
Long ()
Float ()
Complex ()
STR ()
List ()
Tuple ()
Hex ()
Oct ()
Chr ()
Ord ()
Eval ()
String handling functions
Str.capitalize ()
Str.replace ()
Str.split ()
Str.join ()
String module
Sequence processing functions
Len ()
Max ()
Min ()
Sequence processing functions
Filter ()
Zip ()
Map ()
Reduce ()
Python Learning-Review 5 Lessons (December 2)