Introduction to Python Programming
-
Upload
tsungwei-hu -
Category
Technology
-
view
2.150 -
download
11
description
Transcript of Introduction to Python Programming
自由軟體程式設計入門胡崇偉
自由軟體鑄造場http://www.openfoundry.org/中央研究院 資訊科學研究所
軟體自由協會http://www.slat.org/
Apr 26, 2008
2
自由軟體鑄造場簡介
• 營運 OpenFoundry 系統– 專案管理服務 : 下載檔案 回報修改意見 通信討論
• 彙整學習資源及社群資訊– 資源目錄 人才資料庫 電子報 推廣光碟
• 提供法律授權研究報告及諮詢– 授權條款精靈 各國政策研究
• 進行校園演講研討會及國際交流
3
軟體自由協會簡介
• 宣揚軟體自由的理念• 推廣自由軟體與開放源碼的使用和開發• 參與校園自由軟體應用諮詢中心專案• 舉辦 ICOS 研討會
4
為什麼要學程式語言 ?
• 培養解決問題的能力與技巧– 數學家 工程師 藝術家 三種思惟的結合
• 增進對電腦系統的掌握並發揮其效能• 找到動機就能事半功倍
5
學習電腦的好策略
•找到學習動機並設定合理的目標• 結合日常生活的應用情境• 優先學習通用原則並長期投資• 三人行必有我師 ( 身邊朋友助益最直接 )
• 主動尋找社群同好• 善用工具 ( 自由軟體是寶山 )
6
好工具助你快速上路• Python 下載及資訊網址
– http://www.python.org/
– http://www.python.org.tw/
• Python 特色優點–易學易安裝–開放源碼免費在各式環境執行–原始碼易讀易管理
7
Python Shell
>>> 是提示符號
8
Python 程式初體驗print “hello world“
9 + 7 - 6
5 * 4 / 2
3 ** 2
a = “3 x 3 =“
b = 3 * 3
print a, b
9
運算式的表達3 * 3 == 3 ** 2
3 * 3 == 3 ** 3
if 3 * 3 == 3 ** 2:
print “True!”
10
程式碼的撰寫與執行
# 代表註解符號 , 表示之後的字串都是註解文字 , 將不會被執行
11
迴圈運算a = 3
while a > 0:
print “count down”, a
for a in 3,2,1:
print “count down“, a
12
產生數列的工具range(3)
for a in range(3):
print “count down”, a
for a in range(10,0,-1):
print “count down“, a
13
顯示九九乘法表
14
錯誤訊息的處理
15
interable 例子• myString = ‘I Love Python’
• myTuple = (‘I Love’, 7, 11)
• myList = [‘I Love’, 7, 11]
• 操作方式 : 索引運算 , 切片運算• myString[0]
• myList[0:2]
16
索引運算
I L o v e P y t h o n
[0
↓
1
↓
2
↓
7
-6
↓
-2
↓
-1
↓]
17
載入其他功能import math
math.sqrt(2)
from math import sqrt
sqrt(2)
from math import *
18
function
• def myFunc(score):• from math import sqrt• a_score = sqrt(score)*10• return a_score
19
檔案讀取file = open(“sample1.txt”, “r”)
lines = file.readlines()
lines
len(lines)
for line in lines:
print line
20
字串轉成數字• score = ’60’
• math.sqrt(score)
• TypeError: a float is required
• import string
• string.atoi(score)
21
文字稿變成網頁• RestructuredText
• http://docutils.sourceforge.net/
• 支援的格式 : HTML,XML,S5,LaTeX
• rst2html < myRST.txt > myRST.html
22
內容管理系統• Plone: 以 Python 寫成的網站軟體• http://plone.org/
• 可下載 Windows Installer 輕鬆安裝使用
• 適合建置成知識管理網站
23
以 Zope 為基礎
• 內建的功能包括– 網站伺服器–物件式資料庫–關連式資料庫–搜尋引擎– 會員權限管理– 工作流程管理
Scripting Content
Template Object
ZOPE
Python
Plone
24
管理介面
• Zope Management Interface (ZMI)
• Root Folder 是最上層的目錄• 使用者權限主要分兩類 : Manager, Membe
r
• 帳號密碼管理 : acl_users
• 操作技巧 : 同時開啟兩種瀏覽器 以不同身份與權限來登入系統
25
網站設定
26
可新增的內容項目
• Link• Document• NewsItem• Event• Image• PowerPoint
27
Add Link 新增書籤
28
工作流程管理Unauthorized
User PublishedContents
Reviewer
Author
Publish
Reject
PendingContents
PrivateContents
View
EditSubmit
Retract