Python基礎課:一起學習python基礎題

來源:互聯網
上載者:User

標籤:word   color   write   nlog   ret   local   小夥伴   python   join   

python最近老火了,萬能開發語言,很多小夥伴們要麼初學,要麼從別的開發語言轉過來的,如果你能把下面幾道基礎題不費勁的寫出來,基礎應該可以打80分,可以進行進階的學習了,如果下面的題目如果做不出來,拜託不要耽誤時間,趕快打好基礎,勿在浮沙築高台。

題目出給新鳥打基礎的,實現答案的方法千千萬,如果老鳥有更厲害的答案就不要噴了,先謝謝了。

還有新鳥先不要看答案,不要看答案,不要看答案,(重要的事情說三遍)自己先去解,用自己最簡單的想法去實現,能用python內建的方法就不要自己造輪子。

好啦,開始吧!

 

一、通常,文章(論文,文章)的標題,都要求英文首字母大寫,這是一種常見的書寫規範。
    現在,要求實現一個“處理函數”,能滿足如下需求:
    1. 能夠接收任意的字串
    2. 能夠將接收的這個字串的英文首字母轉換成大寫
    3. 能夠返迴轉換後的字串
    例如:接收 ‘this is python.‘,會輸出 ‘This Is Python.‘

 

 1 def capital(*args): 2     ws_list = [] 3     if args: 4         for arg in args: 5             words = arg.split(‘ ‘) 6             w_list = [] 7             for w in words: 8                 w = w.capitalize() 9                 w_list.append(w)10             new_word = ‘ ‘.join(w_list)11             ws_list.append(new_word)12         return ws_list
‘‘‘-------------運行結果-------------->>> capital(‘this is python‘,‘it\‘s my apple‘)[‘This Is Python‘, "It‘s My Apple"]‘‘‘

 

二、有時,我們會需要一連串等間距的數字(這在資料分析和進度控制等方面格外有用),例如:[0, 2, 4 , 6, 8]
    對於上述例子,使用 list(range(0, 10))可以十分方便的完成。
    但是 range 的步長只能是整數,因此,加入我們需要 [0, 0.1, 0.2, ... , 0.9], 那麼 range 就不能滿足我們的需求了。
    因此,寫一個類似 range 的函數,來支援浮點數的步長。
    (我用列表推導產生,當然還有小小bug,新鳥可能閱讀起來費勁,看看你能否找出bug)

 

1 def float_range(start, end, step):2     return [round((x*step),1) for x in range(start,end*10) if round((x*step),1) < end and round((x*step),1) > start]
‘‘‘-------------運行結果-------------->>> float_range(0,1,0.2)[0.0, 0.2, 0.4, 0.6, 0.8]>>> float_range(0,2,0.2)[0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8]>>> float_range(0,2,0.3)[0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]>>> float_range(1,3,0.5)[0.5, 1.0, 1.5, 2.0, 2.5]‘‘‘

 

三、加密與解密是一種最常見的需求。
    最簡單加密方法之一就是,按照某種規則,將現有的字元變換成另一種。例如:a換成b, b換成c ... z換成a ... 。
    按照上述的規則, ‘list‘ 就應該變成了 ‘mjtu‘。
    現在,要求按照上述規則,完成兩個函數:
    1. 加密函數
    2. 解密函數

    一旦你完成了上述的兩種函數,試著定製一個自己的字串。為你定製的字串類添加以上兩種方法。
    提示:“定製” 使用 “繼承” 來實現

 

 1 class father_security: 2     # 父加密類 3     def encrypt(self,s): #加密函數 4         self.s = s 5         return ‘‘.join([chr(ord(x)+1) for x in self.s]) 6  7     def decrypt(self,s): #解密函數 8         self.s = s 9         return ‘‘.join([chr(ord(x)-1) for x in self.s])10 11 12 class child_security(father_security):13     # 子加密類14     def __init__(self, s):15         self.s = s16 17     def child_encrypt(self):  #子加密函數18         return super().encrypt(self.s)19 20     def child_decrypt(self):  #子解密函數21         return super().decrypt(super().encrypt(self.s))
‘‘‘-------------運行結果-------------->>> m = child_security(‘apple‘)>>> m.child_decrypt()‘apple‘>>> m.child_encrypt()‘crrng‘>>> ‘‘‘

 

四、寫一個裝飾器,測試函數的已耗用時間,現在要求大家做如下測試:
    1. 可以測試函數啟動並執行時間
    2. 可以將函數啟動並執行時間輸出到螢幕上
    3. 將已耗用時間,以日誌的形式記錄在指定的檔案中(當作日誌)。

 

 1 import time 2  3 def run_time(func): 4     def new_func(*args, **kwargs): 5         path = r‘d:\test\pythonlog.txt‘ 6         tmp_time = time.time() 7         start_time = time.strftime(‘%x %X‘,time.localtime()) 8         print(‘開始時間:{}‘.format(start_time)) 9         result = func(*args)10         end_time = time.strftime(‘%x %X‘,time.localtime())11         print(‘結束時間:{}‘.format(end_time))12         run_time = round(time.time()-tmp_time,4)13         print(‘已耗用時間:{}‘.format(run_time))14         with open(path,‘a‘, encoding=‘utf8‘) as f:15             f.writelines([‘開始時間:‘+str(start_time)+‘\n‘, ‘結束時間:‘+str(end_time)+‘\n‘, ‘已耗用時間:‘+str(run_time)+‘\n‘])16             f.flush()17         return result18     return new_func19 20 21 @run_time22 def fab(max): 23     n, a, b = 0, 0, 124     L = []25     while n < max:26         L.append(b)27         print(b)28         a, b = b, a+b29         n = n + 130     return L31 32 33 @run_time34 def fab_yield(max):35     n, a, b = 0, 0, 136     while n < max:37         yield b38         # print b39         a, b = b, a + b40         n = n + 141 42 x = 1043 L = [i for i in fab_yield(x)]
‘‘‘-------------運行結果-------------->>> ========================= RESTART: D:/test/ff33.py =========================開始時間:06/30/17 14:43:15結束時間:06/30/17 14:43:15已耗用時間:0.0156>>> ========================= RESTART: D:/test/ff33.py =========================開始時間:06/30/17 14:45:25結束時間:06/30/17 14:45:25已耗用時間:0.0312>>> ‘‘‘

記得去D盤下Test目錄下看看日誌是否寫入

以上是python基礎試題,檢驗一下自己是否真的入門了,如果還沒有趕快加油吧!

祝你早日成功!

 

Python基礎課:一起學習python基礎題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.