raspberry pi讀書會#3 - raspberry pi cookbook ch5, ch6, ch7

19
Raspberry Pi 讀書會 #3 - Raspberry Pi Cookbook 台灣樹莓派 <[email protected]> Dec 08, 2014

Upload: raspberrypi-tw

Post on 15-Jul-2015

121.040 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

Raspberry Pi 讀書會 #3- Raspberry Pi Cookbook

台灣樹莓派 <[email protected]>Dec 08, 2014

Page 2: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

姓名標示 — 非商業性 — 相同方式分享

CC (Creative Commons)

姓名標示 — 你必須給予 適當表彰、提供指向本授權條款的連結,以及 指出(本作品的原始版本)是否已被變更。你可以任何合理方式為前述表彰,但不得以任何方式暗示授權人為你或你的使用方式背書。

非商業性 — 你不得將本素材進行商業目的之使用。

相同方式分享 — 若你重混、轉換本素材,或依本素材建立新素材,你必須依本素材的授權條款來散布你的貢獻物。

Page 3: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

3

● 5. Python Basics● 6. Python Lists and Dictionaries● 7. Advanced Python

本次領讀章節

Page 4: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

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

Page 5: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

5

● 動態型別 (dynamic typing)

# 這是註解

i = 3 # 變數 i 指到數字物件 3

i = [1, 2, 3, 4, 5] # 變數 i 指到串列物件

print( i[2] ) # 印出串列中第三個元素

i = “abcde” # 變數 i 指到字串物件

print( i[2] ) # 印出字串中第三個元素

變數 , 物件 , 型別 , 註解

Page 6: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

6

● 相連 (+)● “abc” + “def”

● 數字轉型 , 字串轉型

● str(123), int(“-123”)● 字串長度

● len(“abcdef”)● 字串擷取

● s = “abcdefghi”● s[1:5], s[ :5], s[3: ]● s[-3: ]

字串用法

Page 7: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

7

● 用縮排取代大括號● 程式碼的區塊是用縮排分隔● 不使用 tab, 使用空白鍵

● 常見縮排為 4 個空白鍵

縮排

Page 8: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

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 敘述

Page 9: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

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

Page 10: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

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

Page 11: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

11

● 需要 key-value 的存取

a = {'key1':'value1', 'key2':'value2'}

a['key1']

a['foo'] = 'bar'

a.pop('key2')

Dictionary

Page 12: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

12

def count(from_num=1, to_num=10):

for i in range(from_num., to_num+1):

print(i)

count()

count(5)

count(5, 10)

函式

Page 13: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

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()

類別

Page 14: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

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

模組

Page 15: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

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

Page 16: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

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

Page 17: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

17

<table border="1">

%for (i, x) in enumerate(rows):

<tr>

<td>{{i}}</td>

<td>{{x}}</td>

</tr>

%end

</table>

foobar.tpl

Page 18: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

18

Demo

Page 19: Raspberry Pi讀書會#3 - Raspberry Pi Cookbook Ch5, Ch6, Ch7

Raspberry Pi Rocks the World

Thanks