[1] Python 3 簡介與安裝


Posted by bessyhuang on 2023-04-29

Python 簡介

Python 設計者:Guido van Rossum

  • 起源
    • 1989 年 12 月,在家打發聖誕節前後的時間。(hobby project)
    • 一個 Monty Python's Flying Circus 的狂熱愛好者。

Python 版本

  • Python 2.7
    • 現在已不再維護、更新。(於 2020 年停止維護)
    • Latest Python 2 Release: Python 2.7.18
        # Python 2.x
        print "Hello, World"
      
  • Python 3.x
    • 當前最新版本為 Python 3.11.3
    • Cleaner and faster!
        # Python 3.x
        print("Hello, World")
      
  • Python 版本之間的比較
    1. 兩版本 不相容
    2. 一些語法上的差異。

Python 特色

針對程式語言本身

  • 程式執行上,Python 比 JavaScript, C, C#, C++, Java, Go … 慢
  • 高階程式語言 (語法簡潔易懂)

    • print

        # Python
        print("Hi ~")
      
        // C
        #include <stdio.h>
      
        int main()
        {
            printf("Hi ~");
            return 0;
        }
      
    • 閏年判斷

        # Python
        year = int(input('請輸入任意數字:'))
      
        if ((year % 100 != 0) and (year % 4 == 0) or (year % 400 == 0)):
            print("{} is leap year.\n".format(year))
        else:
            print("{} is not leap year.\n".format(year))
      
        // C
        #include <stdio.h>
      
        int main()
        {
            int year;
            scanf("%d", &year);
      
            if ((year % 100 != 0) && (year % 4 == 0) || (year % 400 == 0)){
                printf("%d is leap year.\n", year);
            } else {
                printf("%d is not leap year.\n", year);
            }
      
            return 0;
        }
      
  • 直譯 語言 (Interpreted language)
    • 執行時,會 一行一行的 將程式碼編譯成機器碼 並執行
    • 直譯語言多半以動態語言 (dynamic language) 為主,具有靈活的型別處理,動態生成與程式彈性,但速度會比編譯式語言要慢一些。
    • e.g. Python, JavaScript, Ruby, Perl, PHP, R
  • 動態 型別
    • 簡單來說,就是關於「變數和型別的綁定方法」。
    • 不需要 宣告變數的型別,且型別可隨時變化
      也就是說,不需要給變數顯式的指明其資料型別,該語言會在第一次賦值的時候,將內部的資料型別記錄下來。
    • 執行期間 才去做資料型別的檢查。
        # Python
        var = 1
        var = '2'
      
  • 型別
    • 簡單來說,就是關於「編譯器或直譯器對型別檢查的寬容/嚴格程度」。
      或更淺白地形容:允許編譯器或直譯器自作主張的程度。
    • 不可以 將數字與字串相加。
        # Python
        a = 123 + '123'
        # TypeError: unsupported operand type(s) for +: 'int' and 'str'
      
型別 Weak Strong
Dynamic Perl, PHP, JavaScript Python, Ruby
Static C, C++ C#, Java

針對程式語言的廣泛性 (通用性)、應用性、實用性

  • 熱門程式語言排行榜 TIOBE Index
    • Python: 2022 & 2023 Top 1
  • 免費、開放原始碼、活耀的社群、完整的線上文件
  • 內建函式庫 & 第三方函式庫
    • 內建:標準函式庫 (Standard Library)
    • 第三方:Packages
      被設計為各種應用領域服務的程式語言。
      • Data Science: numpy, pandas
      • Machine Learning: scikit-learn, tensorflow, keras
      • Visualization: matplotlib, seaborn
      • Interacting with database: pymongo, pymysql
      • Web Framework: flask, django
      • ...
  • 物件導向程式設計(Object-oriented programming, OOP)

Python 安裝

Windows

  1. Python 官網下載 Stable Releases (Python 3.x)
  2. 下載執行檔 Windows installer (64-bit)
  3. 點擊執行檔,並勾選 Install launcher for all usersAdd python.exe to PATH
     💡 強烈建議加入 PATH (環境變數)
     原因:這樣才能使用 CMD (命令提示字元) 直接執行 Python Script。
    
  4. 點擊 Install Now 進行安裝

💡 此方式安裝的 Python 已經順便安裝了 pip (package installer for Python) 套件管理工具。

Linux

事實上,作業系統內已經自帶 Python 2.x 和 Python 3.x,只是版本較舊。

  • 使用 which python 可在 CMD 查找 Python 2.x 在系統的安裝路徑
  • 使用 which python3 可在 CMD 查找 Python 3.x 在系統的安裝路徑

如欲安裝最新版本的 Python,詳細安裝過程請參考 References [2]。

💡 在 Linux 環境安裝 Python 3.x 的套件時,必須打 pip3 install [package_name]。

MacOS

  • MacOS 自 12.3 版本以後,不再預設安裝 Python 2.7。
  • 在 MacOS 上安裝 Python 有兩種方法,其一使用「官方提供的執行檔」,其二使用「Homebrew 套件管理工具」,詳細安裝過程請參考 References [2]。

References

  1. 在Windows底下最適當安裝Python環境的方法
  2. Python 3 Installation & Setup Guide
  3. PHP vs Python:2022 最佳網頁應用開發該選誰?

#Python #Intro #Install







Related Posts

進階 React Component Patterns 筆記(下)

進階 React Component Patterns 筆記(下)

DAY38:Duplicate Encoder

DAY38:Duplicate Encoder

JS30 Day 19 筆記

JS30 Day 19 筆記


Comments