Python Practice function 2

Source: Internet
Author: User

Exercises:

  1. Define a method Get_num (NUM), the num parameter is a list type, and the element in the list is a numeric type. The other type is an error, and returns an even list: (Note: The elements in the lists are even).

    def get_num(num):    t_list = []    for x in num:        if not isinstance(x, int):            return "type error"        elif x%2 == 0:            t_list.append(x)    return t_list
  2. Define a method Get_page (URL), url parameter is the URL that needs to get the content of the webpage, return the content of the webpage. Hint (you can learn about Python's Urllib module).

    from urllib import requestdef get_page(url):    with request.urlopen(url) as f:        data = f.read()    return dataprint(get_page("http://www.baidu.com"))
  3. Defines a method, Func, that introduces any number of list parameters, returning the largest element in any list.

    def func(*args):    max_list = [max(x) for x in args]    return max_listprint(func([1,2,3],[4,5,6]))
  4. Define a method Get_dir (f), the F parameter is any disk path, the function returns a list of all folders under the path, and returns "not dir" if no folder.

    import globimport osdef get_dir(f):    if os.path.exists(f):        file = glob.glob(r"%s*" % f)        return file    else:        return "Not dir"print(get_dir("D://"))

Python Practice function 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.