Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Raspberry Pi 讀書會 #03 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

Raspberry Pi 讀書會 #03 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

第三次 Raspberry Pi 讀書會,讀書內容為 Raspberry Pi Cookbook。本投影片涵蓋第五章(Python 基礎)、第六章(Python Lists and Dictionaries)和第七章(Python 進階)。

https://www.piepie.com.tw/3035/raspberrypi-bookclub-03

台灣樹莓派

December 08, 2014
Tweet

More Decks by 台灣樹莓派

Other Decks in Technology

Transcript

  1. 姓名標示 — 非商業性 — 相同方式分享 CC (Creative Commons) 姓名標示 —

    你必須給予 適當表彰、提供指向本授權 條款的連結,以及 指出(本作品的原始版本)是否已 被變更。你可以任何合理方式為前述表彰,但不得以 任何方式暗示授權人為你或你的使用方式背書。 非商業性 — 你不得將本素材進行商業目的之使 用。 相同方式分享 — 若你重混、轉換本素材,或依本 素材建立新素材,你必須依本素材的授權條款來 散布你的貢獻物。
  2. 3 • 5. Python Basics • 6. Python Lists and

    Dictionaries • 7. Advanced Python 本次領讀章節
  3. 4 • Python 3 from 2008/12 • Python 2.7.2 為最後版本

    • 差異 (3 的優點 ) : • 更強大的函式庫: concurrent, enum, asyncio... • Str 型態內建已 unicode 儲存 • 2014-09 版本已內建 • 為什麼不要用 3( 或缺點 ) ? • 如果已經有大量的程式在 production 上 Python 2 vs. Python 3 更多改變: https://wiki.python.org/moin/Python2orPython3
  4. 5 • 動態型別 (dynamic typing) # 這是註解 i = 3

    # 變數 i 指到數字物件 3 i = [1, 2, 3, 4, 5] # 變數 i 指到串列物件 print( i[2] ) # 印出串列中第三個元素 i = “abcde” # 變數 i 指到字串物件 print( i[2] ) # 印出字串中第三個元素 變數 , 物件 , 型別 , 註解
  5. 6 • 相連 (+) • “abc” + “def” • 數字轉型

    , 字串轉型 • str(123), int(“-123”) • 字串長度 • len(“abcdef”) • 字串擷取 • s = “abcdefghi” • s[1:5], s[ :5], s[3: ] • s[-3: ] 字串用法
  6. 8 # if / elif x = 17 if x>=

    10 and x<= 20: print("x is in the middle") # for loop for i in range(1, 11, 2): print(i) # while loop answer = '' while answer != 'X': answer = input('Enter command: ') 條件判斷 / 迴圈 / while 敘述
  7. 9 • list :類似無型別的陣列 • a = [34, 'Fred', 12,

    False, 72.3] a[1], len(a) a.append(“new”), a.extend(74, 75) a.pop(), a.pop(0) a[1:3], a[ :3], a[-2: ] List
  8. 10 a = [34, 'Fred', 12, False, 72.3] for x

    in a: print(x) a = [34, 'Fred', 12, False, 72.3] for (i, x) in enumerate(a): print(i, x) Iterating & Enumerating a list
  9. 13 class Person: def __init__(self, first_name, surname, tel): self.first_name =

    first_name self.surname = surname self.tel = tel def full_name(self): return self.first_name + " " + self.surname p = Person("Xiao-Min", "Wang", "1234567") p.full_name() 類別
  10. 14 # import MODULE_NAME import RPi.GPIO # import MODULE_NAME as

    ALIAS_NAME import RPi.GPIO as GPIO # from MODULE_NAME import FUNCTION_NAME from time import sleep 模組
  11. 15 • 安裝必要的套件 • sudo apt-get update • sudo apt-get

    install python-dev python-pip • sudo pip install bottle • 建立一目錄 web, 包含兩個檔案 server.py 和 foobar.tpl, 結構如 下 $ tree web/ web/ |-- foobar.tpl `-- server.py • 執行 $ python server.py 用 Bottle 寫一個簡單的 Web Server
  12. 16 from bottle import route, run, debug, template @route('/hello') def

    hello(): return "Hello World!" @route('/foo') def bar(): output = template("foobar", rows=[34, 'Fred', 12, False, 72.3]) return output run(host='0.0.0.0', port=8080, debug=True) server.py