TKU STAT × Python Basic

Introduction


Kristen Chan

Goals


  • 資料結構 (Data structure)
  • 資料處理 (Data processing)
  • 資料探索 (Exploratory Data Analysis)
  • 資料視覺化 (Data visualization)

Introduction


  • Tools
    • Python3
    • Anaconda
    • Jupyter Notebook
  • 認識 Python 的第一步
    • The Zen of Python
    • Python 特性
    • Python for Data Science
  • 第一支 Python 程式
  • 補充
    • Python 整合開發環境 (IDE)
    • Anaconda Navigator

Tools

Tools


  • Python3
  • Anaconda
  • Jupyter Notebook

Tools -- Python3


Why Python

  • 簡單易學
  • 開源免費
  • 跨平台 (Linux、Windows、Mac)
  • 豐富的函式庫
  • 語法簡潔易懂
  • 整齊、可讀性
  • 應用廣泛

Tools -- Python3


Python Roadmap

  • [基礎學習]:物件型態、判斷式、迴圈、函式、類別、模組、檔案IO、例外處理
  • [進階技巧]:Effective Python
  • [各種應用]

Tools -- Python3


Applications for Python

  • [網路爬蟲]:urllib、requests、lxml、beautiful_soup、scrapy、selenium
  • [網路分析]:scapy
  • [資料庫串接]:sqlite3 (sqlite)、MySQLdb (MySQL)、pymssql (MSSQL)、Psycopg (PostgreSQL)
  • [統計應用]:pandas、numpy、scipy、matplotlib
  • [機器學習]:scikit-learn、TensorFlow
  • [自然語言]:NLTK、jieba
  • [影像處理]:PIL、opencv
  • [網站架設]:Django、Flask
  • [GUI設計]:tkinter、PyQt
  • [軟硬整合]:raspberry pi (樹莓派)、Arduino
  • [遊戲開發]:pygame
  • [App開發]:kivy
  • [各種服務的API串接]:Bot

Tools -- Anaconda


Why Anaconda

  • 開源免費
  • 跨平台 (Linux、Windows、Mac)
  • 不需配置 Python 環境
  • 安裝套件很簡單
  • 套件管理系統 (conda)

Tools -- Anaconda


anaconda

Reference:https://www.anaconda.com

Tools -- Anaconda


安裝步驟--Advanced Installation Optons

  • Add Anaconda to the system PATH environment variable
    • 將安裝目錄加到環境變數中,但由於有時候一台電腦中常同時有多個版本,所以不建議選此選項
  • Register anaconda as the system Python 3.6
    • 讓其他 IDE 能夠檢測到 Python 3.6 並設定默認 Python 3.6

anaconda

Tools -- Jupyter Notebook


Why Jupyter Notebook

  • 結合編輯環境、編譯器、Terminal、檔案總管、工作管理員於網頁介面
  • 互動式計算介面
  • 支援多種程式語言
  • 支援 markdown

Tools -- Jupyter Notebook


Jupyter Online

jupyter online

source:http://jupyter.org/

Tools -- Jupyter Notebook


Azure Notebooks

目前只要有 Microsoft 帳號,就可以免費使用,允許自己安裝套件

Azure Notebooks

Reference:https://notebooks.azure.com

Tools -- Jupyter Notebook


Windows Open your Jupyter Notebook

Anaconda Prompt

Open Jupyter Notebook(Windows)

Tools -- Jupyter Notebook


Mac Open your Jupyter Notebook

Open Terminal ( 終端機 )

Open Jupyter Notebook(Mac)

Tools -- Jupyter Notebook


Jupyter 介面介紹

[Files] → [New]

  • Text Files
  • Folder
  • Terminal
  • Notebooks

Jupyter Notebook

Tools -- Jupyter Notebook


建立一個新的 Python3 Notebook

[New] → [Python3]

Jupyter Notebook

Tools -- Jupyter Notebook


建立一個新的 Python3 Notebook


Jupyter Notebook

Tools -- Jupyter Notebook


Jupyter 介面介紹

[Files] → [Select one File]

  • Duplicate
  • Rename
  • Move
  • Delete

Select one File

Tools -- Jupyter Notebook


Jupyter 介面介紹

[Running]

  • 執行中程式管理
  • 可以把沒用到的Notebook、Terminal關閉

Jupyter Notebook

Tools -- Jupyter Notebook


How to use Jupyter Notebook (常用快捷鍵)

  • 執行
    • 執行此 cell 內容:Ctrl + Enter
    • 執行此 cell 內容:Shift + Enter
    • 執行此 cell 內容:Alt + Enter , Option + Enter
  • 儲存:Ctrl + S , Command + S
  • 編輯模式(綠色)
    • 修改 cell 內容
    • 啟動編輯:Enter
  • 指令模式(藍色)
    • 使用快捷鍵執行指令
    • 啟動指令:Esc

NOTE Help -> Keyboard Shortcuts

Tools -- Jupyter Notebook


How to use Jupyter Notebook (常用快捷鍵)

  • 快速鍵(無大小寫之分)
    • 上方新增一個 cell:A
    • 下方新增一個 cell:B
    • 刪除一個 cell:DD
    • 複製一個 cell:C
    • 貼上一個 cell:V
    • 啟動 Code 模式:Y ( 程式碼撰寫及執行程式 )
    • 啟動 Markdown 模式:M ( 可以做筆記; Markdown語法介紹)

NOTE Help -> Keyboard Shortcuts

Tools -- Jupyter Notebook


How to use Jupyter Notebook (幫助提示鍵)

  • 變數詳細說明 : ?
    questionmark

Tools -- Jupyter Notebook


How to use Jupyter Notebook (幫助提示鍵)

  • 變數提示 : Shift + Tab
    Shift_Tab

Tools -- Jupyter Notebook


How to use Jupyter Notebook (幫助提示鍵)

  • attributes、methods 提示 : Tab
    Tab

Tools -- Jupyter Notebook


How to use Jupyter Notebook (其他指令)

  • 使用系統指令 : !
In [1]:
!pwd
/Users/sun/Desktop/TKUXPython
In [2]:
!conda list | grep "numpy\|pandas\|matplotlib" #Mac
#!conda list | findstr "numpy pandas matplotlib" #Windows
matplotlib                2.0.2               np112py36_0  
numpy                     1.12.1                   py36_0  
numpydoc                  0.6.0                    py36_0  
pandas                    0.20.1              np112py36_0  

NOTE 確認各套件版本(利用 command line 指令)

認識 Python 的第一步

認識 Python 的第一步


  • The Zen of Python
  • Python 特性
  • Python for Data Science

認識 Python 的第一步 -- The Zen of Python


In [3]:
import this
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 的第一步 -- Python 特性


  • 物件導向 (Object-oriented,OO)
  • 直譯式
  • Python 擁有大量第三方程式庫

認識 Python 的第一步 -- Python for Data Science


  • 數學矩陣運算 : Numpy
  • 科學計算、統計分析 : Scipy、StatsModels
  • 資料視覺化 : Matplotlib
  • 資料表分析和視覺化 : Pandas
  • 機器學習(Machine Learning) : Scikit-Learn、TensorFlow
  • 深度學習(Deep Learning) : Keras、TensorFlow
  • Other : 爬蟲、自然語言處理...

第一支Python 程式

第一支Python 程式

Note

print( ) : 使用 print 把物件輸出

第一支Python 程式


輸入程式碼

In [4]:
print ("Hello Python!!")
Hello Python!!

Python 註解

In [5]:
print('Hello Python !!') #我的第一個python程式
Hello Python !!

補充


補充


  • Python 整合開發環境(IDE)
  • Anaconda Navigator

補充 -- Python 整合開發環境(IDE)


Python IDLE

IDLE

NOTE Integrated Development Environment (IDE)

補充 -- Python 整合開發環境(IDE)


Python Spyder

spyder

NOTE Integrated Development Environment (IDE)

補充 -- Python 整合開發環境(IDE)


PyCharm

pycharm

NOTE Integrated Development Environment (IDE)

補充 -- Anaconda Navigator


Windows Anaconda Navigator

anaconda navigator

補充 -- Anaconda Navigator


MAC Anaconda Navigator

anaconda navigator

補充 -- Anaconda Navigator


anaconda navigator

NOTE 點選 Launch 也可以將 Jupyter, Spyder 打開

補充 -- Anaconda Navigator


維護 Python 安裝的相關套件

anaconda navigator