introduction to python programming

28
自自自 自自自自自自 自自自 [email protected] 自自自 自自自 http://www.openfoundry.org/ 自自自自自 自自自自自自自 自自自自 http://www.slat.org/ Apr 26, 2008

Upload: tsungwei-hu

Post on 12-May-2015

2.157 views

Category:

Technology


11 download

DESCRIPTION

A quick tutorial on Python programming, Apr 26, 2008, in YiLan, Taiwan.

TRANSCRIPT

Page 1: Introduction to Python Programming

自由軟體程式設計入門胡崇偉

[email protected]

自由軟體鑄造場http://www.openfoundry.org/中央研究院 資訊科學研究所

軟體自由協會http://www.slat.org/

Apr 26, 2008

Page 2: Introduction to Python Programming

2

自由軟體鑄造場簡介

• 營運 OpenFoundry 系統– 專案管理服務 : 下載檔案 回報修改意見 通信討論

• 彙整學習資源及社群資訊– 資源目錄 人才資料庫 電子報 推廣光碟

• 提供法律授權研究報告及諮詢– 授權條款精靈 各國政策研究

• 進行校園演講研討會及國際交流

Page 3: Introduction to Python Programming

3

軟體自由協會簡介

• 宣揚軟體自由的理念• 推廣自由軟體與開放源碼的使用和開發• 參與校園自由軟體應用諮詢中心專案• 舉辦 ICOS 研討會

Page 4: Introduction to Python Programming

4

為什麼要學程式語言 ?

• 培養解決問題的能力與技巧– 數學家 工程師 藝術家 三種思惟的結合

• 增進對電腦系統的掌握並發揮其效能• 找到動機就能事半功倍

Page 5: Introduction to Python Programming

5

學習電腦的好策略

•找到學習動機並設定合理的目標• 結合日常生活的應用情境• 優先學習通用原則並長期投資• 三人行必有我師 ( 身邊朋友助益最直接 )

• 主動尋找社群同好• 善用工具 ( 自由軟體是寶山 )

Page 6: Introduction to Python Programming

6

好工具助你快速上路• Python 下載及資訊網址

– http://www.python.org/

– http://www.python.org.tw/

• Python 特色優點–易學易安裝–開放源碼免費在各式環境執行–原始碼易讀易管理

Page 7: Introduction to Python Programming

7

Python Shell

>>> 是提示符號

Page 8: Introduction to Python Programming

8

Python 程式初體驗print “hello world“

9 + 7 - 6

5 * 4 / 2

3 ** 2

a = “3 x 3 =“

b = 3 * 3

print a, b

Page 9: Introduction to Python Programming

9

運算式的表達3 * 3 == 3 ** 2

3 * 3 == 3 ** 3

if 3 * 3 == 3 ** 2:

print “True!”

Page 10: Introduction to Python Programming

10

程式碼的撰寫與執行

# 代表註解符號 , 表示之後的字串都是註解文字 , 將不會被執行

Page 11: Introduction to Python Programming

11

迴圈運算a = 3

while a > 0:

print “count down”, a

for a in 3,2,1:

print “count down“, a

Page 12: Introduction to Python Programming

12

產生數列的工具range(3)

for a in range(3):

print “count down”, a

for a in range(10,0,-1):

print “count down“, a

Page 13: Introduction to Python Programming

13

顯示九九乘法表

Page 14: Introduction to Python Programming

14

錯誤訊息的處理

Page 15: Introduction to Python Programming

15

interable 例子• myString = ‘I Love Python’

• myTuple = (‘I Love’, 7, 11)

• myList = [‘I Love’, 7, 11]

• 操作方式 : 索引運算 , 切片運算• myString[0]

• myList[0:2]

Page 16: Introduction to Python Programming

16

索引運算

I L o v e P y t h o n

[0

1

2

7

-6

-2

-1

↓]

Page 17: Introduction to Python Programming

17

載入其他功能import math

math.sqrt(2)

from math import sqrt

sqrt(2)

from math import *

Page 18: Introduction to Python Programming

18

function

• def myFunc(score):• from math import sqrt• a_score = sqrt(score)*10• return a_score

Page 19: Introduction to Python Programming

19

檔案讀取file = open(“sample1.txt”, “r”)

lines = file.readlines()

lines

len(lines)

for line in lines:

print line

Page 20: Introduction to Python Programming

20

字串轉成數字• score = ’60’

• math.sqrt(score)

• TypeError: a float is required

• import string

• string.atoi(score)

Page 21: Introduction to Python Programming

21

文字稿變成網頁• RestructuredText

• http://docutils.sourceforge.net/

• 支援的格式 : HTML,XML,S5,LaTeX

• rst2html < myRST.txt > myRST.html

Page 22: Introduction to Python Programming

22

內容管理系統• Plone: 以 Python 寫成的網站軟體• http://plone.org/

• 可下載 Windows Installer 輕鬆安裝使用

• 適合建置成知識管理網站

Page 23: Introduction to Python Programming

23

以 Zope 為基礎

• 內建的功能包括– 網站伺服器–物件式資料庫–關連式資料庫–搜尋引擎– 會員權限管理– 工作流程管理

Scripting Content

Template Object

ZOPE

Python

Plone

Page 24: Introduction to Python Programming

24

管理介面

• Zope Management Interface (ZMI)

• Root Folder 是最上層的目錄• 使用者權限主要分兩類 : Manager, Membe

r

• 帳號密碼管理 : acl_users

• 操作技巧 : 同時開啟兩種瀏覽器 以不同身份與權限來登入系統

Page 25: Introduction to Python Programming

25

網站設定

Page 26: Introduction to Python Programming

26

可新增的內容項目

• Link• Document• NewsItem• Event• Image• PowerPoint

Page 27: Introduction to Python Programming

27

Add Link 新增書籤

Page 28: Introduction to Python Programming

28

工作流程管理Unauthorized

User PublishedContents

Reviewer

Author

Publish

Reject

PendingContents

PrivateContents

View

EditSubmit

Retract