python練習 函數2

來源:互聯網
上載者:User

標籤:.com   path   pre   instance   其他   www   元素   data   判斷   

習題:

  1. 定義一個方法get_num(num),num參數是清單類型,判斷列表裡面的元素為數字類型。其他類型則報錯,並且返回一個偶數列表:(註:列表裡面的元素為偶數)。

    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. 定義一個方法get_page(url),url參數是需要擷取網頁內容的網址,返回網頁的內容。提示(可以瞭解python的urllib模組)。

    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. 定義一個方法 func,該func引入任意多的列表參數,返回所有列表中最大的那個元素。

    def func(*args):    max_list = [max(x) for x in args]    return max_listprint(func([1,2,3],[4,5,6]))
  4. 定義一個方法get_dir(f),f參數為任意一個磁碟路徑,該函數返迴路徑下的所有檔案夾組成的列表,如果沒有檔案夾則返回"Not dir"。

    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練習 函數2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.