40
Raspberry Pi 研習營 CAVE Education 徐豐智 E - mail:jesusvictory777@cavedu,com 1

[3]投影片 futurewad樹莓派研習會 141204

Embed Size (px)

Citation preview

Page 1: [3]投影片 futurewad樹莓派研習會 141204

Raspberry Pi 研習營

CAVE Education

徐豐智

E-mail:jesusvictory777@cavedu,com

1

Page 2: [3]投影片 futurewad樹莓派研習會 141204

Session 4: Python Introduction

https://code.google.com/p/zhpy/wiki/IntroZhpy

2

Page 3: [3]投影片 futurewad樹莓派研習會 141204

Python優點

• 物件導向式語言

• 語法如同閱讀英文

• 直譯:不再需要擔心如何编譯程序,如何確保連接轉譯成正确的函式庫

• 可擴展性與可編譯性

• 豐富的函式庫– 文字檔生成、單元測試、線程、資料庫、網頁瀏覽器、CGI、FTP、電子郵

件、XML、XML-RPC、HTML、WAV文件、密碼系統、GUI(圖形使用者介面)

3

Page 4: [3]投影片 futurewad樹莓派研習會 141204

Python語法缺點

• 強制縮排

• 單行語句

• 命令行輸出問題

4

Page 6: [3]投影片 futurewad樹莓派研習會 141204

啟動IDLE 3

6

Page 7: [3]投影片 futurewad樹莓派研習會 141204

Python建立.py檔案

• 新增.py檔案,自行取名檔案名稱

• 立即儲存檔案

–標題會有取名的檔案名稱

7

Page 8: [3]投影片 futurewad樹莓派研習會 141204

建立Python程式

• .py檔中輸入指令後儲存:

• a = input(“input value:”)

• b = input(“input second”)

• c = a + b

• print c

8

Page 9: [3]投影片 futurewad樹莓派研習會 141204

建立Python程式

• .py檔中輸入指令後儲存:

• a = input(“input value:”) #宣告變數a,等待輸入數值

• b = input(“input second”)#宣告變數b,等待輸入數值

• c = a + b #宣告變數c,c=a+b

• print c#印出變數c

9

Page 10: [3]投影片 futurewad樹莓派研習會 141204

LXTerminal執行Python程式

• 在LXTerminal輸入指令:

• $python filename.py :

• 使用python程式,開啟檔案filename.py

10

Page 11: [3]投影片 futurewad樹莓派研習會 141204

辨識符號a,b,c

• 使用辨識符號時只需要給它們賦一個值。不需要聲明或定義資料型別

• 標識符號的第一個字符必須是字母表中的字母(大寫或小寫)或者底線

• 標識符號名稱需要區分大小寫

• EX:apple7、7apple

11

Page 12: [3]投影片 futurewad樹莓派研習會 141204

建立Python程式2

• .py檔中輸入指令後儲存:

• #!/usr/bin/python :使用的python程式路徑

• a = input(“input value:”)

• b = input(“input second”)

• c = a + b

• print c

註1:必须放在python文件的第一行或第二行

註2: # -*- coding: <encoding name> -*- #!/usr/bin/pythona = input(“input value:”)

b = input(“input second”)c = a + b

print c

Page 13: [3]投影片 futurewad樹莓派研習會 141204

LXTerminal執行Python程式2

• LXTerminal:

• $chmod +x filename.py

• $./filename.py

– chmod設定檔案及目錄讀取屬性及狀態

– +x 增加權限,設為可以執行

13

Page 14: [3]投影片 futurewad樹莓派研習會 141204

Linux指令:chmod、chgrp、chown

• 更改檔案的權限與屬性

• chgrp : (change group)改變檔案所屬群組

• chown :(change owner)改變檔案擁有者

• chmod :改變檔案的權限

14

Page 15: [3]投影片 futurewad樹莓派研習會 141204

下達指令

• sudo ls -al

• (list -all)

15

Page 16: [3]投影片 futurewad樹莓派研習會 141204

Linux檔案屬性

• [1]檔案類型的權限

16

Page 17: [3]投影片 futurewad樹莓派研習會 141204

Linux檔案屬性

• [2]表示有多少檔名連結到此節點

• [3]表示這個檔案(或目錄)的『擁有者帳號』

• [4]表示這個檔案的所屬群組

17

Page 18: [3]投影片 futurewad樹莓派研習會 141204

Linux檔案屬性

• [5]為這個檔案的容量大小,預設單位為bytes

• [6]為這個檔案的建檔日期或者是最近的修改日期

• [7]為這個檔案的檔名

18

Page 19: [3]投影片 futurewad樹莓派研習會 141204

改變檔案權限:chmod

• 數字類型改變檔案權限

– owner/group/others三種身份各有自己的read/write/execute權限

–檔案的權限字元為:『-rwxrwxrwx』

–各權限的分數r:4,w:2,x:1

–各自的三個權限(r/w/x)分數需要累加

• Ex:owner = rwx = 4+2+1 = 7

– group = rwx = 4+2+1 = 7

– others= --- = 0+0+0 = 019

Page 20: [3]投影片 futurewad樹莓派研習會 141204

• sudo chmod 770

20

Page 21: [3]投影片 futurewad樹莓派研習會 141204

改變檔案權限:chmod

• 符號類型改變檔案權限

• Ex:

– sudo chmod u=rwx,go=rx filename

– sudo chmod a+w filename

– ls -al filename

21

chmod

ugoa

+(加入)-(除去)=(設定)

rwx

檔案或目錄

Page 22: [3]投影片 futurewad樹莓派研習會 141204

改變檔案群組:chgrp

• 群組檔案

• /etc/group

• cd /etc

• sudo nano group

• Ex:

• chgrp groupname filename

22

Page 23: [3]投影片 futurewad樹莓派研習會 141204

改變檔案使用者:chown

• 使用者檔案

• /etc/passwd

• Ex:

• chown ownername filename

23

Page 24: [3]投影片 futurewad樹莓派研習會 141204

建立Python程式3

• .py:• #!/usr/bin/python

• a = input(“input value:”)

• b = input(“input second”)

• c = a + b

• if a > b: :如果a>b

• c=a-b : (縮排)c=a-b

• else: :否則

• c=b-a : (縮排)c= b-a

• print c :印出變數c

24

Page 25: [3]投影片 futurewad樹莓派研習會 141204

Python程式與C的不同

• 一開始打入的a與b,視為宣告變數a、b

• 判斷式沒有大括弧與小括弧,一切以程式是否同一排為準。”縮排”(按鍵Tab)十分重要

• 一行程式碼結束,不需要 ; 號

• 註解為”#”號

25

Page 26: [3]投影片 futurewad樹莓派研習會 141204

建立Python程式4

.py檔案:

#!/usr/bin/python

def function1():

a = input(“input value:”)

b = input(“input second”)

c = a + b

if a > b: :如果a>b

c=a-b : (縮排)c=a-b

else: :否則

c=b-a : (縮排)c= b-a

print c :印出變數c

function1()

26

Page 27: [3]投影片 futurewad樹莓派研習會 141204

Python程式使用函式庫/模組

27

#!/usr/bin/pythonimport time #輸入time函式庫/模組

a = input(“input value:”)b = input(“input second”)c = a + btime.sleep(5) #time的sleep函式等待五秒

print c

Page 28: [3]投影片 futurewad樹莓派研習會 141204

Python程式與C的不同:使用函式庫

• Python語言

• Import libraryname

• libraryname.libraryname2

• C語言

• Include<libraryname>

28

Page 29: [3]投影片 futurewad樹莓派研習會 141204

• Python函式庫介紹

• https://docs.python.org/2/library/

• 在Rpi搜尋套件

• sudo apt-cache search packagename

29

Page 30: [3]投影片 futurewad樹莓派研習會 141204

Python:撰寫函式庫1(.py)

#!/usr/bin/ python# File name: mymodule.py

def sayhi():print 'Hi, this is', __name__, 'speaking'

version = '0.1'# End of mymodule.py

30

Page 31: [3]投影片 futurewad樹莓派研習會 141204

Python:撰寫函式庫1(.py)

#!/usr/bin/python# File name: mymodule_demo.py

import mymodulemymodule.sayhi()print 'Version', mymodule.version

31

Page 32: [3]投影片 futurewad樹莓派研習會 141204

Python:撰寫函式庫1(LXT)

• $ python mymodule_demo.py

32

Page 33: [3]投影片 futurewad樹莓派研習會 141204

Python:撰寫函式庫2(.py)

• #!/usr/bin/python

• # File name: mymodule.py

if __name__ == '__main__':print 'This program is being run by itself.'print 'I can do other stuff here.'

else:print 'I am being imported from another module.'

33

Page 34: [3]投影片 futurewad樹莓派研習會 141204

Python:撰寫函式庫2(LXT)

• 執行1

• $python mymodule.py

• 執行2

• $python

• >>> import mymodule

34

Page 35: [3]投影片 futurewad樹莓派研習會 141204

在程式一開始時,啟動.py檔

• 修改 /etc/rc.local,在檔案後方的exit 0 前一行加入”要執行的指令”

• Ex:

• sudo nano /etc/rc.local

• 在rc.local的exit0前一行加入

• sudo /home/pi/filename

• 註:需要將Rpi開機預設為text console模式

35

Page 36: [3]投影片 futurewad樹莓派研習會 141204

Session 5: Scratch Introduction

http://www.codedata.com.tw/social-coding/scratch-for-kids-1

36

Page 37: [3]投影片 futurewad樹莓派研習會 141204

什麼是Scractch?

• 圖形化、中文化、積木堆疊的程式設計

–增加 – 增加更多的程式積木方塊進來,看看有沒有什麼有趣的變化?

–減少 – 減少程式中現有程式方塊,看看有沒有什麼問題發生?

–改變 – 改變程式方塊中的數值,看看是不是有按照預想中的方式執行

37

Page 38: [3]投影片 futurewad樹莓派研習會 141204

Scratch介面

38

Page 39: [3]投影片 futurewad樹莓派研習會 141204

Scratch動畫的座標軸

• 以座標控制角色的移動、旋轉

39

Page 40: [3]投影片 futurewad樹莓派研習會 141204

Scratch範例程式

• 惡龍鬥巫婆

• 下載檔案scratch_dragon.sb:

• https://sites.google.com/a/cavedu.com/www/file

40