Python A Comprehensive Programming Language 謝育璘 r03944051@ntu.edu.tw
Guido van Rossum Monty Python's Flying Circus
直譯 : 不用經過編譯就能執行.py 程式碼檔 (source file) 就是執行檔 (executable file) 不過系統要先安裝好 python 環境 3
直譯 : 不用經過編譯就能執行 互動式命令列 interpret.py 4
Python 版本 Python 2 vs. Python 3? 為什麼用 Python 2 許多函式庫尚未完成 Python 3 的移植 Python 2 最後一個開發版本將維持在 2.7.10, 並且不會再有新版 ( 不再加新功能 ), 除非有重大安全才會釋出安全更新版 為什麼用 Python 3 更強大更方便的語法 更強大更方便的函式庫 Python 3 是目前官方持續開發 ( 加新功能 ) 的版本
Python 安裝 Windows https://www.python.org/ Ubuntu sudo apt-get install python2.7 Mac https://docs.pythonguide.org/en/latest/starting/install/osx/
The Zen of Python There should be one and preferably only one obvious way to do it.
設計哲學 優雅 明確 簡單 高階語言 ( 比起 Java C++ C) 8
TIOBE 程式語言排名 9
I/O 基本輸入輸出 10
基本輸出 :print() 11
Python vs C: 輸入的不同 In C: scanf( %d,&a); scanf( %f,&a); scanf( %s,&a); 代表什麼? a 是 int / float / string 的行為會一樣嗎? 其實 C 有幫我們把輸入的 string 轉成變數的型態 In Python: python 的輸入 input() input() 是一個函式, 有回傳值 ( 就是輸入的 string ) 沒有幫我們把轉成任何變數型態 12
基本輸入 :input() input() 以行為單位 就算空白行也是行 使用者輸入的 input.p y 13
小知識 :python 的註解 單行註解 :# 之後到換行為止都是註解 ( 所以最多註解單行 ) 多行註解 : 利用多行字串 14
Built-in DataTypes Number String List Dictionary Set 7, 11.0 Hello World [7, 11, store ] { name : marr, home : taipei } set([7, 11, store ]) elements elements
int 整數 124 + - * / 1246 16
新增整數變數 四則運算 Elementary arithmetic 17
指定運算子 = 18
變數交換 變數 A 要跟變數 B 交換,C++ 怎麼做? In Python: A, B = B, A 19
小知識 : 型態承受力 整 數 語言 python C/C++ C/C++ 型態 int long long int 記憶體大小 不固定 8 byte 4 byte 數值大小 不固定 20
string 字串 H a t s u n e M I k u 21
新增字串變數 加乘運算 string operation 22
易讀多行字串 multi-line string 前後三個單引號或雙引號 23
String
Control Flow 程式流程控制 :if while 25
Code Indenting No explicit begin or end No curly braces if age > 18: print You are old enough. else: print You are too young.
if Statements x = int(raw_input("please enter an integer: ")) if x < 0: x = 0 print 'Negative changed to zero elif x == 0: print 'Zero elif x == 1: print 'Single else: print 'More'
For Statements words = ['cat', 'window', 'defenestrate'] for w in words: print w, len(w), cat 3 window 6 defenestrate 12 range(5, 10) [5, 6, 7, 8, 9] range(0, 10, 3) [0, 3, 6, 9] xrange vs. range for i in xrange(10): print i 0 1 2 3 4 5 6 7 8 9
else, break, continue for n in range(2, 10): for x in range(2, n): if n % x == 0: print n, 'equals', x, '*', n/x break else: print n, 'is a prime number'
else, break, continue for num in range(2, 10): if num % 2 == 0: print "Found an even number", num continue print "Found a number", num
while Statements x = 0 while x < 10: print x, x += 1 0 1 2 3 4 5 6 7 8 9
Data Structure 資料結構 :List Dictionary Set 32
List squares = [1, 4, 9, 16, 25] len(squares) 5 squares[-1] 25 squares[-3:] [9, 16, 25] # slicing returns a new list squares[:] [1, 4, 9, 16, 25] # slicing returns a new list squares + [36, 49] [1, 4, 9, 16, 25, 36, 49] squares.append(36) [1, 4, 9, 16, 25, 36] squares[2:3] = [] [1, 4, 25] squares[:] = [] x = [[ a, b, c ], [1, 2, 3]]
Dictionary tel = {'jack': 4098, 'sape': 4139} tel['guido'] = 4127 tel = {'jack': 4098, 'sape': 4139, 'guido': 4127} del tel['sape'] tel = {'jack': 4098, 'guido': 4127} tel.keys() ['guido', 'irv', 'jack'] 'guido' in tel True
Set basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] fruit = set(basket) # create a set without duplicates 'orange' in fruit True a = set('abracadabra') set(['a', 'r', 'b', 'c', 'd']) a = set('abc') b = set('ade') a b set(['c', 'b']) a b set(['a', 'c', 'b', 'e', 'd']) a & b set(['a']) a ^ b set(['c', 'b', 'e', 'd'])
function 函式 ( 函數 ) 36
Function Example def myfunc(a, b, c): document strings x = a + 1 y = b + 2 z = C + 3 return x, y, z
File Handling fileobj = open( myfile, r ) lines = fileobj.readlines() for line in lines: print line
hw1 輸入若干 n 個數字, 輸出指定第 k 大的數字 如果 k=0 就結束程式 Sample input: 6 8 15 29 35 47 77 3 2 4 1 0 Sample output: 第 3 大的數字是 35 第 2 大的數字是 47 第 4 大的數字是 29 第 1 大的數字是 77 39
hw2 寫一個 python 程式來判斷輸入字串有沒有雙回文 ( 回文 : aba / a)( 輸入字串不會有空白 ) ( 是不是兩個回文字串黏在一起 ) Sample input:abcbaacca Sample output: yes, abcbaacca = abcba + acca Sample input:abcbaca Sample output:no, abcbaca is not a double palindrome Hint: 1.Write a function to determine whether an input string is a palindrome. 2.What we mentioned in class. 40
python 與 C 的差異 不用打 ; input/print 預設是以整行為單位 變數不用宣告 變數名稱沒有綁住型態 while / if 用 : 結尾 while / if 用縮排排版 else if 變成 elif 沒有 i++ / i--, 要用 i += 1 / i -= 1 41
THE END 課堂有限, 學海無涯 自學才是王道! 42
參考資料與延伸學習 Python 官方教學,http://docs.python.org/3/tutorial/index.html Python 官方函式庫,http://docs.python.org/3.3/library/ Mark Lutz Python 學習手冊 OpenFoundry 活動,http://www.openfoundry.org/tw/activities Codecademy,http://www.codecademy.com Wiki 條目 Python,http://zh.wikipedia.org/wiki/Python TIOBE, http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html 43