tags: python

Python入門

建立python開發環境影片:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


簡介

  • 由荷蘭程式設計師Guido van Rossum於1990年創造出來。
  • 美國大學學術常用程式語言
  • 全球10大熱門程式語言
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

特色

  • 語法精鍊
    • 易學易寫
    • 不易岀錯
    • 容易閱讀
  • 廣泛的函數庫
  • 開放原始碼
  • 跨平台
  • 使用人口眾多
  • 可使用其他語言函數庫,提供更強火力
    • Java
    • .Net Framework
    • C/C#
  • 動態型別
  • 直譯式語言:不用編譯

設計哲學

  • 優美
  • 明確
  • 簡單

The Zen of Python, by Tim Peters

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Flat is better than nested.
  • Sparse is better than dense.
  • Readability counts.
  • Special cases aren't special enough to break the rules.
  • Although practicality beats purity.
  • Errors should never pass silently.
  • Unless explicitly silenced.
  • In the face of ambiguity, refuse the temptation to guess.
  • There should be one and preferably only one obvious way to do it.
  • Although that way may not be obvious at first unless you're Dutch.
  • Now is better than never.
  • Although never is often better than right now.
  • If the implementation is hard to explain, it's a bad idea.
  • If the implementation is easy to explain, it may be a good idea.
  • Namespaces are one honking great idea let's do more of those!

Python之禪(參考資料1)

  • 美比醜好
  • 直接比間接好
  • 簡單比複雜好
  • 複雜比難懂好。
  • 平鋪比套疊好。
  • 稀比密好。
  • 可讀性重要。
  • 特殊案例不足以破壞規矩。
  • 但實務重於純粹。
  • 別讓錯誤悄悄逃走。
  • 除非表明要安靜。
  • 面對含糊時堅拒猜測。
  • 應該有一個-最好只有一個-明顯的做法。
  • 然而這個做法一開始並不明顯,除非你是那個荷蘭佬。
  • 現在開始總比不作為好。
  • 但不作為通常比 馬上做 好。
  • 難以解釋的實作不好。
  • 容易解釋的實作也許不錯。
  • 命名空間是個好主意-多來幾個吧!

版本(2.X 3.x 語法略有不同)

  • 2.7.x(2017/7停止開發、維護)
  • 3.6.x

應用領域

  • 網站
  • 軟體(圖形界面)
  • 遊戲
  • 自動化
  • 網頁爬蟲
  • 資料探勘/資料處理
  • 統計分析(可單獨處理,或與R語言結合)
  • 機器學習/人工智慧
  • 區塊鍊

python開發環境

  • 程式語言:python
  • 編輯器:visual studio code(VS code)
  • 版本管理:git (非必要,但強烈建議)

快速安裝!!(教室請執行此檔案,節省時間)

  1. 連到 bit.ly/python3env 網站
  2. 點「下載」
  3. 執行剛剛下載的檔案(會自動下載、安裝python3, vscode)

安裝python

  1. 打開瀏覽器,連到 python.org
  2. 點Downloads/3.6.2
  3. 儲存並執行
  4. 打勾Add Python 3.6 to PATH
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

檢查是否安裝、設定成功(可略過)

  1. 開始/cmd
  2. 執行python
  3. print("Hello, World")
  4. 5+3
  5. exit()

也可透過IDLE進入直譯模式
開始/所有程式/Python 3.6/IDLE(Python 3.6)

補充說明

直譯模式: 輸入一行指令,enter之後就執行一行指令,只是用來練習python語法使用

腳本模式(scritp): 真正應用情境。也就是利用任何一種文書編輯軟體(例如記事本, VS Code, )來撰寫很多行指令,形成一個有意義、有目的邏輯,並存成.py的檔案,然後在視窗環境或是命令列(command line: cmd)環境執行該python檔案


簡易範例1:顯示資料到螢幕上及四則運算

print(5+2) # 加 print(5-2) # 減 print(5*2) # 乘 print(5/2) # 除

簡易範例2:變數(被命名的箱子,可以裝資料)

num1 = 3 # 將變數num1放入數值3 num2 = 5 # 將變數num2放入數值5 sum = num1 + num2 # 把變數num1的內容加上變數num2的內容放到變數sum中 print(sum) # 印出變數sum的內容

規範

  • 文字有分大小寫,預設為小寫
  • 善用空白(增加易讀性)
  • 一行過長可用\ 換行繼續
  • 縮排(空白、tab擇一使用,建議空白)
  • 註解 #
  • 多行註解( ''' ''')

關鍵字(請勿使用在變數或函數上)

- - - -
and as assert break
class continue def del
elif else except False
finally for from global
lambda None nonlocal not
if import in is
or pass raise return
True try with while
yield

參考資料

問題與解決

Q: 若執行python出現「api-ms-win-crt-runtime-l1-1-0-dll 遺失」問題可以下載微軟以下檔案

Visual C++ 可轉散發套件

32位元電腦請下載「vc_redist.x86.exe」
64位元電腦請下載「vc_redist.x64.exe」