iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 11
0
自我挑戰組

大數據概念系列 第 11

Day11 | Python模組

一、Imports
1.Generic Imports
https://scontent.ftpe7-1.fna.fbcdn.net/v/t1.0-9/44618628_2083344538384084_4845692479711739904_n.jpg?_nc_cat=106&_nc_eui2=AeHTnIcz62oV3KJOVlSAZTjoP_2Wv9XG2INgDyuNKSo54uNz1YZCaKfKKDpoELZAg0Ul2XPJVvwWvTm203EmCRXRhLT4eOFGeTE-f4YGgwO86g&_nc_ht=scontent.ftpe7-1.fna&oh=df8f72927c92a208ce3845803600e501&oe=5C4631F2
執行結果是 NameError: name 'sqrt' is not defined.
因為Python並不知道square(平方根)是什麼,所以module(模組)就很重要了
Module包含了變數和函式,而sqrt()是其中一個函式。如果要執行,需要import,稱為generic import

因此須將程式碼改成:
https://scontent.ftpe7-1.fna.fbcdn.net/v/t1.0-9/44478910_2083344548384083_5780113402944815104_n.jpg?_nc_cat=110&_nc_eui2=AeElmU4N_aE4qckShqCclKo86trSNWjPGOucLSTmlZub8fC_4TTiqqWnwFFFqoye_OkYuBTF4UFH0-XJgxMDwzMQqtQHtGbNtZ6ElVQjClwFYw&_nc_ht=scontent.ftpe7-1.fna&oh=f8d9cb39ccb6dc4fa710403f13a9b432&oe=5C5363E4

2.Function Imports
只需sqrt函數時程式碼應該為下:
https://scontent.ftpe7-3.fna.fbcdn.net/v/t1.0-9/44435768_2083344541717417_7265001811851870208_n.jpg?_nc_cat=103&_nc_eui2=AeFlYklvjTW2x1qNeqcTNLpM4bxCtavFfUU3Rkz5WbiEl_R8E8AqVP_87xiUSEY3H4q2vm8rXBuIlRq3WICwHZcdwNLPYh96jSJTV0DxEg50mw&_nc_ht=scontent.ftpe7-3.fna&oh=ea0af2d1774f93f45d86fce381a1a301&oe=5C4DCCB8

3.Universal Imports
Import math模組的每個函式
https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-9/44404763_2083344568384081_2296691554892382208_n.jpg?_nc_cat=104&_nc_eui2=AeHbGIxVK1j3AYy23xqhZ46JsmhZMtLIYwSo6uf32aLN0tjfjVHu7s9_fifT6cZkBqItw1paz4UPowIyASFpTxpTYJxlGcUCBdMoA-KMcZUUxA&_nc_ht=scontent.ftpe7-2.fna&oh=8238f371682ee2c624447061f7913377&oe=5C4F7FAB
雖然Universal imports看起來很方便,但實際上會產生一些問題,可能會和自己設定的函數產生衝突
因此最好 module.name

二、Pythoon裡的函式(built-in function)
1.max()
https://scontent.ftpe7-4.fna.fbcdn.net/v/t1.0-9/44452973_2083344575050747_7530536219306885120_n.jpg?_nc_cat=101&_nc_eui2=AeG0Pdz1CR2Uggs3GfcfSulZqA2EkkykUzPXwU2jSKeLpkUngr7QSFj0Y4VN8Is47Fzk7P-VRw9U7MpQpsRpQrCYHue-L1-I7FhcnbwTNI8yAg&_nc_ht=scontent.ftpe7-4.fna&oh=aaf4f7ea71edf605a42bf462e5677524&oe=5C4C3CF8

2.min()
https://scontent.ftpe7-3.fna.fbcdn.net/v/t1.0-9/44552769_2083344585050746_1405100440672010240_n.jpg?_nc_cat=103&_nc_eui2=AeHbW3Du5HBiqmVttTovmJGQs5YnvfFbNe_HCKL7ccuReMvokKfqQXKqT80m9d_Td3t9UHHkHyZmOvIuYJ4pUBPPPpkFU4kEZTQYZjBu7zIANw&_nc_ht=scontent.ftpe7-3.fna&oh=f07fe3a17b4ffd749ce14ae74e50cee5&oe=5C55F858

3.abs() 絕對值
https://scontent.ftpe7-1.fna.fbcdn.net/v/t1.0-9/44546767_2083344631717408_4196132964578361344_n.jpg?_nc_cat=100&_nc_eui2=AeGur5AjAudINEjZj1rMVsWlPbh6eKbyHPQv47Xn3Sg8fPXIGoJFucR68MuImDI659QnkM20d9MsF3o-Rg4e8_gmbJWnSVUca-p8sDNHNwZwKQ&_nc_ht=scontent.ftpe7-1.fna&oh=cf069788d37514366a391f06cb0d42f9&oe=5C84E863

4.type()
https://scontent.ftpe7-1.fna.fbcdn.net/v/t1.0-9/44500766_2083344645050740_1337646647848468480_n.jpg?_nc_cat=110&_nc_eui2=AeF_Yq0A8GrX_HvYxpyP37a1v-FljnC-GlfBmGo9zHEJuxV0KzTDmT-7XREZeb-nV4FfbUVqZ1sTDiUCp5Zh1iljHhvzA73Nec7xqqZvBUJl5g&_nc_ht=scontent.ftpe7-1.fna&oh=fbd2415cc31e82386b28d705f785cfe7&oe=5C458ADD


上一篇
Day10 | Python函式
下一篇
Day12 | Python串列
系列文
大數據概念30

尚未有邦友留言

立即登入留言