Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python
Search
nctunba
May 06, 2012
3
520
Python
nctunba
May 06, 2012
Tweet
Share
More Decks by nctunba
See All by nctunba
被 Qt 快樂的玩弄
nctunba
2
390
被 Qt 快樂的玩弄 part 1
nctunba
1
280
Node - Express - Socket.io
nctunba
1
420
node
nctunba
9
50k
jQuery
nctunba
9
610
JS
nctunba
11
640
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
Speed Design
sergeychernyshev
32
1k
Why Our Code Smells
bkeepers
PRO
337
57k
The Cost Of JavaScript in 2023
addyosmani
51
8.5k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
Done Done
chrislema
184
16k
How to Ace a Technical Interview
jacobian
277
23k
We Have a Design System, Now What?
morganepeng
53
7.7k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.4k
Transcript
Python 社課 教學:小雞
Python 下載安裝 http://www.python.org/getit/
None
None
Python 語言介紹 • 完全物件導向語言 • 直譯式 • 語法簡潔清晰 • 作者
- 吉多 · 范羅蘇母
Python 設計哲學 「優雅」、「明確」、「簡單」 「最好是只有一種方法來做一件事」 import this Beautiful is better than
ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.
Why Python ? 優點 可讀性極佳、易維護 開發速度快 自動記憶體管理 小缺陷 執行速度較慢
誰在用 Python ? google 網站搜尋系統 youtube 視頻共享系統 BitTorrent 點對點檔案共享系統
Python 基本特性 • 縮排 • 不用分號 • 宣告 • 強型態
• 註解
Sample Code //c++ int age = 18; if(age < 20)
{ cout<<"You can't drive car\n"; } else { cout<<"You can drive car\n"; } cout<<“ 這句在 if 的外面 \n"; #python age = 18; if age < 20: print "You can't drive car" else: print "You can drive car“ print “ 這句在 if “ 的外面 “”” 我是註解 “””
Python 物件型態 • 數字 Number • 字串 String • 串列
List • 辭典 Dictoinary • 元組 Tuple • 布林 boolean
物件型態 - 數字 (1 /2) >>> 10 10 >>> 1+2
3 >>> 1.0005*3 3.0015
物件型態 - 數字 (2 /2) >>>9999999999999999999999999 99999 99999999999999999999999999999 9L >>>2**100
12676506002282294014967032053 76L
物件型態 - 字串 (1 /5) >>> S = "python“ >>>
S 'python‘ >>> len(S) 6
物件型態 - 字串 (2 /5) >>> S[0] 'p' >>> S[-1]
'n' >>> S[0:2] 'py' >>> S[0:] 'python' >>> S[1:] 'ython'
物件型態 - 字串 (3 /5) >>> S + " is
good!!" 'python is good!!' >>> S * 4 'pythonpythonpythonpython'
物件型態 - 字串 (4 /5) >>> S = "python" >>>
S[0] = 'z' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment
物件型態 - 字串 (5 /5) >>>S = " python" >>>
S.find("on") 4 >>> S.replace("p", "XYZ") 'XYZython' >>>help(str)
物件型態 - 字串 (5 /5) >>>S = " python" >>>
S.find("on") 4 >>> S.replace("p", "XYZ") 'XYZython' >>>help(str)
物件型態 - 字串 (5 /5) >>>S = " python" >>>
S.find("on") 4 >>> S.replace("p", "XYZ") 'XYZython' >>>help(str)
DIY 將字串改成正確的內容 S=“I love Perl, and I hate Python”
DIY 參考解答 S.replace("hate", "love") .replace("love", "hate", 1)
物件型態 - 串列 (1 /6) >>> L = [123, "python",
12.3] >>> L [123, 'python', 12.3] >>> len(L) 3
物件型態 - 串列 (2 /6) >>> L = [123, "python",
12.3] >>> L[0] 123 >>> L[1:] ['python', 12.3]
物件型態 - 串列 (3 /6) >>> L + [3, 4,
5] [123, 'python', 12.3, 3, 4, 5] >>> L[0] = "hello world" >>> L ['hello world', 'python', 12.3]
物件型態 - 串列 (4 /6) >>> L[99] Traceback (most recent
call last): File "<stdin>", line 1, in <module> IndexError: list index out of range
物件型態 - 串列 (5 /6) >>> L = [123, "python",
12.3] >>> L.append("last") >>> L [123, 'python', 12.3, 'last'] >>> L.pop() 'last‘
物件型態 - 串列 (5 /6) >>> L = [3,4,2,5,1] >>>
L.sort() >>> L [1, 2, 3, 4, 5] >>> L.reverse() >>>L [5, 4, 3, 2, 1]
DIY Try try L = [ “1234”, “23”, “345”] L.sort()
DIY 參考解答 L = [ “1234”, “23”, “345”] 完全不變 ~
物件型態 - 串列 (6 /6) >>> L2 = [[1, 2,
3], [4, 5, 6], [7, 8, >>> L2 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> L2[0][0] 1
物件型態 - 辭典 (1 /3) >>> D = {"python" :
"good", "hello" : "world", "number" : 1} >>> D['python'] 'good'
物件型態 - 辭典 (2 /3) >>> info = { "name":{"first":"Bob",
"last":"Sm "children":["John", "Peter"], "age":40}
物件型態 - 辭典 (3 /3) >>> D = {“A" :
1, " B" : 2} >>> D.keys() ['A', 'B'] >>> D.has_key("B") True
DIY >>> D = {“A" : 1, " B" :
2, “C”:3} >>> E = {“1”:”A”, “2”: “B”, “3”:”C” D + E 是?
DIY 參考解答 Error ! Dict … 不能相加 … 不過,若要把 E
加進 D ,該如何 做? >>>D.update(E )
物件型態 -tuple >>> list = [123, "python", 12.3] >>> tuple
= (123, "python", 12.3) >>> list[0] = 456 >>> tuple[0] = 456 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
物件型態 - 布林 >>> True True >>> true Traceback (most
recent call last): File "<stdin>", line 1, in <module> NameError: name 'true' is not defined
DIY … 請問 假值是?
DIY 參考解答 …. False
If 判斷式 a = 10 b = 11 if a
< b: print " a<b" elif a == b: print " a = b" else: print “a > b"
推薦用法 D = { 'a':1, "b":2, "c":3} 較差的用法 if D.has_key(‘a’):
print ‘haha’ 較好的用法 if ‘a’ in D: print ‘haha’
流程控制 • for • while • Try/Except
流程控制 -For(1/3) #foreach nameList = ["Peter", "Jane", "John"] for name
in nameList: print name
流程控制 -For(2/3) #for for _ in range(3): print nameList[_]
流程控制 -For(3/3) Animals = [(0, ‘dog’), (1, ‘cat’), ] for
index, animal in Animals: print index, animal
DIY pets = ['dog', 'cat', 'bird'] 請照順序,依序印出 < 序號 ,
名稱 > <0, dog> <1, cat> <2, bird>
DIY 參考解答 for index, pet in enumerate(pets): print “<%d, %s>”%(index,
pet)
流程控制 -While i = 0 while True: print "in while"
if i>10: break i=i+1
DIY while False: print “ 我會被印出來嗎? "
DIY 參考解答 … 不會 廢話。
Try-except(1/2) try: while True: print " 啦啦~有種就中斷我 " except KeyboardInterrupt:
print " 我被中斷了 "
Try-except(2/2) AttributeError 使用不存在的方法 IndexError 使用超出大小的串列 IOError I/O 操作所引發的異常 KeyError 使用字典中不存在的關鍵
字 TabError 語句塊縮進不正確 KeyboardInterrupt 按 Ctrl+C 中斷
DIY D = {'a':' IndexError ', 'b':' KeyEr ror '}
請問? D[‘c’] 是 IndexError or KeyError
DIY 參考解答 D[‘c’] 是 KeyError
基本輸入輸出 (1/2) raw_input() # 輸入 print # 輸出 string =
“world” print “hello %s”%string
基本輸入輸出 (2/2) #open 讀檔 f = open("test.txt", "r") print f.readline()
f = open(“output.txt”, “w”) f.write(“hahahah”) f.close()
DIY 實作 你的名字是: < 你的名字 > Hello < 你的名字 >
DIY 參考解答 name = raw_input(“ 你的名字 是: ") print “Hello
%s”%name
自定義函式 -def(1/ 2) >>> def func(): ... : print ‘I
am chicken’ >>> func() I am chicken
自定義函式 -def(2/ 2) >>> def func2(a, b): ... : return
(a,b) >>> func(1, 3) (1,3)
DIY >>> a = 3 >>> def func3(): ... :
a = 4 請問 >>> a
DIY 參考解答 a=3
自定義函式 >>> def func4(*names): … : return names >>>func4(“marco”, “chicken”)
(“marco”, “chicken”)
自定義函式 >>> def func5(**args): … : return args >>>func5(id=3, name=‘chicken
’) {'id': 3, 'name': 'chicken'}
DIY 想一想,下課解答 如何實作 printf
模組 (1/4) c/c++ 有 include Java 有 import … 那
python 呢? import modulename
模組 (2/4) >>>import math >>>math.sqrt(2) 1.4142135623730951
模組 (3/4) #test.py def func1(): print "in the test.py“
模組 (4/4) #test2.py import test test.func1() >>>python test.py in the
test.py
DIY 請寫一個 myModule.py 檔 引入後能夠達到下列效果 >>>Import myModule >>>myModule.sum([1,2,3]) 6
DIY 實作 printf ,參考解答 def printf(format, *data): print format%data