Python Exercise: function 1

Source: Internet
Author: User
Tags glob

Exercises:

  1. 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)
  2. 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
  3. 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"))
  4. 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"))
  5. 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

Related Article

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.