2010 08-19-30 minutes of python

41
30 minute of Python 30 minute of Python Name Name : : Kang-min Wang ( Aminzai ) Kang-min Wang ( Aminzai ) Date Date : : 2010/08/19 2010/08/19 Email Email : : aminzai –at-- aminzai.net aminzai –at-- aminzai.net

Upload: kang-min-wang

Post on 19-May-2015

328 views

Category:

Education


2 download

TRANSCRIPT

Page 1: 2010 08-19-30 minutes of python

30 minute of Python30 minute of Python

NameName :: Kang-min Wang ( Aminzai )Kang-min Wang ( Aminzai )DateDate :: 2010/08/192010/08/19EmailEmail :: aminzai –at-- aminzai.netaminzai –at-- aminzai.net

Page 2: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

2

OutlineOutline

● 迷漾的 Outline....

Page 3: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

3

What's Python?What's Python?

一條蟒蛇?

Page 4: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

4

What's Python?What's Python?

Page 5: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

5

HistoryHistory

● Python 的創始人為吉多 · 范羅蘇姆。在 1989 年聖誕節期間的阿姆斯特丹,吉多為了打發聖誕節的無趣,決心開發一個新的指令碼解釋程式● 我們究竟在幹嘛阿 ...

Page 6: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

6

Who use it?Who use it?

● Google● Youtube● BitTorrent● NASA● OLPC● Plurk● Me 0.0\-/

Page 7: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python 7

Page 8: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

8

Why Google use it?Why Google use it?

● 由於 Python 對於 C 和其他語言的良好支援,很多人還把 Python 作為一種「膠水語言」( glue language )使用。使用 Python 將其他語言編寫的程式進行整合和封裝。

Page 9: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

9

Python's Core ideologyPython's Core ideology

● There is only one way to do it.要做好一件事,一種方法就夠了。

● Everything is object.萬物皆物件

● Readability counts.可讀性優先

● Explicit is better than implicit.明顯比隱晦好

● Simple is better than complex.簡單比複雜好

Page 10: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

10

AdvantageAdvantage

● 簡單易讀● 開發快速● 易於協同開發● 很快就可以上手● 記憶體回收機制● 養成良好習慣

Page 11: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

11

ShortcomingShortcoming

● 速度仍然比 C 慢● 有些超級老的 cpu 不能跑● 有些模組比較肥 (xml 相關 )

Page 12: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

12

Develop EnviormentDevelop Enviorment

● Eclipse + pyDev● Python IDLE● Vim <== 我在用的 XD

Page 13: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

13

11stst example example

Hello, World!

Page 14: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

14

NumbersNumbers

● 整數 (int)● 浮點 (float)● 長整數 (long)● 八進位與十六進位● 複數 (complex)● 布林值 (bool)

Page 15: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

15

OperationOperation

● + - * / ** //...● 遵守四則運算規則● 數學函式: pow abs...● Modules : random math...● 轉換進制: oct() hex()....

from decimal import DecimalDecimal()

● 集合 set()

Page 16: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

16

StringString

● 字串 (str)● Raw● Unicode● byte(in Python 3.0)● + *...● len() ● slice notation● Replace ,Upper....

Page 17: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

17

Slice(array)Slice(array)

S L I C E

0 1 2 3 4 5

[: :]

[ 起使:終止:步進 ]

Page 18: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

18

ListList

● [ 'abc', 123 , [ 'a' , 'b' ] ]● 可任意巢狀化,不限定型態

Page 19: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

19

TupleTuple

● ( 'abc', 123 , [ 'a' , 'b' ] )● 內含物不可變更的 list

Page 20: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

20

Dict (Dict (Dictory)Dictory)

● { 'name' : 'billy3321' ,'jobs' : ['student', 'maintainer'], 'develop' : {'name' : 'lazybuntu', 'OS' : 'Ubuntu' } }

● Key : Value● 無序集合體,以 key存取● 以 hash table實作

Page 21: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

21

File I/OFile I/O

● myfile = open('myfile', 'w')myfile.write('Hello, World!')myfile.close()

● myfile = open('myfile', 'r')myfile.readline()myfile.readline()myfile.readline()myfile.readlines()myfile.close

Page 22: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

22

Dynamic type?Dynamic type?

● 整數,字串是不能改的?我明明就可以改他啊!● A = 3B = Aprint A , BA = 'hello'print A , B

● Refrence(參照值 ) → object( 物件 )● 屬性是屬於物件的,而變數就是參照值● [ 'abc' , [(1, 2), ([3],4)],'def']

Page 23: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python 23

[ 'abc' , [(1, 2), ([3],4)],'def']

'abc' (1, 2) ([3],4) 'def'

1 2 [3] 4

3

Page 24: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python 24

Let's think!為什麼縮排?

Page 25: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python 25

給你三秒鐘

Page 26: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

26

給你三秒鐘給你三秒鐘 !!!!

if (x) if (y)

statement1;else statement;

else statement2;

Page 27: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python 27

看完了嘛?

Page 28: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python 28

縮排是 Python 的其中一重點!

Page 29: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

29

縮排縮排

● 可讀性優先 ; readability counts● 把習慣養好● 方便大家協同作業,大家的程式碼一目瞭然。

Page 30: 2010 08-19-30 minutes of python

●While loopWhile loop

● while true: print "Spam"else print "Aminzai"

● while i < 5: i = i + 1 print i

● countinue break pass else

Page 31: 2010 08-19-30 minutes of python

●For loopFor loop

● for i in lists: print i

● for line in open('file'):● 如果要反覆特定次數:搭配 range()

for i in range(5) print "Spam"

Page 32: 2010 08-19-30 minutes of python

FunctionFunction

● def print_aminzai(): print "Aminzai"

print_aminzai()● 利用 return 回傳值● 注意:變數所存在之範圍內建>廣域>函式>區域

Page 33: 2010 08-19-30 minutes of python

ObjectObject

● class Person: def __init__(self, name, job): self.name = name self.job = job def info(self): return (self.name, self.job)

aminzai = Person(‘Aminzai’, ‘student’)aminzai.jobaminzai.info()

Page 34: 2010 08-19-30 minutes of python

ObjectObject

● class man(Person): self.sex = male

aminzai = man(‘aminzai’, ‘student’)aminzai.sexaminzai.info()

Page 35: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

35

DocumentDocument

● help()● dir()● Pydoc● 說明文件和程式碼放在一起

Page 36: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python 36

Page 37: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python 37

Page 38: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

38

Fire Show!!Fire Show!!

● Plurk BOT● http://www.plurk.com/isu_ann

● Lazyscripts● http://lazyscripts.org

● Google App Engines● Geeklothes

– http://geeklothes-tw.appspot.com● ECG Waveform

– http://isucsieeslabecg.appspot.com/SWF● Conference Joke

– http://confjoke.appspot.com/

Page 39: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

39

ResourceResource

● PyTUG:www.python.org.tw● irc.://irc.freenode.net/#python.tw● Ptt Python討論版● http://www.python.org/

Page 40: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python 40

Page 41: 2010 08-19-30 minutes of python

2010/08/09 30 minutes Python

41

Next TopicNext Topic

HadoopOr

My Develop Envirment