Python Learning-Review 5 Lessons (December 2)

Source: Internet
Author: User

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)

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.