Exercises:
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
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"))
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]))
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