Exercises:
Defines a method func, which can introduce any number of integer parameters, and the result returns the largest and smallest values.
def func(**args): return max(args),min(args)
Defines a method, func, that can introduce any number of string arguments, and the result returns the longest string (length).
def func(**args): max = args[0] max = max if len(max)>len(x) else x return max
Defines a method Get_doc (module), which is the module object that is imported or defined in the script, which returns the Help document for module.
Using __import__ to dynamically import modules
def get_doc2(moudle): mod = __import__(moudle) return mod.__doc__print(get_doc2("string"))
Define a method Get_text (f), the F parameter is the disk path of any file, and the function returns the contents of the F file.
def get_text(f): with open(f) as file text = file.read() return textprint(get_text("text.log"))
Defines a method Get_dir (folder), which is any folder, which returns a list of files in the Folders folder. Tips (You can learn about Python's Glob module)
import glob def get_dir(folder): alist = glob.glob(r"%s*" %folder) return alist
Python Exercise: function 1