基礎 python 程式設計

Size: px
Start display at page:

Download "基礎 python 程式設計"

Transcription

1 計概二 基礎 python 程式設計 吳維漢中央大學數學系

2 Python 1989 年底由荷蘭人 Guido van Rossum 開始建構 為解譯式程式語言 (interpreter) 2000 : python : python 年二月在 TIOBE 程式語言排名榜中為居於第五位 1 簡介

3 Why Python? 容易使用 (easy to use) 很快寫出來 (expressive) 可讀性高 (readable) 完備 (complete) 跨平臺 (cross-platform) 免費 (free) 2 簡介

4 Why not Python 執行效率不是很高 程式庫不是最多 編譯過程不會檢查變數型別 3 簡介

5 The Zen of Python 美麗優於醜陋 Beautiful is better than ugly. 明講優於暗喻 Explicit is better than implicit. 簡單比複雜好 Simple is better than complex. 複雜又比繁澀佳 Complex is better than complicated. 程式可讀性很重要 Readability counts. 4 簡介

6 Python 一些資料型別 整數 (integers): 0, -343, 浮點數 (floats): 3., 123e23, 0.1e-3 複數 (complex numbers):1+2j, -j, j 布林數 (booleans):true, False >>> x = 23 # x 為整數 >>> print(x) >>> x = 1+2j # 改為複變數 >>> print(x) 5 基本資料型別

7 mutable 與 immutable 型別 mutable : 資料可以被更動串列 (list), 集合 (set), 字典 (dictionary) immutable : 資料不可以被更動整數 (integer), 浮點數 (float), 複數 (complex), 字串 (string),... 6 mutable 與 immutable 型別

8 immutable 與儲存空間配置 # a 儲存空間內存 3 a >>> a = 3 3 # b 與 a 共用儲存空間 a b >>> b = a 3 # a 在新的儲存空間存 4, 舊空間為 b 使用 >>> a = b+1 a 4 b 3 7 immutable 型別

9 mutable 與儲存空間配置 # a 儲存 1, 2 兩個元素 >>> a = [ 1, 2 ] # b 與 a 共用儲存空間 >>> b = a # b 增加一個元素 3, 也影響 a >>> b.append(3) a 1 2 a b 1 2 a 1 2 b 3 # b 在新的儲存空間儲存 5 >>> b = [ 5 ] a b 5 8 mutable 型別

10 python 的資料型別 type 型別 M/I 樣式 int 整數 I 23, -234 float 浮點數 I 1.2, -234e-12 boolean 布林數 I True, False complex 複數 I 1-j, j str 字串 I "hello", "123" list tuple 串列元組 M I [ 2, "c", [ -2.2, "1+j" ] ] ( 1, ), ( "2", [ 2, "j" ] ) set frozenset 集合凍集合 M I { 12, "ab" }, set ( [1,2] ) frozenset ( [ 12, "ab"] ) dict 字典 M { "ncu":" 中大 ", " 中大 ":"ncu" } bytearray 位元組序列 M bytearray(b cat ) bytes 位元組序列 I b cat, b([99,97,116]) 9 基本資料型別

11 數學運算子 符號 運算子 範例 + - * / 加減乘除 3+4 = 7 % // ** += -= *= /= %= << >> < > <= >= ==!= 餘數運算 floor 除法指數運算加減... 複合運算位元左右位移比較大小 7%3 = 1 7//3 = 2, 7.5//3 = 2 3**2 = 9 a+=4 --> a = a + 4 1<<2 = 4, 7>>1 = 3 4>3, x <= X 10 運算子

12 整數 無位數限制 兩整數間的加, 減, 乘等計算結果皆為整數 使用 / 除法的計算結果為浮點數 使用 // 除法 (floor division) 的計算結果為整數 int("123") 可將字串轉為整數 int(123.45) 會浮點數的小數部份轉為整數 round(a) 使用偶數進位法 (round-to-even) round(2.5) = 2,round(1.5) = 2 11 整數

13 二進位, 八進位, 十六進位整數 0x11 --> 十六進位 11 0o11 --> 八進位 11 0b11 --> 二進位 11 將 a 整數轉為十六進位, 八進位, 二進位字串 hex(a), oct(a), bin(a) 將各種進位數字串轉為 10 進位數字 int( 0x11,16), int( 0o11,8), int( 0b11,2) 將各種進位數字串轉為 10 進位數字 int(hex(11),16) = 11, int(oct(11),8) = 11 int(bin(11),2) = 整數

14 浮點數 雙精確浮點數 有截去誤差 (round-off error) = round(2.15, 1) = 2.1 # 取到小數點後第一位 round(2.25, 1) = 2.2 # 取到小數點後第一位 不等於 2.16 實際十進位數字 >>> from decimal import Decimal >>> Decimal(0.1) Decimal( ) 13 浮點數

15 數字運算函式 abs, divmod, float, hex, max, min, oct, pow, oct, bin, round 使用 math 函式庫 包含 :sin, cos, tan, pi, exp, log10, log, pow, floor,... dir (math) 或 help (math) 取得用法 使用方式 : >>> import math >>> a = 2 * math.pi >>> print(a) >>> from math import * >>> b = 2 * pi >>> print(b) 14 數字運算

16 串列 :list 由排成一列的元素組成 元素可包含各種型別 :[ 1, "abc", 3+2j, 5.6 ] 串列元素也可以為串列 : >>> b = [ [ 1, 2-1j ], [ 3+2j, 4 ] ] 串列長度 :len ( 串列 ) len([1,"abc",5.6]) = 3 len([ [ 1, 2-1j ], [ 3+2j ] ]) = 2 空串列為 [] 15 串列

17 串列下標 ( 一 ) 串列元素可使用陣列下標取得 a = [ 1, " 中大 ", 3+2j, 5.6 ] 串列 a 正向下標逆向下標 1 " 中大 " 3+2j 則 a[1] = 1, a[-1] = 5.6, a[1][1] = " 大 " 二維串列 : >>> b = [ [ 1, 2-1j ], [ 3+2j, 4 ] ] 則 b[0][1] = 2-1j, b[1][1] = 4 16 串列

18 串列下標 ( 二 ) 若 a = [ 1, " 中大 ", 3+2j, 5.6 ] a[i:j] 取得 a[i], a[i+1],..., a[j-1] 間的 j-i 個元素 a[1:3] = [ 中大, (3+2j) ], a[0:2] = [ 1, " 中大 " ] a[i:] a[:j] 複製由 a[i] 到末尾的元素 複製由 a[0] 到 a[j-1] 的元素 a[i:j:k] 取得 a[i], a[i+k], a[i+2k],, 下標不超過 j-1 a[2:] = [ (3+2j), 5.6 ] a[0:] = [ 1, " 中大 ", 3+2j, 5.6 ] a[0:3:2] = 中大 17 串列

19 更改串列 ( 一 ) 若 a = [ 1, 中大, 3+2j, 5.6 ] 串列為 mutable: 可以自由變更串列元素 >>> a[2] = math >>> a [1, 中大, math, 5.6] 串列複製 >>> b = a[:] # b 與 a 為相同內容的兩串列 >>> a[2] = math >>> b b = [ 1, 中大, (3+2j), 5.6 ] 串列指定 >>> b = a # b 與 a 為同一串列 >>> a[2] = math >>> b [1, 中大, math, 5.6] 18 串列

20 更改串列 ( 二 ) 若 a = [ 1, 中大, 3+2j, 5.6 ] 更改串列部份元素 >>> a[1:3] = [ aa, bb, 8 ] >>> a [1, aa, bb, 8, 5.6] 接到末尾 >>> a[len(a):] = [ 7, 8 ] >>> a [ 1, 中大, (3+2j), 5.6, 7, 8 ] 放在前端 >>> a[:0] = [ 7, 8 ] >>> a [ 7, 8, 1, 中大, (3+2j), 5.6 ] 保留頭尾 >>> a[1:-1] = [] >>> a [ 1, 5.6 ] 19 串列

21 串列合成與元素複製 使用 + 合成兩串列 >>> a = [ 1, 2, 3 ] + [ 4, 5 ] >>> a [1, 2, 3, 4, 5] 使用 * 複製元素 >>> a = [ 1 ] * 3 >>> a [1, 1, 1] 可以合併使用 + * 製造串列 >>> a = [ 1 ] * 3 + [ 4, 5 ] * 2 >>> a [1, 1, 1, 4, 5, 4, 5] 20 串列

22 list 進階設定方式 使用 range 物件 : >>> list(range(4)) [0, 1, 2, 3] >>> list(range(1,4)) [1, 2, 3] >>> list(range(1,5,2)) [1, 3] list comprehension >>> [ x for x in range(4) ] [0, 1, 2, 3] >>> [ x*x for x in range(4) ] [0, 1, 4, 9] >>> [ [ x, x*x ] for x in range(4) ] [[0, 0], [1, 1], [2, 4], [3, 9]] >>> [ [ x, x*x ] for x in range(4) if x%2 ] [[1, 1], [3, 9]] 21 串列

23 list comprehension : 範例 >>> [ (x//2+1)*2 for x in range(8)] [2, 2, 4, 4, 6, 6, 8, 8] >>> [ [x,y] for x in range(3) for y in range(2)] [[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]] >>> # a 為 25 以下的合數, b 為 25 以下的奇數 >>> a = [j for i in range(2,6) for j in range(i*2,26,i)] >>> b = [ x for x in range(2,26) if x not in a ] >>> b [2, 3, 5, 7, 11, 13, 17, 19, 23] 22 串列

24 設定一到三維串列 設定 3 個元素一維串列 >>> a = [ None ] * 3 [None, None, None] None 為 python 的常數, 用來表示數值尚未設定 設定 3 2 個元素二維串列 >>> a = [ [None]*2 for x in range(3) ] [[None, None], [None, None], [None, None]] 設定 個元素三維串列 >>> a = [[[None]*4 for j in range(2)] for k in range(3)] [[[None, None, None, None], [None, None, None, None]],[[None, None, None, None], [None, None, None, None]],[[None, None, None, None], [None, None, None, None]]] 23 串列

25 二維串列 設定 3 2 個元素二維串列 正確方式 : 使用 list comprehension >>> a = [ [None]*2 for x in range(3) ] [[None, None], [None, None], [None, None]] >>> a[2][1] = 5 >>> a [[None, None], [None, None], [None, 5]] 錯誤方式 : 使用 * >>> b = [ [None]*2 ] * 3 [[None, None], [None, None], [None, None]] >>> b[2][1] = 5 >>> b [[None, 5], [None, 5], [None, 5]] NOTE: b 二維串列內的每個一維序列都使用同樣的記憶空間 24 串列

26 串列複製的空間配置 ( 一 ) 複製 immutable 物件 a = [1,3] b = a*2 = [1,3,1,3] 複製 mutable 物件 c = [1,3] d = [c]*2 = [[1,3],[1,3]] >>> a[0] is b[0] and a[0] is b[2] True >>> a[1] is b[1] and a[1] is b[3] True >>> a[0]=5 >>> a >>> b [5,3] b=[1,3,1,3] >>> c is d[0] and c is d[1] True >>> c.append(5) >>> c [1,3,5] >>> d [[1,3,5],[1,3,5]] 25 串列

27 串列複製的空間配置 ( 二 ) a = [1]*2 = [1,1] a 1 1 不同空間 b = [ [1,2] ]*3 = [ [1,2],[1,2],[1,2] ] c = [1,2]*3 = [1,2,1,2,1,2] b 相同空間 [1,2] [1,2] [1,2] 相同空間 c 各個佔有不同空間 26 串列

28 tuple tuple 為元素不可更動的 list tuple 使用小括號 >>> a = ( 3, x, 2.4 ) >>> a[1] x >>> a[0] = 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: tuple object does not support item assignment 一個元素的 tuple 須在其後加逗點 >>> (3+2,) (5,) >>> (3+2) 5 空 tuple:() 27 tuple 型別

29 tuple 設定方式 使用 tuple 將 list 換為 tuple: >>> a = [ 3, 4 ] >>> b = tuple(a) >>> b (3, 4) 使用 list comprehension 後轉型為 tuple >>> c = tuple( [ x*x for x in range(5) ] ) >>> c (0, 1, 4, 9, 16) 可使用 + 或 * 產生 tuple >>> a = (2,3) + (5,) >>> a (2, 3, 5) >>> b = b + ( x,) >>> b (2, 3, 5, x ) >>> (3,1) * 2 (3, 1, 3, 1) NOTE: 若 a 為 tuple, 也可使用 list(a) 產生串列 28 tuple 型別

30 tuple : 使用包裝設定資料 快速設定資料 : >>> ( one, two, three ) = ( 1, 2, 3 ) >>> three 3 >>> a = ( 4, 5, 6 ) >>> ( four, five, six ) = a >>> six 6 可省略括號 : >>> one, two = 1, 2 >>> two 2 >>> # 對調元素資料 >>> x, y = 3, 4 >>> x, y = y, x 29 tuple 型別

31 星號式子語法 星號式子語法 : >>> ( a, *b ) = ( 1, 2, 3, 4 ) >>> a 1 >>> b [2, 3, 4] >>> a, b (1, [2, 3, 4]) >>> *a, b, c = [ 1, 2, 3, 4 ] >>> a, b, c ([1, 2], 3, 4) NOTE:(1) 一個設定式子不可超過一個以上的星號 (2) 在指定式時, 星號語法僅能用來被設定 ( 指定左側 ) 30 tuple 型別

32 字串 使用單引號或雙引號夾住的文字 : abc, "abc", "Tom s" 多列字串使用三個引號 : """ 中央大學數學系 """ 字元序列 sequences of (utf-8) characters >>> x = abc 則 x[0]= a, x[1]= b, x[2]= c >>> y = 中央大學 則 y[0]= 中, y[1]= 央 >>> len(y) 字串長度 相等字元 : m, \155, \x6d 特殊字元 : \ 單引號 \\ 反斜線 \ooo: 八進位 ooo 字元 \" 雙引號 \b 退後字元 \xhh:16 進位 hh 字元 \n 換行字元 \t 定位字元 31 字串

33 基本字串處理 字串合成 : x = " 中央大學 " + " " + " 數學系 " 字串乘法 : x = " 貓 " * 3 = " 貓貓貓 " 分解為字元序列 : >>> list( 中央 math ) [ 中, 央, m, a, t, h ] 字元序列 : 字串 a 正向下標逆向下標 中央大學數學系 a[-1] = 系 ( 末尾元素 ) a[0] = a[-7] = 中, a[1] = a[-6] = 央... 字串為 immutable: 無法更改字串內容 >>> a = " 數學系 " >>> a[2] = " 人 " # 錯誤, 字串內容不能被更改 32 字串

34 字串處理 : 複製子字串 x[i:j] 可複製 x[i] 到 x[j-1] 之間的字元 >>> a = " 中央大學 math" 則 a[2:4] = 大學, a[4:-1] = mat a[i:] 複製由 a[i] 到末尾的字元 a[:j] 複製由 a[0] 到 x[j-1] 的字元 a[i:j:k] 取得 a[i], a[i+k], a[i+2k], 直到 a[j-1] a[4:] = math, a[0:] = a[:] = 中央大學 math a[0:3:2] = 中大 逆向複製子字串 a[-1:-5:-1] = htam a[-1::-1] = a[::-1] = htam 學大央中 字串長度 :len ( 字串 ) len(" 中央大學 math") = 7 33 字串

35 字串分解成字串串列 字串分解成字串串列 list( 字串 ) >>> list(" 中央大學 ") [ 中, 央, 大, 學 ] 字串.split( 分隔字串, 數量 ) >>> " 中 - 央 - 大 - 學 ".split("-") [ 中, 央, 大, 學 ] >>> " 中 - 央 - 大學 ".split("-",2) [ 中, 央, 大學 ] 字串.split() : 去除字串間的所有空格 >>> "a bc d".split() # a bc d 間有兩個空格 [ a, b, c ] >>> "a bc d".split(" ") # a bc d 間有兩個空格 [ a,, b,, c ] 34 字串

36 字串串列合併為字串 字串串列轉成字串 分隔字串.join( 串列 ) >>> "-".join([ 中, 央, 大, 學 ]) 中 - 央 - 大 - 學 >>> "".join([ 中, 央, 大, 學 ]) 中央大學 NOTE:.join( list( 字串 ) ) = 原字串 35 字串

37 字元在萬國碼的編碼位置 chr ( 字元 ) 與 ord ( 數字 ) >>> ord( a ) # a 在萬國碼 (unicode) 內的編碼位置 97 >>> chr(97) # 在萬國碼中編碼位置為 97 的字元 a >>> ord( 中 ) # 中 在萬國碼內的編碼位置 >>> chr(20013) # 在萬國碼編碼位置為 的字元 中 >>> [ ord(x) for x in " 中央 "] [20013, 22830] >>> "".join( [chr(x) for x in [20013, 22830]]) 中央 >>> [ chr(x+ord( a )) for x in range(5) ] [ a, b, c, d, e ] >>> "".join([ chr(x+ord( a )) for x in range(26)]) abcdefghijklmnopqrstuvwxyz 36 字串

38 字串數字轉字串 float( 字串數字 ) >>> float("3.14") 3.14 int( 字串數字, 進位方式 ) >>> int("24") 24 >>> int("12",8) # 八進位的 >>> int("12",6) # 六進位的 12 8 >>> int( ff,16) # 十六進位的 ff 字串

39 去除兩側的空白字元 空白字元 >>> import string # 輸入字串檔 >>> string.whitespace # 空白字元 \t\n\r\x0b\x0c foo 為字串 foo.strip() >>> x = " 中央大學數學系 " >>> x.strip() 中央大學數學系 >>> x.lstrip() 中央大學數學系 NOTE: 原 foo 字串不會變更 foo 去除前後兩側空白字元後存到新字串 foo.lstrip() foo 去除左側空白字元後存到新字串 foo.rstrip() foo 去除右側空白字元後存到新字串 38 字串

40 去除兩側多餘的字元 foo 與 bar 皆為字串 foo.strip(bar) 去除 foo 左右兩側在 bar 字串內的字元存到新字串 foo.lstrip(bar) 去除 foo 左側在 bar 字串內的字元後存到新字串 foo.rstrip(bar) 去除 foo 右側在 bar 字串內的字元後存到新字串 >>> x = " >>> x.strip("wt.") ncu.edu >>> y = x.rstrip("wt.") >>> y >>> x 39 字串

41 搜尋子字串的位置 若 foo 與 bar 為字串, 則 foo.find(bar) 在 foo 字串搜尋 bar 字串出現的下標位置 foo.find(bar,a,b) 在 foo[a:b] 內找尋 bar 字串出現的下標位置 foo.rfind(bar) 在 foo 字串搜尋 bar 字串所出現的最高下標位置 foo.rfind(bar,a,b) 在 foo[a:b] 找尋 bar 字串所出現的最高下標位置 >>> x = " >>> x.find("ncu") 4 >>> x.find("ncu",5) -1 >>> x.rfind("w") 13 NOTE: 若未搜尋, 則回傳 字串

42 計算子字串出現的次數 若 foo 與 bar 為字串, 則 foo.count(bar) 在 foo 字串找尋 bar 字串出現的次數 foo.count(bar,a,b) 在 foo[a:b] 內找尋 bar 字串出現的次數 >>> x = " >>> x.count("w") 4 >>> x.count("ww") 1 >>> x.count("ncu") 1 >>> x.count("ncu",5) 0 41 字串

43 檢查字串是否有起始或結束的子字串 若 foo 為字串, bar 為字串或為內含字串的 tuple, 則 foo.startswith(bar) foo.endswith(bar) 範例 : x = " 則 檢查 foo 字串是否有以 bar 字起始的子字串, 回傳 True 或 False 檢查 foo 字串是否有以 bar 字結束的子字串, 回傳 True 或 False foo.startswith(bar,a,b) 檢查 foo[a:b] 字串是否有以 bar 起始的子字串, 回傳 True 或 False foo.endswith(bar,a,b) 檢查 foo[a:b] 字串是否有以 bar 結束的子字串, 回傳 True 或 False x.startswith("www") ---> True x.startswith("ncu",3) ---> False x.startswith("ncu",4) ---> True x.endswith(("tw","cn")) ---> True x.endswith(("kr","cn","jp"))---> False 42 字串

44 逆轉字串 若 foo 為字串, 則 foo[::-1] >>> x = " 中央大學 " >>> x[::-1] 學大央中 >>> x[0:2][::-1] 央中 >>> x = "web1.nsc.gov.tw" >>> ".".join( [ y[::-1] for y in x.split(".") ] ) 1bew.csn.vog.wt.join(reversed(foo)) >>> x = " 數學系 " >>>.join(reversed(x)) 系學數 NOTE: reversed( 字串 ): 將字串逆轉存成字元迭代器 (iterator) 43 字串

45 取代字串內的子字串 若 foo, old, new 為字串, 則 foo.replace(old,new): 將 foo 內 old 全部改為 new >>> x = " >>> x.replace("ntu","ncu") >>> x.replace("w","z") zzz.ncu.edu.tz foo.replace(old,new,n): 將 foo 內前 n 個 old 全部改為 new >>> x = " >>> x.replace("w","y",3) yyy.ncu.edu.tw NOTE: 字串為 immutable 物件, 取代僅會產生新字串, 原有字串不會變更 44 字串

46 變更字串字母大小寫 若 foo 為字串, 則 foo.upper() 將 foo 內的字母轉成大寫 foo.lower() 將 foo 內的字母轉成小寫 foo.title() 將 foo 內的每一個字的第一個字母轉成大寫 >>> x = "My name is Tom." >>> x.upper() MY NAME IS TOM. >>> x.lower() my name is tom. >>> x.title() My Name Is Tom. 45 字串

47 判斷字串內容類型 若 foo 為字串, 則 foo.isdigit() 判斷 foo 是否全為數字 foo.isalpha() 判斷 foo 是否全為英文字母 foo.isalnum() 判斷 foo 是否全為英文字母或數字 foo.isupper() 判斷 foo 的字母是否全為大寫 foo.islower() 判斷 foo 的字母是否全為小寫 foo.islower() 判斷 foo 每個字的首個英文字母是否全為大寫 foo.isspace() 判斷 foo 是否全為空白字元 範例 : "123".isdigit() ---> True "123num".isalnum() ---> True "123num".isalpha() ---> False "My Age Is 23.".istitle() ---> True "The Year Is In 2010s.".istitle() ---> False 46 字串

48 format 格式輸出 使用 format 設定輸出格式 : >>> "{}/{}/{}".format( 1977,8,10) 1977/8/10 >>> "{2} 系 {1} 學院 {0} 大學 ".format(" 中央 "," 理 ", " 數學 ") 數學系理學院中央大學 format 的輸出為字串, 類似 C 語言的 sprintf: >>> a = {} 為 {} 元.format( apple,234) >>> a apple 為 234 元 47 格式輸出

49 字串的輸出格式 ( 一 ) 設定輸出位置 : {0},{1},,{n} >>> "{0} 大學 {1} 學院 {2} 系 ".format(" 中央 "," 理 "," 數學 ") 中央大學理學院數學系 >>> "{2} 系 {1} 學院 {0} 大學 ".format(" 中央 "," 理 "," 數學 ") 數學系理學院中央大學 設定各種輸出型式 : 寬度, 精度, 對齊方式等 : >>> "Total : {0:5} 元 ".format(234) Total : 234 元 >>> "Total : {0:#>5} 元 ".format(234) Total : ##234 元 >>> "Total : {0:#<7.2f} 元 ".format( ) Total : 23.46## 元 48 格式輸出

50 整數的輸出格式 ( 二 ) 填補字元, 對齊, 寬度 >>> "{0:#<7}{1:@>7}{2:*^8}".format(12345,67890,1345) 12345##@@67890**1345** NOTE : 填補字元在使用對齊時才能使用 進位方式 >>> "{0:#<4}-{0:08b}-{0:#>4x}".format(12) 12## ###c NOTE : b 二進位,o 八進位,x/X 16 進位 正負號, 對齊 >>> "{0:0=+5}".format(12) >>> "{0:#>+5} {0:#=+5}".format(12) ##+12 +##12 逗點 >>> {:#>20,}.format( ) #######1,234,567, 格式輸出

51 浮點數的輸出格式 小數點型式 : >>> {:>f}.format( ) # 預設小數點精度為 6 位 >>> {:#>7.2f}.format( ) # 以 7 格列印, 小數點後用 2 格 ##12.24 科學記號形式 >>> {0:e}{0:#>10.2e}{0:#>10.2E}.format( ) e+01##1.22e+01##1.22E+01 百分號形式 >>> {0:%}{0:#>.1%}{0:#>10.2%}.format(12.239) % 50 格式輸出

52 使用名稱參數輸出 使用名稱取代數字代號 : >>> {b} has {a:#>3} pts..format(a=99,b="tom") Tom has #99 pts. >>> x = "John" >>> {b} has {a:#>3} pts..format(a=2.3434,b=x) John has #99 pts. >>> {a:{f}{g}{x}.{y}f}.format(a=2.3434,f= #,g= >,x=5,y=2) # 格式輸出

53 列印資料 :print 使用 print() 來列印資料 >>> print("a","b") a b NOTE: a b 之間自動空一格, 印完後自動換行 設定各資料間的分離字串 >>> print("a","b","c",sep="--") a--b--c 設定列印後的自動輸出串 >>> print("a","b","c",sep="--",end="##") a--b--c## NOTE: 預設的自動輸出字串為 "\n" 52 輸出與輸入

54 讀取資料 ( 一 ) 使用 input 來讀取整行資料 : >>> x = input("-> ") -> hello kitty >>> x hello kitty NOTE:input ( 字串 ) 的字串為輸入的提示字串 讀取一筆數字 : >>> a = int(input(" 輸入 > ") # int 用來轉型 輸入 > 30 >>> b = int(input(" 輸入 > ") # int 用來轉型 輸入 > 25 >>> print(a+b) 輸出與輸入

55 讀取資料 ( 二 ) 讀取一串數字 : >>> a, b, c = eval(input("-> ") ) -> 1.1, 2, 4 # 須用逗點隔開 >>> a+b+c 7.1 NOTE: 用一行方式讀取各種資料時, 須用逗點分開 讀取運算式 : >>> a = eval(input("-> ")) -> [ x*x for x in range(1,6) ] >>> a [1, 4, 9, 16, 25] NOTE: eval 函式用來計算字串式子的值, 例如 : >>> eval("3+4*5") 23 >>> eval("[ x**3 for x in range(1,5)]") [1, 8, 27, 64] 54 輸出與輸入

56 讀取資料 ( 三 ) 分解輸入的字串成串列 : >>> a = input("-> ").split(" ") -> hello kitty >>> a [ hello, kitty ] >>> b = input("-> ").split(" ") > >>> b [ 2, 3, 4 ] 分解輸入的字串成數字串列 : >>> [ int(x) for x in input("-> ").split(" ") ] -> [2, 3, 4] 計算輸入數值的平方和 : >>> sum( [ int(x) for x in input("-> ").split(" ") ] ) -> 輸出與輸入

57 邏輯式子 ( 一 ) Boolean 真假值 : 假 :False, None, 0, 0.0, 0j, "", [], (), {} 真 :True, 非假 比較大小 : == ( 等於 ),!= ( 不等於 ), >, >=, <, <= and ( 而且 ), or ( 或 ), not ( 非 ): 邏輯運算 >>> a = 3 ; b = 5 >>> a > 4 and b < 5 False >>> not ( a < 4 or b > 5 ) False >>> ( a > 4 and a < 7 ) 可寫成 ( 4 < a < 7 ) NOTE : 同一列若有多個式子可用分號隔開 56 邏輯式子

58 邏輯式子 ( 二 ) x is y, x is not y: 比較 x 與 y 是否為 ( 不 ) 同物件 >>> a = 3 ; b = a >>> a is b True >>> b = 4 ; >>> a is b False >>> c = [ 2 ] ; d = c >>> d is c True >>> c.append(5) >>> d is c True 57 邏輯式子

59 邏輯式子 ( 三 ) a in b, a not in b: 比較 a 是否 ( 不 ) 在 b 之中 >>> a = "cat" >>> t in a True >>> x not in a True >>> c = range(4) >>> 4 in c False >>> e = [1, 3, 5] ; f = 4 >>> f in e False >>> f not in e True 58 邏輯式子

60 流程控制 ( 一 ) A if C else B: 如果 C 為真則回傳 A 否則回傳 B >>> x = -3 >>> x if x > 0 else -x 3 if: if x > 3 : print(x) if x > 3 : print(x) # 冒號結束 # 縮排 NOTE: (1) if 之後需加冒號結束條件式 (2) if 之後要執行的式子, 若僅有一行, 可直接寫在同一行 若超過一行, 則需以縮排方式寫在下面 59 流程控制

61 流程控制 ( 二 ) if-elif-else: if x > 10 : print("x > 10") elif 5 <= x <= 10 : print("x in [5,10]") elif x > 3 : print("x in (3,5)") else : print("x <= 3") NOTE: 若問題不需 elif 或 else 可以省略, 同時 elif 數量不受限制 60 流程控制

62 流程控制範例 : 一元二次方程式的根 from math import * while True : a, b, c = ( int(x) for x in input("> ").split(" ") ) delta = b*b - 4*a*c if delta > 0 : r1 = (-b + sqrt(delta))/(2*a) r2 = (-b - sqrt(delta))/(2*a) elif delta == 0 : r1 = r2 = -b/(2*a) else : r1 = complex(-b/(2*a),sqrt(-delta)/(2*a)) r2 = complex(-b/(2*a),-sqrt(-delta)/(2*a)) print( "{0} x^2 + {1} x +{2} :".format(a,b,c),r1,r2 ) 61 流程控制

63 while 迴圈 ( 一 ) 當 A 為真, 重複執行 B 直到 A 為假 while A : B A while A : B 假 B 真 # 計算一到十的和 sum, x, n = 0, 1, 10 while x <= n : sum += x x += 1 print(sum) 62 迴圈

64 while 迴圈 ( 二 ) # 產生十個介於 [1,5] 間亂數存於串列內 from random import * c, x = ( 0, [] ) while c < 10 : # randint(a,b) 產生介於 [a,b] 的整數亂數 x.append(randint(1,5)) c += 1 print(x) NOTE: 以上 c 也等於 [ randint(1,5) for x in range(10) ] 63 迴圈

65 for 迴圈 ( 一 ) 由 A 中一一取出元素名為 a 後執行 B 直到 A 的元素取完為止 for a in A : B for a in A : B # 印出高度為 5 的金字塔 n = 5 for x in range(1,n+1) : * *** ***** ******* ********* print( " "*(n-x), "*"*(2*x-1) ) 64 迴圈

66 for 迴圈 ( 二 ) for 與 if 交錯使用 # 印出高度為 7 的鑽石 n = 7 h = n//2 + 1 for x in range(1,n+1) : if x <= h : print( " "*(h-x),"*"*(2*x-1),sep="" ) else : * *** ***** ******* ***** *** * print( " "*(x-h),"*"*(2*(n+1-x)-1),sep="" ) 65 迴圈

67 多層迴圈 迴圈內也可有迴圈 # 印出高度為 7 的交叉圖形 n = 7 for i in range(1,n+1) : for j in range(1,n+1) : if i == j or i+j == n+1 : print("*",end="") else : print(" ",end="") print() * * * * * * * * * * * * * NOTE: 以上 if... else... 可改用以下一列方式 : print( "*" if i==j or i+j==n+1 else " ", end ="") 66 迴圈

68 提前跳出迴圈 :break ( 一 ) 使用 break 提前跳離迴圈 : for a in A : C if B : break while A : for b in B : if C : break D if d in B : break E NOTE: 多層迴圈的 break 僅會跳離一層迴圈 67 迴圈

69 提前跳出迴圈 :break ( 二 ) # 當輸入的數字和為奇數時才跳出迴圈 while True : a = [ int(x) for x in input("> ").split(" ") ] s = sum(a) print( a, " 和為 ", " 奇數 " if s%2 else " 偶數 ") if ( s%2 ) : break 68 迴圈

70 提前進入下一個迭代 :continue ( 一 ) 使用 continue 可提早進入下一個迭代 while True : if A : continue B while True : if not A : B NOTE:continue 僅是提早進入下一個迭代, 並非 跳離迴圈 69 迴圈

71 提前進入下一個迭代 :continue ( 二 ) while True : sum = 0 b = [] for a in [ int(x) for x in input(">").split()] : if a < 0 : continue # 當 a 為負數, 提前進入下個迭代 b.append(a) sum += a print( "sum of", b, ":", sum ) > sum of [2, 3, 4, 1] : 10 > sum of [2, 3, 2, 3] : 迴圈

72 基礎程式設計 抽象數學與邏輯思維的實體應用 山裡有山路轉彎, 高山流水響潺潺 深山百鳥聲聲叫, 路上行人步步難 勸君莫作雲遊客, 孤身日日在山間 人人說道華山好, 我道華山第八山 山山八裡山第有山華道轉路山好我彎高山華道說響水流山間人人潺潺深山在日日身聲聲鳥百雲遊客孤叫路上行作莫君勸難步步人 迴圈與條件式的交織結合 71 基礎程式設計

73 lambda 函式 : 簡易函式定義方式 lambda 函式 : # 單參數函數 : 平方 f = lambda x : x*x for x in range(5) print(f(x),end=" ") g = lambda x : (x,x+1,x+2) for x in range(3) print(g(x),end=" ") (0, 1, 2) (1, 2, 3) (2, 3, 4) # 多參數函數 : 九九乘法表 h = lambda x, y : x*y for a in range(1,10) : for b in range(1,10) : print("{0:4}x{1:1}={2:2}".format(a,b,h(a,b)),end="") print() 72 lambda 函式

74 enumerate 函式 : 列舉函式 enumerate(a): 將序列的下標與數值打包 >>> d = enumerate( ["a","b","c"] ) a b c (0, a ) (1, b ) (2, c ) for i, v in d : 或 print( i, :, v ) 0 : a 1 : b 2 : c for m in d : print(m[0], :,m[1]) 73 zip 函式

75 zip 函式 ( 一 ) : 拉鏈函式 zip(a,b,c...): 將多個一維序列合成多維序列 >>> a = list( zip( [1,2,3], ["a","b","c"] ) ) >>> a [(1, a ), (2, b ), (3, c )] A : B : a b c 1 a 2 b 3 c for x, y, z in zip( [1,2], ["a","b"], (5,"c")) : print(x,y,z) 1 a 5 2 b c 74 zip 函式

76 zip 函式 ( 二 ) : 拉鏈函式 zip(* 多維串列 ): 將多維序列拆解為多個一維序列 >>> [a,b] = zip( *[(1, a ),(2, b ),(3, c )] ) >>> a [1, 2, 3] >>> b [9, 8, 7] 星號置於串列前效果有如拆解串列成為一個個元素 zip( *[(1,2),(3,4)] ) zip( (1,2),(3,4) ) 1 a 2 b 3 c a b c a b 75 zip 函式

77 map 函式 ( 一 ) : 映射函式 map(fn,seq): 取出 seq 序列的元素送到 fn 函式處理 >>> a = list(map( len, [ "cat", "tiger", "lion" ] )) >>> a [3, 5, 4] "cat" "tiger" "lion" len >>> sum( map( int, [ "3", "5", "7" ] ) ) 15 NOTE: map 也可同時處理多個 seq 序列 76 map 函式

78 map 函式 ( 二 ) : 映射函式 map 常與 for 迴圈合用 : # 計算輸入數字的平方和 : sum = 0 for n in map( int, input("> ").split(" ") ) : sum += n*n print( sum ) > map 函式

79 map 函式 ( 三 ) : 映射函式 map 與 lambda 函式合用 : # 單一串列 : 攝式轉華式 for x, y in map( lambda c : ( c, 9*c/ ), range(0,101,10) ) ) : print( "{0:3} --> {1:3.0f}".format(x,y) ) 0 --> > > 212 # 多個串列 : 數字與字串合併 for x in map( lambda x, y : x+str(y),["a","b","c"],[4,5,6] ) ) ) print(x,end=" ") a4 b5 c6 78 map 函式

80 filter 函式 : 過濾函式 filter(fn,seq): 濾出 seq 序列滿足 fn 函式條件的元素 # 印出 5 的倍數 for a in filter( lambda x : x%5==0, range(1,51) ) : print("{0} ".format(a),end="") # 印出大於 0 的數 for a in filter( lambda x : x > 0, [1,3,-1,2,9] ) : print("{0} ".format(a),end="") filter 函式

81 reduce 函式 ( 一 ) reduce(fn,seq): 兩兩前後接續取出 seq 序列元素於 fn 函式內運算 # 計算 1 加到 10 的和 print( reduce( lambda x,y : x+y, range(11)) ) 55 NOTE: reduce 須使用 import functools 80 reduce 函式

82 reduce 函式 ( 二 ) # 在字母間加入橫線 print( reduce( lambda x,y : x+ - +y, " 數學系 " ) ) 數 - 學 - 系 # C 42 取 6 數量 print( reduce( lambda x,y : x*y, range(7,43) ) /reduce( lambda x,y : x*y, range(1,37) ) ) reduce 函式

83 sort 函式 ( 一 ) 排序函式 : A.sort(): 將 A 序列由小排到大 sorted(a): 回傳 A 序列由小排到大的結果, 但 A 序列不變 >>> a = [ 12, 76, 3 ] >>> sorted(a) [3, 12, 76] >>> a [12, 76, 3] >>> a.sort() >>> a [3, 12, 76] NOTE: tuple 僅能使用 sorted 排序 82 sort 函式

84 sort 函式 ( 二 ) 逆向排序 : A.sort(reverse=True) sorted(a,reverse=true) >>> a = [ 12, 76, 3 ] >>> a.sort(a,reverse=true) >>> a [76, 12, 3] >>> b = [ "cat", "ox", "tiger" ] >>> sorted( map(len,b), reverse=true ) [5, 3, 2] 83 sort 函式

85 sort 函式 ( 三 ) 自訂 lambda 函式排序 : A.sort(key=fn) sorted(a,key=fn) # 比較個位數字, 由小排到大 >>> sorted( [12,76,3], key=lambda x : x%10 ) [12, 3, 76] # 比較字串長度, 由大排到小 >>> sorted( ["cat","ox","tiger"], key=len,reverse=true) [ tiger, cat, ox ] 84 sort 函式

86 sort 函式 ( 四 ) 自訂函式排序方式 : # 比較日期 : 先年後月, 由小到大 def by_date1 (x) : s = x.split( / ) return ( int(s[1]), int(s[0]) ) # 比較日期 : 先月後年, 由小到大 def by_date2 (x) : s = x.split( / ) return ( int(s[0]), int(s[1]) ) a = [ 2/2010, 9/2010, 7/2009, 7/2008 ] print( sorted(a,key=by_date1) ) [ 7/2008, 7/2009, 2/2010, 9/2010 ] print( sorted(a,key=by_date2,reverse=true) ) [ 9/2010, 7/2009, 7/2008, 2/2010 ] 85 sort 函式

87 檔案處理 : 讀檔 ( 一 ) 檔案處理物件 : 負責處理讀 / 存檔的物件 設定讀檔物件 : # infile : 讀檔物件,fname: 檔名,r: 讀檔 infile = open("fname","r") # 由 infile 每次讀入一行存在 line for line in infile : # 去除每行後的空白字元後印出 print(line.strip()) # 關檔 infile.close() NOTE: (1) 關檔後,infile 可以再用來開啟其它的檔案 (2) open("fname","r") 也可寫為 open("fname") (3) 檔案資料讀取會一字不漏的讀取, 不會處理換行字元 86 檔案處理

88 檔案處理 : 讀檔 ( 二 ) 使用 with... as... # infile 物件負責將資料由 fname 檔讀入 with open("fname","r") as infile : # 由 infile 每次讀入一整行 for line in infile : # 讀入由逗點隔開的資料行 a = line.strip().split(",") print(" ".join(a)) NOTE: 在離開 with... as 後, 檔案會自動關閉 87 檔案處理

89 檔案處理 : 讀檔 ( 三 ) readline(): 一次讀取一行資料後回傳 readlines(): 讀取檔案剩下的資料轉成串列後回傳 read(n): 讀取 n 個位元組資料後回傳 read(): 將檔案剩下的資料串成一個字串後回傳 with open("aa","r") as infile : NOTE: # 讀取第一行並印出 print( infile.readline().rstrip() ) # 讀取第二行起的所有資料存入 lines 串列 lines = infile.readlines() for line in lines : # 印出每行的前 12 個字元 print(line[0:12]) (1) lines = infile.readlines() 也可寫成 lines =list(infile) (2) 當讀到檔案末尾時,readline() 回傳空字串 ("") 88 檔案處理

90 檔案處理 : 存檔 ( 一 ) 設定存檔物件 : # outfile : 存檔物件,fname: 檔名,w: 存檔 outfile = open("fname","w") # 儲存九九乘法表 : for i in range(1,10) : line = "" for j in range(1,10) : s = "{0:3} x {1:1} = {2:2}".format(i,j,i*j) line += s # 儲存一行, 後加上換行字元 outfile.write(line+"\n") # 關檔 outfile.close() NOTE: (1) 關檔後,outfile 可以再用來開啟其它的檔案 (2) write() 僅能存入字串 (3) 若將 w 改為 "a" 則表示輸出的資料將由檔案的末尾處寫入 89 檔案處理

91 檔案處理 : 存檔 ( 二 ) 使用 with... as # 同時開啟 aa 檔讀入資料與 bb 檔儲存資料 with open("aa","r") as infile, open("bb","w") as outfile : c = 1 for line in infile : # 讀入資料後加上行數輸出 out = "[{0:0>2}] {1}\n".format(c,line.strip()) outfile.write(out) c += 1 NOTE: (1) with... as 可以同時開啟數個檔案 (2) 在離開 with... as 後, 檔案會自動關閉 90 檔案處理

92 檔案處理 : 存檔 ( 三 ) writelines(a): 將序列字串 A 合併存入檔案 with open("aa","w") as f : # 印出 0 到 9 存到檔案 "aa", 每個數字一行 f.writelines( "{0}\n".format(n) for n in range(10)) # 也可使用 f.writelines( map( lambda n : str(n)+"\n",range(10) ) ) # 也可使用 for x in range(10) : f.write( str(x)+"\n" ) # 也可使用, 但末尾少了一個換行字元 f.write( "\n".join( map(str,range(10) ) ) ) 91 檔案處理

93 字典型別設定 ( 一 ) dictionary: 可使用非整數下標的串列 字典物件的設定 >>> # 定義 a 為空字典 >>> a = {} >>> # 可使用字串當下標 >>> a["one"] = 1 >>> a["two"] = 2 >>> a { two : 2, one : 1} >>> # 也可使用數字當下標 >>> b = { 1 : one } >>> b[2] = "two" >>> b[1.1] = "one point one" >>> b {1: one, 2: two, 1.1: one point one } NOTE: 字典物件使用前須先設定 92 dictionary 型別

94 字典型別設定 ( 二 ) 使用 dict comprehensions: >>> a = { x : x*x for x in range(1,5) } >>> a {1: 1, 2: 4, 3: 9, 4: 16} >>> # 對調 a 的 key 與 value 成為 b 字典 >>> b = { v : k for k, v in a.items() } 使用 dict 設定 : >>> # 此種設定方式的下標須為字串 >>> a = dict( one = 1, two = 2, three = 3 ) >>> a { three : 3, two : 2, one : 1} 使用 zip: >>> c = dict( zip( ("one","two"), (1,2) ) ) >>> c { two : 2, one : 1} 93 dictionary 型別

95 字典資料取用 如果 a = { "one" : 1, "two" : 2 } keys(): 取出所有的下標 ( 取出次序不定 ) for k in a.keys() : print( k, a[k] ) 輸出 : two 2 one 1 NOTE: a.keys() 也可使用 list(a) values(): 取出資料值 ( 取出次序不定 ) for v in sorted(a.values()) : # 對取出的值排序 print( v, end=" " ) 輸出 :1 2 items(): 取出成對的下標與資料值 ( 取出次序不定 ) for k, v in a.items() : print( k, v ) 輸出 : one 1 two 2 94 dictionary 型別

96 字典基本操作 ( 一 ) len(a): 回傳字典物件 A 儲存的筆數 >>> a = { "one" : 1, "three" : 3 } >>> len(a) 2 del(a[key]): 去儲字典 A 下標為 key 的資料 >>> del( a["one"] ) >>> a { three : 3} A.update(B): 將字典物件 B 內容加入 A >>> c = { "three" : 3, "four" : 4 } >>> a.update(c) >>> c { four : 4, three : 3, one : 1} 95 dictionary 型別

97 字典基本操作 ( 二 ) a in A: 判斷下標 a 是否在字典物件 A 內 >>> c = "three" : 3, "four" : 4 >>> "four" in c True >>> "five" in c False A.get(a,b=None): 找出在字典物件 A 內下標為 a 的值, 若 下標 a 不存在, 則回傳 b 值 >>> c.get("three") >>> 3 >>> c.get("three",5) >>> 3 >>> c.get("six") >>> c.get("six",6) >>> 6 96 dictionary 型別

98 複製字典物件 A.copy(): 複製 A 字典物件內容, 計憶空間相依 copy.deepcopy(a): 複製出記憶空間獨立的 A 物件內容 >>> c = [ 4, iv ] >>> d = { "one" : 1, "four" : c } >>> e = d.copy() >>> f = copy.deepcopy(d) >>> e { four : [4, iv ], one : 1} >>> del( c[1] ) >> e { four : [4], one : 1} >> f { four : [4, iv ], one : 1} NOTE: (1) 使用 copy.deepcopy(a) 型式, 須加入 import copy (2) deepcopy 也可用來複製有獨立計憶空間的序列物件 97 dictionary 型別

99 字典與串列互換 字典轉串列 : >>> a = { "one" : 1, "two" : 2 } >>> vals = list( a.values() ) >>> keys = list( a.keys() ) >>> items = list( a.items() ) >>> keys [2, 1] >>> items [( two, 2), ( one, 1)] 串列轉字典 : >>> b = [ ( "one", 1 ), [ "two", 2 ] ] >>> c = dict(b) >>> c { two : 2, one : 1} 98 dictionary 型別

100 字典範例 ( 一 ) : 單字數量 a = 一紙公文九個該, 一該該出是非來 從此該員該注意, 不該該處不該該 c = {} for x in a : c[x] = c.get(x,0) + 1 # 列印所有的資料 i = 0 for k in c.keys() : if ( k ==, or k == ) : continue i+=1 print(k,c[k],end= if i%5 else \n ) print() # 依照數值大小排序 i = 0 for k, v in sorted( c.items(), key= lambda x : x[1], reverse=true) : if ( k ==, or k == ) : continue i += 1 print(k,v,end= if i%5 else \n ) 員 1 一 2 此 1 文 1 來 1 從 1 個 1 不 2 公 1 是 1 該 9 處 1 意 1 注 1 紙 1 出 1 九 1 非 1 該 9 一 2 不 2 員此 1 文 1 來 1 從 1 個 1 公 1 是 1 處 1 意 1 注 1 紙 1 出 1 九 1 非 1 99 dictionary 型別

101 字典範例 ( 二 ) : 稀疏矩陣 # 使用二維下標當 key: from random import * x = list(range(81)) shuffle(x) # 設定 mat = { (0,1):2 } for c in range(10) : if ( x[c] == 1 ) : continue i = x[c]//9 j = x[c]%9 mat[(i,j)] = randint(1,9) # 依序列印 n = 0 for i, j in sorted(mat) : n += 1 print( (,i,,,j, ) =, mat[(i,j)], \ end= if n%4 else \n ) ( 0, 1 ) = 2 ( 1, 0 ) = 3 ( 1, 4 ) = 2 ( 2, 0 ) = 9 ( 5, 0 ) = 1 ( 5, 5 ) = 3 ( 5, 7 ) = 9 ( 6, 8 ) = 1 ( 7, 7 ) = 7 ( 8, 1 ) = 5 ( 8, 5 ) = dictionary 型別

102 set : 集合 集合 : 由不同元素所組成 集合設定 : >>> a = { 3, 2, 2} >>> a 2, 3 >>> b = set( [1,2,2,3] ) >>> b {1, 2, 3} 空集合 >>> x = set() >>> # 使用 clear() 取得空集合 >>> y = set([1,2]) >>> y.clear() 複製集合 >>> x = set([1,2]) >>> y = x.copy() 101 set 型別

103 set : 增減集合元素 add(a): 加入 a 元素 >>> b = set() >>> b.add(3) >>> b 3 remove(a): 刪除 a 元素 但集合若無此元素, 產生錯誤訊息 >>> b = set( [1,2,3] ) >>> b.remove(2) >>> b {1, 3} discard(a): 若集合有 a 元素則刪除, 否則沒動作 >>> b = set( [1,2,3] ) >>> b.discard(2) >>> b {1, 3} clear(): 清空集合 >>> b = set( (1,2) ) >>> b.clear() >>> b set() 102 set 型別

104 set : 是否為集合元素 a in A : 檢查 A 集合是否有 a 元素 >>> b = set( [1,2] ) >>> 2 in b True >>> 3 in b False a not in A : 檢查 A 集合是否沒有 a 元素 >>> b = set( [1,2] ) >>> 2 not in b False len(a) : A 集合的元素個數 >>> b = set( [1,5,2] ) >>> len(b) set 型別

105 set : 聯集, 交集, 差集, 對稱差集 ( 一 ) a = { 2, 3, 4 }, b = { 5, 3, 6 } : 聯集 >>> a b {2, 3, 4, 5, 6} & : 交集 >>> a & b {3} - : 差集 >>> a - b { 2, 4} >>> b - a { 5, 6} ^ : 對稱差集 >>> a ^ b { 2, 4, 5, 6 } 104 set 型別

106 set : 聯集, 交集, 差集, 對稱差集 ( 二 ) a = { 2, 3, 4 }, b = { 5, 3, 6 } c = { 3 } =: 聯集更新 >>> a = b >>> a {2, 3, 4, 5, 6} &=: 交集更新 >>> a &= b >>> a { 3 } =: 差集更新 >>> a -= b >>> a {2, 4} ^=: 對稱差集更新 >>> a ^= b >>> a {2, 4, 5, 6} 105 set 型別

107 set : 包含 a = { 2, 3, 4 }, b = { 5, 3, 6 }, c = { 3 } A >= B: A 集合是否包含 B 集合 A > B: A 集合是否包含 B 集合, 但 A 不等於 B A <= B: A 集合是否包含於 B 集合 A < B: A 集合是否包含於 B 集合, 但 A 不等於 B A == B: A 集合是否等於 B 集合 A!= B: A 集合是否不等於 B 集合 >>> a >= c True >>> a <= b False >>> b == c False A.isdisjoint(B): A B 兩集合是否無交集 >>> a.isdisjoint(b) False 106 set 型別

108 frozenset : 凍集合 frozenset: 集合建構後就不能被更動 >>> a = frozenset([1,2]) >>> a frozenset({1, 2}) >>> a.add(3) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: frozenset object has no attribute add frozenset 可成為其它集合的元素 >>> a = frozenset([1,2]) >>> b = set({3,a}) >>> b {frozenset({1, 2}), 3} 107 frozenset 型別

109 strings 與 bytes strings 與 bytes 為同筆資料儲存的不同型式 : strings bytes 型別 str bytes, bytearray 儲存內容 unicode text encoded unicode data 序列 character 介於 [0,255] 間的位元組 型式 cat 中 b cat b \xe4\xb8\xad str 轉 bytes x = 中 y = x.encode() x = 中 y = bytes(x, utf-8 ) y = bytearray(x, utf-8 ) bytes 轉 str x = y.decode() y= b \xe4\xb8\xad x = str(y, utf-8 ) y = b \xe4\xb8\xad NOTE: bytearray 為 bytes 的 mutable 版本型別 108 strings 與 bytes

110 bytes 與 bytearray 操作 可使用下標取得資料 : >>> x = "cat" >>> y = x.encode() >>> for i in range(len(x)) : print(x[i],y[i], end="-") c 99-d 97-t 116 可當成序列使用 : >>> y = bytearray( cat, utf-8 ) >>> for c in y : print(c,end=" ") 可使用 list 轉為數字串列 >>> list("abc".encode()) [97, 98, 99] 截取資料 : >>> y = " 中央 ".encode() >>> y[3:6] b \xe5\xa4\xae >>> y[3:6].decode() 央 >>> z = bytearray(" 中央 ", utf-8 ) >>> z[3:6] = " 大 ".encode() >>> z.decode() 中大 NOTE: bytes 為 immutable, 無法更動資料 109 bytes 與 bytearray

111 二進位檔案的儲存 二進位檔 : 檔案內容是以二進位方式儲存 存檔模式 : "wb" import struct with open("fname","wb") as outfile : # 字串可直接轉成 bytes 後寫入檔案 outfile.write(bytes(" 中央大學 ", utf-8 )) outfile.write("math DEPT".encode()) # 將兩個整數 n, n*n 與一個浮點數 n/10 打包成 # 2 個 int 與 1 個 double for n in range(10) : outfile.write(struct.pack("2id",n,n*n,n/10)) pack 打包參數 參數 C/C++ 型別 python 型別佔用位元組數 c char string of length 1 1 i int int 4 d double float 8 NOTE: 使用 pack 需要使用 struct package 110 二進位檔案

112 二進位檔案的讀檔 讀檔模式 : "rb" with open("fname","rb") as infile : # 中央大學 共佔 12 位元組 ncu = infile.read(12).decode() # 讀入 MATH DEPT math = infile.read(9).decode() print(ncu,math) for n in range(10) : # 讀取 16 個位元組資料, 拆解成 2 個 int # 與 1 個 double 存入 num tuple 內 num = unpack("2id",infile.read(16)) # 列印以上 3 個數字 print(" ".join(map(str,num))) NOTE: 使用 unpack 需要使用 struct package 111 二進位檔案

113 序列型別 :sequence 序列型別 : 資料如同串成一列的有次序排列 六種基本型別 : strings,byte sequence,byte arrays,lists,tuples,range objects 皆由更小的元素組成 : 序列型別樣式元素型別 str "cat" character (string of one element) bytes b"cat" int in [0,255] bytearray bytearray(b cat ) int in [0,255] list [ 2, "cat", [ 3, 2.5 ] ] any data type tuple ( "cat", ( 2+3j, 4 ) ) any data type range range(10) int 112 序列型別

114 序列型別 : 常用操作 ( 一 ) 整數下標取得資料 序列型別 x x[1] str "cat" a bytes b"cat" 97 bytearray bytearray(b cat ) 97 list [ 2, "cat", [ 3, 2.5 ] ] "cat" tuple ( "cat", ( 2+3j, 4 ) ) ( (2+3j), 4 ) range range(10) 1 下標截取 : 序列型別 x x[1:3] str "cat" at bytes b"cat" b at bytearray bytearray(b cat ) bytearray(b at ) list [ 2, "cat", [ 3, 2.5 ] ] [ "cat", [ 3, 2.5 ] ] tuple ( "cat", ( 2+3j, 4 ), 5 ) ( ( (2+3j), 4 ),5 ) range range(2,10,3) range(5,11,3) 113 序列型別

115 序列型別 : 常用操作 ( 二 ) S 為序列, x 為元素, n 為整數 len(s): 序列長度 max(s), min(s): 序列的最大值與最小值 ( 所有元素須相同型別 ) x in S, x not in S: 檢查 x 是否在 S 序列內 S.count(x): 回傳 x 元素在 S 序列出現的次數 S.index(x): 回傳 x 元素在 S 序列中第一次出現的下標 +, += 合成 : 序列合成, 對 immutable 序列無法使用 += >>> "ncu" + "math" ncumath x * n, n * x : 淺層複製元素 >>> 3 * "cat" catcatcat NOTE: range 物件無法使用最後兩種用法 114 序列型別

116 函式 函式語法 : def fname( arg1, arg2,... ) : body 函式範例 : def factorial(n) : # documentment string or docstring """ 計算 n 階乘 """ p = 1 for s in range(2,n+1) : p *= s return p (1)docstring: 以上三個雙 ( 單 ) 引號夾住的字串 ( 可跨行 ) 為函式 factorial 的說明文字, 可以使用 factorial.doc 印出來 (2) 使用 return arg 停止並離開函式, 回傳 arg 值 (3) 若無 return 則回傳 None 115 函式

117 函式參數設定 ( 一 ) 參數位置對應 : 依參數次序一一對應 def power( a = 10, n = 1 ) : p = a for s in range(2,n) : p *= a return p 函式參數設定計算結果說明 power(3,2) 9 a = 3, n = 2 power(3) 3 使用 a = 3, n = 1 power() 10 使用 a = 10, n = 1 預設值的設定方式, 由末尾逆向排列 # 錯誤的預設值設定 def power( a = 10, n ) : body 116 函式

118 函式參數設定 ( 二 ) 參數名稱對應 : 直接使用參數名稱傳遞數值 def age( byear, year = 2014 ) : return year byear 函式參數設定 計算結果說明 age(byear=2000) 14 byear = 2000, year = 2014 age(year=2015,byear=2002) 13 byear = 2002, year = 2015 age(year=2015) 錯誤 byear 不知 NOTE: 若以名稱對應方式傳入參數數值, 則參數的次序無影響 117 函式

119 函式 : 星號式子語法 星號式子語法 : >>> ( a, *b ) = ( 1, 2, 3, 4 ) >>> a 1 >>> b [2, 3, 4] >>> a, b (1, [2, 3, 4]) >>> *a, b, c = [ 1, 2, 3, 4 ] >>> a, b, c ([1, 2], 3, 4) NOTE:(1) 一個設定式子不可超過一個以上的星號 (2) 在指定式時, 星號語法僅能用來被設定 ( 指定左側 ) 118 函式

120 函式 : 不定數量的位置參數 使用星號式子語法 : def sum( n, *args ) : s = n for m in args : s += m return s 函式設定方式計算結果說明 sum() 錯誤 n 無設定 sum(2) 2 args = () sum(2,1,3) sum(2,1,3,8) 6 args=(1,3) 14 args=(1,3,8) 119 函式

121 函式 : 不定數量的關鍵字參數 將過多參數存入以 ** 表示的字典物件內 : def record( a, **b ) : print(a, " :") for k, v in b.items() : print(k,":",v) 函式設定方式輸出說明 record("tom") Tom : b 空字典 record("tom",age=20) Tom : age:20 record("tom",age=20,gender=male) Tom : age:20 gender: male 120 函式

122 函式 : 混合不定數量的位置與關鍵字參數 位置參數與關鍵字參數可以混合使用 : def record( a, *b, **c ) : print(a, " :", b) for k, v in c.items() : print(k,":",v) 函式設定方式輸出說明 record("tom") Tom : b (), c {} record("tom",183,50) Tom : (183, 50) c {} record("tom",age=20,gender=male) Tom : () age:20 gender:male record("tom",183,age=20,gender=male) Tom : (183) age:20 gender:male b () NOTE: 不定數量的混合型態參數較易出錯須仔細處理 121 函式

123 函式 : 傳入串列到個別參數 若要將串列傳入函式的個別獨立參數, 可用 * 號拆解 (unpack) 串列後對應到函式的個別參數 def sum( a, b ) : return a + b >>> sum(1,4) 5 >>> c = [1,4] >>> sum(*c) # a, b = c[0], c[1] 4 >>> sum(c) # 錯誤,sum 需要兩個參數 # 但僅輸入一個參數 122 函式

124 函式 : 傳入字典到個別參數 若要將字典傳入函式的個別參數, 可用 ** 符號拆解 (unpack) 字典物件對應到函式的個別參數 def sum( math, phy ) : return math + 2*phy >>> sum(1,4) 9 >>> c = { phy :1, math :4 } >>> sum(**c) # math, phy = 4, 1 6 >>> sum(c) # 錯誤,sum 需要兩個參數 # 但僅輸入一個參數 123 函式

125 函式 : 變更參數值 immutable 物件參數內容無法在函式內被更動, mutable 物件參數內容可在函式內被更改 def fn( n, s, a, b, c ) : # n 為整數, s 為字串, 其餘參數為串列 n += 1 s += "dog" a.append("dog") b = [ "dog" ] c[0] = "dog" print(n,s,a,b,c) >>> x=5 ; y=cat ; z=[1] ; d=[ cat, cat ] ; e=[ cat ] >>> print(x,y,z,d,e) 5 cat [1] [ cat, cat ] [ cat ] >>> fn(x,y,z,d,e) 6 catdog [1, dog ] [ dog ] [ dog ] >>> print(x,y,z,d,e) 5 cat [1, dog ] [ cat, cat ] [ dog ] NOTE: 函式內 a 與 c 運算是用來改變串列資料值, b 則是指向新的串列 124 函式

126 函式 : 局部, 全域變數 局部變數 (local variable): 函式內使用的介面參數與其內使用的變數名稱, 僅在內部使用, 不會向外影響 def fn1( a ) : # a 與 b 皆為函式的局部變數 a += 3 b = 1 print(a,b) >>> a = 2 >>> fn1(a) 5 >>> a 2 全域變數 (global variable): 不透過參數傳入, 直接使用函式外部的變數 def fn2() : # 函式直接使用外部的 a 變數 global a a += 3 print(a) >>> a = 2 >>> fn2(a) 5 >>> a 函式

127 可迭代器與迭代器 :iterable 與 iterator( 一 ) iterable( 可迭代器 ): 可用來迭代的物件, 例如 : 用在 for 或 while 迴圈, 或其它需要迭代的函式 for x in [1,4,9] : print(x) for x in "cat" : print(x) for x in range(10) : print(x) with open("fname","r") as infile : for line in infile : print(line) map( str, [1,4,9] ) 以上的串列 ([1,4,9]), 字串 ("cat"), range(10), infile 都是 iterable 使用 iter () 用來產生迭代器 (iterator) >>> itr1 = [1,4,9]. iter () >>> itr2 = iter([1,4,9]) # 也寫成左邊的型式 126 可迭代器與迭代器

128 可迭代器與迭代器 :iterable 與 iterator( 二 ) iterator( 迭代器 ): 物件定義如何迭代, 利用 next () 函式取得下一筆資料 >>> itr = [1,4,9] >>> itr. next () # 第一筆資料 1 >>> # next 為 next () 的簡化型式 >>> next(itr) 第二筆資料 4 當全部的資料取完後, 回傳 StopIteration 信號 >>> next(itr) 第三筆資料 9 >>> next(itr) 沒有資料了 Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration 不見得迭代器都需要回傳 StopIteration 信號 127 可迭代器與迭代器

129 可迭代器與迭代器 :iterable 與 iterator( 三 ) iterable 如同可用 迭代 方式取出的資料庫, iterator 為知道如何由 iterable 資料庫取出下筆資料的幫手 iterable 利用 iter 產生 iterator 幫手, iterator 使用 next 取出在 iterable 內的資料 a iit iterable itr=iter(a) itr iterator ii next(itr) 1 next(itr) 4 next(itr) 9 next(itr) StopIteration data source helper 128 可迭代器與迭代器

130 可迭代器與迭代器 :iterable 與 iterator( 四 ) 迴圈運作機置 : alist = [1,4,9] for x in alist : print(x) for 是為可迭代的迴圈, in 之後須要迭代器幫手來將 alist 的資料依次取出 執行 alist. iter () 或 iter(alist) 產生迭代器幫手 itr( 暫時命名 ), 因此 alist 為可迭代器 進入 for 迭代過程中, 重複執行 itr. next () 或 next(itr) 取得下筆資料, 每筆資料名稱為 x 當 alist 所有資料取完後, next(itr) 送出 StopIteration 信號代表終結, 迴圈結束 129 可迭代器與迭代器

131 產生器函式 :generator function( 一 ) generator function: 函式用來產生一種特殊型式的迭代器 generator 使用 yield 送出運算過程中的資料, 資料送出後函式仍繼續執行 # 產生初值 a, 比值 r 的前 n 項等比數列 def gparray( a, r, n ) : for i in range(n) : yield a a *= r for x in gparray(3,2,10) : print(x,end=" ") 輸出 : print( "->".join( map( str, gparray(2,3,5) ) ) ) 輸出 :2->6->18->54->162 NOTE:generator function 使用 yeild 送出運算過程中的資料, 函式仍繼續執行 但一般函式在使用 return 將其後的資料回傳後, 函式隨即中止 130 產生器

132 產生器函式 :generator function( 二 ) 使用 list(gen) 將產生器的所有資料轉成串列 a = list( gparray(2,3,5) ) print(a) 輸出 :[2, 6, 18, 54, 162] generator: 為迭代器, 可使用 next () 或 next(gen) 取得由 yield 回傳的資料 # 產生初值 a, 比值 r 的數列 def gparray( a, r ) : p = a while True : yield p p *= r itr = gparray(1,2) for i in range(5) : print(next(itr),end=" ") 輸出 : # 取出輸入產生器的前 n 個數值 def frontn( fn, n ) : for i in range(n) : yield next(fn) print( "->".join(map(str, frontn(gparray(1,2),8) ) ) ) 輸出 :1->2->4->8->16->32->64-> 產生器

133 第一類物件 :first-class object python 的函式屬於第一類物件, 有以下特性 : 可以被將函式當成參數傳入另一個函式中 def sq( f ) : return f(x)**2 可以在另一個函式中回傳出函式 可將函式設定於其它變數中 def sum( f, g ) : def fn(x) : return f(x)+g(x) return fn h = sum(sin,cos) print(h(0.1)) 可以存入資料結構中 fn = [ sin, cos, exp ] for x in range(10) : x /= 10 for f in fn : print(f(x),end=" ") print() NOTE: 同樣的, 所有的 python 的物件都屬於第一類物件 132 第一類物件

134 函式裝飾器 :function decorator decorator: 用來包裝函式的函式, 可用來擴增原有函式的功能 def decorate( fn ) : def wrapper(a,b,dx) : vals = [] x = a while x < b : vals.append(fn(x)) x += dx return vals return wrapper 以上 decorate 函式將輸入的函式 fn 包裹在其內的 wrapper 函式內,decorate 函式執行完後回傳 wrapper 包裹函式 wrapper 包裹函式的輸入參數為 (a,b,dx), 回傳 fn(x) 在 x 介於 [a,b), 等差數為 dx 之間的函數值, 以串列方式輸出 NOTE:decorate 與 wrapper 非程式保留字 133 函式裝飾器

135 函式裝飾器 : 使用方式 ( 一 ) decorate 函式回傳其內的 wrapper 函式, 若讓 g = decorate( abs ) # g 函式即為 : def g(a,b,dx) : vals = [] x = a while x < b : vals.append( abs(x) ) x += dx return vals # 因此 : print( g(-3,4,1) ) 輸出 :[3, 2, 1, 0, 1, 2, 3] 134 函式裝飾器

136 函式裝飾器 : 使用方式 ( 二 ) 使用方式 : # 平方函式 : def sq(x) : return x * x # 立方函式 : def cubic(x) : return x * x * x # 將 decorate 內部的 wrapper 函式傳出當成 sq series 函式 sq series = decorate(sq) # sq series 的參數列與 wrapper 包裹函式一樣 print( sq series(0,5,1) ) 輸出 :[0, 1, 4, 9, 16] # 將 decorate 內部的 wrapper 函式傳出當成 cubic series 函式 cubic series = decorate(cubic) # cubic series 的參數列與 wrapper 包裹函式一樣 print( cubic series(0,5,1) ) 輸出 :[0, 1, 8, 27, 64] 135 函式裝飾器

137 函式裝飾器 : 使用方式 ( 三 ) 可以直接取代舊函式 : def cubic(x) : return x * x * x # 包裹函式使用舊名稱, 等同擴大原始的函式功能 cubic = decorate(cubic) # 以下的 cubic 為包裹後的新函式, 需要 3 個參數 print( cubic(0,5,1) ) 輸出 :[0, 1, 8, 27, 64] 上面程式碼的前兩行可簡寫為 def cubic(x) : return x * x * x print( cubic(0,5,1) ) 輸出 :[0, 1, 8, 27, 64] 136 函式裝飾器

138 函式裝飾器 : 範例 # 列印不等數量的函式值 : def decorate( fn ) : def wrapper(a,b,dx,*arg) : print( {0:>8} ".format("x"),end=" ") for i in range(len(arg)) : name = arg[i]. name + "(" + fn. name + "(x))" print("{0:>14}".format(name),end=" ") print() x = a while x < b+0.1*dx : print("{0:>10.2f}".format(x),end=" ") for i in range(len(arg)) : print( "{0:>14.2f}".format(arg[i](fn(x))), end = " " ) print() x += dx return def twox(x) : return 2 * def fourx(x) : return 4 * x NOTE: 程式執行中 foo 函式的名稱可使用 foo. name 取得 137 函式裝飾器

139 函式裝飾器 : 執行結果 twox(0,0.5,0.1,sin,cos) x sin(twox(x)) cos(twox(x)) fourx(0,0.5,0.1,sin) x sin(fourx(x)) 函式裝飾器

数据结构与算法 - Python基础

数据结构与算法 - Python基础 Python 教材及课件 课件及作业见网址 xpzhang.me 1 1. Python 2. 3. (list) (tuple) 4. (dict) (set) 5. 6. 7. 2 Python Python 3 Python 4 Python 1, 100, -8080, 0,... 0x 0-9, a-f 0 xff00, 0 xa432bf 5 1.24, 3.14, -9.80,...

More information

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2 Chapter 02 變數與運算式 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.2 2.2.1 2.2.2 2.2.3 type 2.2.4 2.3 2.3.1 print 2.3.2 input 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 + 2.4.6 Python Python 2.1 2.1.1 a p p l e b e a r c 65438790

More information

CHAPTER VC#

CHAPTER VC# 1. 2. 3. 4. CHAPTER 2-1 2-2 2-3 2-4 VC# 2-5 2-6 2-7 2-8 Visual C# 2008 2-1 Visual C# 0~100 (-32768~+32767) 2 4 VC# (Overflow) 2-1 2-2 2-1 2-1.1 2-1 1 10 10!(1 10) 2-3 Visual C# 2008 10! 32767 short( )

More information

新・明解Python入門

新・明解Python入門 !=... 47, 49 "... 14 """... 14, 242, 266 #... 21 #... 30 %... 9 %... 152 %... 152 %=... 91 &... 124 &=... 91 '... 12, 14 '''... 14 ' main '... 296 'ascii'... 359 'cp932'... 359 'euc-jis-2004'... 359 'False'...

More information

Microsoft PowerPoint - C_Structure.ppt

Microsoft PowerPoint - C_Structure.ppt 結構與其他資料型態 Janet Huang 5-1 結構的宣告 struct 結構名稱 struct 結構名稱變數 1, 變數 2,, 變數 m; struct 結構名稱 變數 1, 變數 2,, 變數 m; student; student; 5-2 1 結構變數初值的設定 struct 結構名稱 struct 結構名稱變數 = 初值 1, 初值 2,, 初值 n student="janet","1350901",100,95

More information

Spyder Anaconda Spyder Python Spyder Python Spyder Spyder Spyder 開始 \ 所有程式 \ Anaconda3 (64-bit) \ Spyder Spyder IPython Python IPython Sp

Spyder Anaconda Spyder Python Spyder Python Spyder Spyder Spyder 開始 \ 所有程式 \ Anaconda3 (64-bit) \ Spyder Spyder IPython Python IPython Sp 01 1.6 Spyder Anaconda Spyder Python Spyder Python Spyder Spyder 1.6.1 Spyder 開始 \ 所有程式 \ Anaconda3 (64-bit) \ Spyder Spyder IPython Python IPython Spyder Python File

More information

Microsoft Word - ACG chapter00c-3ed.docx

Microsoft Word - ACG chapter00c-3ed.docx Python 好好玩, 趣學電玩遊戲程式設計 Python Python BASIC Java JavaScript PHP C++ BASIC Python Python Python Xbox PlayStation Nintendo - 2 - 簡介 : 互動式 Python Shell : 編寫程式 Python File editor : 猜數字 : 腦筋急轉彎 : 龍域 ( ) : 使用

More information

C/C++ - 函数

C/C++ - 函数 C/C++ Table of contents 1. 2. 3. & 4. 5. 1 2 3 # include # define SIZE 50 int main ( void ) { float list [ SIZE ]; readlist (list, SIZE ); sort (list, SIZE ); average (list, SIZE ); bargragh

More information

Excel VBA Excel Visual Basic for Application

Excel VBA  Excel Visual Basic for Application Excel VBA Jun5,00 Sub 分頁 () Dim i As Integer Dim Cname As String Dim Code As Variant Set score=thisworkbook.sheets("sheet") Code=Array(" 專北一 "," 專北二 "," 專北三 "," 專桃園 "," 專桃竹 "," 專中苗 ", " 專台中 "," 專台南 ","

More information

840 提示 Excel - Excel -- Excel (=) Excel ch0.xlsx H5 =D5+E5+F5+G5 (=) = - Excel 00

840 提示 Excel - Excel -- Excel (=) Excel ch0.xlsx H5 =D5+E5+F5+G5 (=) = - Excel 00 Excel - - Excel - -4-5 840 提示 Excel - Excel -- Excel (=) Excel ch0.xlsx H5 =D5+E5+F5+G5 (=) = - Excel 00 ( 0 ) 智慧標籤 相關說明提示 -5 -- Excel 4 5 6 7 8 + - * / % ^ = < >= & 9 0 (:) (,) ( ) Chapter - :,

More information

Microsoft Word - ACL chapter02-5ed.docx

Microsoft Word - ACL chapter02-5ed.docx 第 2 章神奇的質數 2.1.1 什麼是質數 1 1 1 打下好基礎 - 程式設計必修的數學思維與邏輯訓練 1 1 0 10 2 3 5 7 4 6 8 9 10 4 10000 1229 1000 168 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131

More information

C/C++ - 字符输入输出和字符确认

C/C++ - 字符输入输出和字符确认 C/C++ Table of contents 1. 2. getchar() putchar() 3. (Buffer) 4. 5. 6. 7. 8. 1 2 3 1 // pseudo code 2 read a character 3 while there is more input 4 increment character count 5 if a line has been read,

More information

C/C++ - 文件IO

C/C++ - 文件IO C/C++ IO Table of contents 1. 2. 3. 4. 1 C ASCII ASCII ASCII 2 10000 00100111 00010000 31H, 30H, 30H, 30H, 30H 1, 0, 0, 0, 0 ASCII 3 4 5 UNIX ANSI C 5 FILE FILE 6 stdio.h typedef struct { int level ;

More information

C/C++语言 - C/C++数据

C/C++语言 - C/C++数据 C/C++ C/C++ Table of contents 1. 2. 3. 4. char 5. 1 C = 5 (F 32). 9 F C 2 1 // fal2cel. c: Convert Fah temperature to Cel temperature 2 # include < stdio.h> 3 int main ( void ) 4 { 5 float fah, cel ;

More information

投影片 1

投影片 1 資料庫管理程式 ( 補充教材 -Part2) 使用 ADO.NET 連結資料庫 ( 自行撰寫程式碼 以實現新增 刪除 修改等功能 ) Private Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertButton.Click ' 宣告相關的 Connection

More information

untitled

untitled 1 Outline 數 料 數 數 列 亂數 練 數 數 數 來 數 數 來 數 料 利 料 來 數 A-Z a-z _ () 不 數 0-9 數 不 數 SCHOOL School school 數 讀 school_name schoolname 易 不 C# my name 7_eleven B&Q new C# (1) public protected private params override

More information

<4D F736F F D DA5BFA6A1C476C1C92DBEC7ACECB8D5A8F728B57BB35D292E646F63>

<4D F736F F D DA5BFA6A1C476C1C92DBEC7ACECB8D5A8F728B57BB35D292E646F63> 全國高級中等學校 106 學年度商業類科學生技藝競賽 程式設計 職種 學科 試卷 選手證號碼 ( 崗位編號 ): 姓名 : 注意事項 : 請將答案劃記於答案卡, 未依規定劃記者不予計分 試題說明 :( 選擇題共 25 題每題 4 分, 答錯不倒扣, 共 100 分 ) ( )1. 執行以下 Visual Basic 程式片段, 其結果為何?(A) 15 (B) 12 (C) 7 (D) 3 Dim

More information

Microsoft PowerPoint - Class2.pptx

Microsoft PowerPoint - Class2.pptx C++ 程式初探 II 2015 暑期 C++ 程式 II 大綱 1. 變數 2. 運算式 3. 輸出 4. 條件判斷 5. 迴圈 6. 陣列 2 基本變數型態 整數 位元組 浮點數 位元組 字元 位元組 short 2 float 4 char ( 整數 ) 1 int 2 (4) double 8 long 4 (8) long double 8(10) 位元組 整數値域 浮點數値域 準確度 1-128

More information

Microsoft PowerPoint - chap3

Microsoft PowerPoint - chap3 第三章基本輸出與輸入的方法 資訊科技系 林偉川 本章簡介 如何從鍵盤輸入資料以及從螢幕輸出結果, 是寫程式一個很基本的技巧, 因為這也是使用者與電腦交談的重要橋樑 在 C 語言函式庫中有不少輸出 / 入相關函式, 不過較常用到的也只有其中幾個 從螢幕輸出類 : 由鍵盤輸入類 : 2 1 從螢幕輸出類 printf(): 函數名稱取 print 以及 format 兩個字組成 此函式會將我們指定的字串以指定的格式輸出在螢幕上

More information

科学计算的语言-FORTRAN95

科学计算的语言-FORTRAN95 科 学 计 算 的 语 言 -FORTRAN95 目 录 第 一 篇 闲 话 第 1 章 目 的 是 计 算 第 2 章 FORTRAN95 如 何 描 述 计 算 第 3 章 FORTRAN 的 编 译 系 统 第 二 篇 计 算 的 叙 述 第 4 章 FORTRAN95 语 言 的 形 貌 第 5 章 准 备 数 据 第 6 章 构 造 数 据 第 7 章 声 明 数 据 第 8 章 构 造

More information

CC213

CC213 : (Ken-Yi Lee), E-mail: feis.tw@gmail.com 49 [P.51] C/C++ [P.52] [P.53] [P.55] (int) [P.57] (float/double) [P.58] printf scanf [P.59] [P.61] ( / ) [P.62] (char) [P.65] : +-*/% [P.67] : = [P.68] : ,

More information

3.1 num = 3 ch = 'C' 2

3.1 num = 3 ch = 'C' 2 Java 1 3.1 num = 3 ch = 'C' 2 final 3.1 final : final final double PI=3.1415926; 3 3.2 4 int 3.2 (long int) (int) (short int) (byte) short sum; // sum 5 3.2 Java int long num=32967359818l; C:\java\app3_2.java:6:

More information

C/C++程序设计 - 字符串与格式化输入/输出

C/C++程序设计 - 字符串与格式化输入/输出 C/C++ / Table of contents 1. 2. 3. 4. 1 i # include # include // density of human body : 1. 04 e3 kg / m ^3 # define DENSITY 1. 04 e3 int main ( void ) { float weight, volume ; int

More information

目次 CONTENTS 2 1 乘法公式與多項式 二次方根與畢氏定理 因式分解 一元二次方程式

目次 CONTENTS 2 1 乘法公式與多項式 二次方根與畢氏定理 因式分解 一元二次方程式 給同學的話 1 2 3 4 目次 CONTENTS 2 1 乘法公式與多項式 1-1 3 1-2 7 1-3 11 1 16 2 二次方根與畢氏定理 2-1 20 2-2 24 2-3 29 2 33 3 因式分解 3-1 37 3-2 41 3-3 45 3 49 4 一元二次方程式 4-1 53 4-2 57 4-3 61 4 65 3 1-1 乘法公式 本節性質與公式摘要 1 分配律 : ddd

More information

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 月 3 日 1 1 INPUTOUTPUT 1 InputOutput 题目描述 用 cin 输入你的姓名 ( 没有空格 ) 和年龄 ( 整数 ), 并用 cout 输出 输入输出符合以下范例 输入 master 999 输出 I am master, 999 years old. 注意 "," 后面有一个空格,"." 结束,

More information

C 語言—陣列及字串

C 語言—陣列及字串 10/16 系程主講人 : 荊輔翔 概論 陣列 陣列是一個具有索引 (index) 性質的連續資料儲存空間集合 陣列中每一個資料儲存空間稱之為陣列元素 (array element); 它們都具有相同的資料名稱 資料型態 及空間大小 ; 但存取它們時則須藉由索引 ( 或稱註標 ) 來區別辨識 索引代表資料在陣列中的相對位址 ( 其計數由 0 開始, 其餘累加類推 ), 且須由中括號 [ ] 涵蓋之

More information

WWW PHP Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2

WWW PHP Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2 WWW PHP 2003 1 Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2 Comments PHP Shell Style: # C++ Style: // C Style: /* */ $value = $p * exp($r * $t); # $value

More information

C/C++语言 - 运算符、表达式和语句

C/C++语言 - 运算符、表达式和语句 C/C++ Table of contents 1. 2. 3. 4. C C++ 5. 6. 7. 1 i // shoe1.c: # include # define ADJUST 7. 64 # define SCALE 0. 325 int main ( void ) { double shoe, foot ; shoe = 9. 0; foot = SCALE * shoe

More information

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不 1. 右 側 程 式 正 確 的 輸 出 應 該 如 下 : * *** ***** ******* ********* 在 不 修 改 右 側 程 式 之 第 4 行 及 第 7 行 程 式 碼 的 前 提 下, 最 少 需 修 改 幾 行 程 式 碼 以 得 到 正 確 輸 出? (A) 1 (B) 2 (C) 3 (D) 4 1 int k = 4; 2 int m = 1; 3 for (int

More information

PowerPoint Presentation

PowerPoint Presentation Python A Comprehensive Programming Language 謝育璘 r03944051@ntu.edu.tw Guido van Rossum Monty Python's Flying Circus 直譯 : 不用經過編譯就能執行.py 程式碼檔 (source file) 就是執行檔 (executable file) 不過系統要先安裝好 python 環境 3 直譯

More information

第5章修改稿

第5章修改稿 (Programming Language), ok,, if then else,(), ()() 5.0 5.0.0, (Variable Declaration) var x : T x, T, x,,,, var x : T P = x, x' : T P P, () var x:t P,,, yz, var x : int x:=2. y := x+z = x, x' : int x' =2

More information

Java 程式設計初階 第 5 章:基本輸出入 & 流程控制

Java 程式設計初階 第 5 章:基本輸出入 & 流程控制 Java 程式設計 標準輸出入與流程控制 本章大綱 標準輸出入 (Standard I/O) 分支 (Branch) if ~ else switch ~ case 迴圈 (Loop) for while do ~ while 中斷指令 break continue 總整理 標準輸出 定義 : 將資料印到螢幕上 Java 標準輸出指令 System.out.println( 資料 ) 將資料印出後換行

More information

Microsoft Word - 投影片ch03

Microsoft Word - 投影片ch03 Java2 JDK5.0 教學手冊第三版洪維恩編著博碩文化出版書號 pg20210 第三章變數與資料型態 本章學習目標認識變數與常數認識 Java 的基本資料型態學習如何進行資料型態轉換學習如何由鍵盤輸入資料 變數與資料型態 3-2 Java 的資料型態分為 : 與 原始資料型態 (primitive type) 非原始資料型態 (non-primitive type) 原始資料型態包括了整數與浮點數等型態

More information

Ps22Pdf

Ps22Pdf A B C D A B C D A B C D a a b c x x x x x x x x x x x x x x x x x a b c x a x x x x x x x x x x a b a b a b x x x x x x x x x x x x A B C A B C A B A B A x B C x D A B C a b c a b x x x x x x x A B A

More information

Microsoft PowerPoint - VB3

Microsoft PowerPoint - VB3 Visual Basic 6.0 & VB.NET 丙檢設計第一站 資科系林偉川 VB 之 for 指令 for 變數 = 初值 to 終值 step 增值多個指令 Next 中途離開用 if 指令判斷條件成立後 exit for Ex: Q=2, w=100, e=2, s=0 For i = q To w Step e s = s + i Next i 2 1 VB6 提供之內建函數 End 執行結束

More information

C/C++基礎程式設計班

C/C++基礎程式設計班 C/C++ 基礎程式設計 字元與字串 講師 : 張傑帆 CSIE, NTU 人的出身並不重要, 你拿時間來做什麼才重要 It s not who you were at birth that matters, but what you do with the time you are given.-steve Jobs 課程大綱 字元 字串 作業 字元 在電腦的世界裡, 所有的一切都是以 0 與 1

More information

碩命題橫式

碩命題橫式 一 解釋名詞 :(50%) 1. Two s complement of an integer in binary 2. Arithmetic right shift of a signed integer 3. Pipelining in instruction execution 4. Highest and lowest layers in the TCP/IP protocol suite

More information

Microsoft PowerPoint - OPVB1基本VB.ppt

Microsoft PowerPoint - OPVB1基本VB.ppt 大 綱 0.VB 能 做 什 麼? CH1 VB 基 本 認 識 1.VB 歷 史 與 版 本 2.VB 環 境 簡 介 3. 即 時 運 算 視 窗 1 0.VB 能 做 什 麼? Visual Basic =>VB=> 程 式 設 計 語 言 => 設 計 程 式 設 計 你 想 要 的 功 能 的 程 式 自 動 化 資 料 庫 計 算 模 擬 遊 戲 網 路 監 控 實 驗 輔 助 自 動

More information

四川省普通高等学校

四川省普通高等学校 四 川 省 普 通 高 等 学 校 计 算 机 应 用 知 识 和 能 力 等 级 考 试 考 试 大 纲 (2013 年 试 行 版 ) 四 川 省 教 育 厅 计 算 机 等 级 考 试 中 心 2013 年 1 月 目 录 一 级 考 试 大 纲 1 二 级 考 试 大 纲 6 程 序 设 计 公 共 基 础 知 识 6 BASIC 语 言 程 序 设 计 (Visual Basic) 9

More information

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new ListView 自訂排版 主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new int[]{r.drawable.dog1, R.drawable.dog2,

More information

FY.DOC

FY.DOC 高 职 高 专 21 世 纪 规 划 教 材 C++ 程 序 设 计 邓 振 杰 主 编 贾 振 华 孟 庆 敏 副 主 编 人 民 邮 电 出 版 社 内 容 提 要 本 书 系 统 地 介 绍 C++ 语 言 的 基 本 概 念 基 本 语 法 和 编 程 方 法, 深 入 浅 出 地 讲 述 C++ 语 言 面 向 对 象 的 重 要 特 征 : 类 和 对 象 抽 象 封 装 继 承 等 主

More information

新・解きながら学ぶJava

新・解きながら学ぶJava 481! 41, 74!= 40, 270 " 4 % 23, 25 %% 121 %c 425 %d 121 %o 121 %x 121 & 199 && 48 ' 81, 425 ( ) 14, 17 ( ) 128 ( ) 183 * 23 */ 3, 390 ++ 79 ++ 80 += 93 + 22 + 23 + 279 + 14 + 124 + 7, 148, 16 -- 79 --

More information

Microsoft PowerPoint - CH02 Introduction to C++ Programming_輸入與輸出 [相容模式]

Microsoft PowerPoint - CH02 Introduction to C++ Programming_輸入與輸出 [相容模式] Ch2. Introduction to C++ Programming 輸入與輸出 標準 I/O 課程名稱 : 程式設計 Computer Programming 班級 : 資管一 Freshman, ISMS 任課教師 : 謝明哲 Hsieh, Ming-Che, Miller 單位職稱 : 台東大學資管系副教授 Associate Professor, ISMS, NTTU 電子郵件 :hmz@nttu.edu.tw

More information

untitled

untitled 不 料 料 例 : ( 料 ) 串 度 8 年 數 串 度 4 串 度 數 數 9- ( ) 利 數 struct { ; ; 數 struct 數 ; 9-2 數 利 數 C struct 數 ; C++ 數 ; struct 省略 9-3 例 ( 料 例 ) struct people{ char name[]; int age; char address[4]; char phone[]; int

More information

Microsoft PowerPoint - Class5.pptx

Microsoft PowerPoint - Class5.pptx C++ 程式初探 V 2015 暑期 ver. 1.0.1 C++ 程式語言 大綱 1. 大量檔案讀取 & 計算 2. 指標 3. 動態記憶體 & 動態陣列 4. 標準函式庫 (STL) vector, algorithm 5. 結構與類別 2 大量檔案讀取 & 計算 若目前有一個程式將讀取純文字文件 (.txt) 中的整數, 並將該文件中的整數有小到大排序後, 儲存到另外一個新的純文字件中 假設有

More information

標題版面配置

標題版面配置 Computer programming and Data Science William Hsu Department of Computer Science and Engineering National Taiwan Ocean University Python 一種相對容易上手但難精的語言 2019/2/20 2 Python 簡介 Google 網站的搜尋系統 Youtube 視訊共享服務

More information

4

4 練習 9A ( 9. 特殊角的三角比 T ( 在本練習中, 不得使用計算機 如有需要, 答案以根式或分數表示. 試完成下表 三角比 θ 0 4 60 sin θ cos θ tan θ 求下列各數式的值 (. cos 60. sin 4 4. tan 4. cos0 4 tan 0 7. sin 4 cos 4 8. cos 60 tan 4 9. tan 60sin 0 0. sin 60 cos

More information

ACI pdf

ACI pdf 09 9.1 -...9-2 9.1.1...9-2 9.1.2...9-3 9.2 -...9-4 9.2.1 PMT - ()...9-4 9.2.2...9-6 9.3 -...9-8 9.3.1 PMT - ()...9-8 9.4...9-10 9.4.1... 9-11 9.4.2...9-12 9.4.3...9-14 9.5 -...9-17 9.5.1...9-18 1 Excel...9-21

More information

Microsoft PowerPoint - Lecture7II.ppt

Microsoft PowerPoint - Lecture7II.ppt Lecture 8II SUDOKU PUZZLE SUDOKU New Play Check 軟體實作與計算實驗 1 4x4 Sudoku row column 3 2 } 4 } block 1 4 軟體實作與計算實驗 2 Sudoku Puzzle Numbers in the puzzle belong {1,2,3,4} Constraints Each column must contain

More information

Microsoft PowerPoint - 06_迴圈2.pptx

Microsoft PowerPoint - 06_迴圈2.pptx 資料型別的選擇 如果資料或是運算過程可能會出現小數點 (e.g. BMI), 宜使用浮點數 (double, float) char 僅能儲存一個英文字 數字 或是英文中出現的標點符號等等鍵盤上可直接看得到 按得出的符號 若要儲存中文字, 目前建議使用 string 型別 A 和 A 的差別, 我們目前還沒辦法說得很詳細 只能說一個是字元 一個是字串 布林運算式的寫法 如果 x 等於, 就印出 Hello

More information

運算子多載 Operator Overloading

運算子多載 Operator Overloading 函數樣板 (Function Template) 與 類別樣板 (Class Template) 講師 : 洪安 1 資料結構與 C++ 程式設計進階班 為何需要通用函數? (1/2) int abs(int x) { return (x>0)?x:-x; 取名困難不好記 float fabs(float x) { return (x>0)?x:-x; complex cabs(complex x)

More information

Python 與中文處理

Python 與中文處理 Python 與中文處理 Tseng Yuen-Hsien, 曾元顯 資訊中心 國立臺灣師範大學 2011/10/27 目錄 Python 與中文處理... 1 Python 與中文能否相處得來?... 1 中文編碼 :Python 內部表達方式 程式檔案 螢幕輸出... 1 中文編碼 : 輸入檔案 輸出檔案... 4 什麼時候用 encode() 什麼時候用 decode()... 5 如何知道某一個字串

More information

WWW PHP

WWW PHP WWW PHP 2003 1 2 function function_name (parameter 1, parameter 2, parameter n ) statement list function_name sin, Sin, SIN parameter 1, parameter 2, parameter n 0 1 1 PHP HTML 3 function strcat ($left,

More information

單步除錯 (1/10) 打開 Android Studio, 點選 Start a new Android Studio project 建立專案 Application name 輸入 BMI 點下 Next 2 P a g e

單步除錯 (1/10) 打開 Android Studio, 點選 Start a new Android Studio project 建立專案 Application name 輸入 BMI 點下 Next 2 P a g e Android Studio Debugging 本篇教學除了最基本的中斷點教學之外, 還有條件式中斷的教學 條件式中斷是進階的除錯技巧, 在某些特定情況中, 我們有一個函數可能會被呼叫數次, 但是我們只希望在某種條件成立時才進行中斷, 進而觀察變數的狀態 而條件式中斷這項技巧正是符合這項需求 本教學分兩部分 單步除錯 (Page2~11, 共 10) 條件式中斷點 (Page12~17, 共 6)

More information

資料結構之C語言重點複習

資料結構之C語言重點複習 鏈結串列自編教材 ( 一 ) 本教材 ( 一 ) 目標問題 : 每次以亂數產生一 [0,1000] 之整數值, 若該值 >100, 則以同方式繼續產生下一亂數值, 若該值

More information

Microsoft PowerPoint - 02_運算.pptx

Microsoft PowerPoint - 02_運算.pptx 回顧 第一個程式 基本架構 五行必寫的公式 註解的寫法 cout

More information

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc References (Section 5.2) Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 15-16, 2010 H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 0 / 22 Fun Time (1) What happens in memory? 1 i n t i ; 2

More information

( ) Wuhan University

( ) Wuhan University Email: huangzh@whueducn, 47 Wuhan Univesity i L A TEX,, : http://affwhueducn/huangzh/ 8 4 49 7 ii : : 4 ; 8 a b c ; a b c 4 4 8 a b c b c a ; c a b x y x + y y x + y x x + y x y 4 + + 8 8 4 4 + 8 + 6 4

More information

4. 债 务 人 明 确 表 示 撞 行 拖 欠 的 债 务, 这 在 法 律 上 将 引 起 ( ) 人. 诉 讼 时 效 的 中 止 日. 诉 讼 时 效 的 中 黯 C. 诉 讼 时 效 的 延 长 D. 法 定 诉 讼 时 敷 黯 爵 的 改 变 5. 职 工 代 表 大 会 是 国 有 企

4. 债 务 人 明 确 表 示 撞 行 拖 欠 的 债 务, 这 在 法 律 上 将 引 起 ( ) 人. 诉 讼 时 效 的 中 止 日. 诉 讼 时 效 的 中 黯 C. 诉 讼 时 效 的 延 长 D. 法 定 诉 讼 时 敷 黯 爵 的 改 变 5. 职 工 代 表 大 会 是 国 有 企 试 卷 代 号 :2137 中 央 广 播 电 握 大 学 2010 2011 学 年 度 第 一 学 期 " 开 放 专 科 押 期 末 考 试 经 济 法 攘 论 试 题 2011 年 1 月 注 意 事 项 一 将 你 的 学 哥 姓 名 及 分 校 ( 工 作 站 } 名 称 填 写 在 答 题 纸 的 规 定 在 肉 考 试 销 束 后, 把 试 卷 和 答 黯 摇 撞 在 桌 上 这 卷

More information

Microsoft Word - 第3章.doc

Microsoft Word - 第3章.doc Java C++ Pascal C# C# if if if for while do while foreach while do while C# 3.1.1 ; 3-1 ischeck Test() While ischeck while static bool ischeck = true; public static void Test() while (ischeck) ; ischeck

More information

Java 程式設計入門

Java 程式設計入門 Java 程式設計入門 講師 : 陳昭源 CISE, NTU August 28, 2005 Outline 變數 Variables 運算子 Operators 運算式 (Expressions) 敘述(Statements) & 程式區塊 (Blocks) 流程控制 Control Flow Statements if-else statements switch statements August

More information

Microsoft Word - C-pgm-ws2010.doc

Microsoft Word - C-pgm-ws2010.doc Information and Communication Technology 資訊與通訊科技 Loops (while/for) C 廻路 姓名 : 班別 : ( ) CS C Programming #1 Functions 函數 : 1 若 n=14, 求以下表示式的值 Expressions 表示式 Value 值 Expressions 表示式 Value 值 A 20 2 * (n /

More information

Slide 1

Slide 1 Java 程式設計入門 講師 : 陳昭源 CSIE, NTU August 3, 2005 Outline Character & String Exercise August 3, 2005 Page 2 Character & String 處理字元資料時,Java 有三個類別可供使用 Character: 處理單一字元 String: 處理內容不改變 (immutable) 的字串 StringBuffer:

More information

PowerPoint プレゼンテーション

PowerPoint プレゼンテーション Perl CGI 1 Perl CGI 2 Perl CGI 3 Perl CGI 4 1. 2. 1. #!/usr/local/bin/perl 2. print "Content-type: text/html n n"; 3. print " n"; 4. print " n"; 3. 4.

More information

Microsoft Word - 09.數學136-281.docx

Microsoft Word - 09.數學136-281.docx 136. 計 算 梯 型 面 積 (1 分 ) 請 以 JAVA 運 算 式 計 算 下 面 梯 形 面 積, 並 輸 出 面 積 結 果 梯 形 面 積 公 式 為 :( 上 底 + 下 底 ) 高 2 每 一 組 依 序 分 別 輸 入 梯 形 的 上 底 下 底 及 高 的 整 數 輸 出 梯 形 面 積 輸 入 輸 出 94 190 120 99 54 47 137. 計 算 三 角 形 面

More information

C 1

C 1 C homepage: xpzhangme 2018 5 30 C 1 C min(x, y) double C // min c # include # include double min ( double x, double y); int main ( int argc, char * argv []) { double x, y; if( argc!=

More information

0 0 = 1 0 = 0 1 = = 1 1 = 0 0 = 1

0 0 = 1 0 = 0 1 = = 1 1 = 0 0 = 1 0 0 = 1 0 = 0 1 = 0 1 1 = 1 1 = 0 0 = 1 : = {0, 1} : 3 (,, ) = + (,, ) = + + (, ) = + (,,, ) = ( + )( + ) + ( + )( + ) + = + = = + + = + = ( + ) + = + ( + ) () = () ( + ) = + + = ( + )( + ) + = = + 0

More information

C C

C C C C 2017 3 8 1. 2. 3. 4. char 5. 2/101 C 1. 3/101 C C = 5 (F 32). 9 F C 4/101 C 1 // fal2cel.c: Convert Fah temperature to Cel temperature 2 #include 3 int main(void) 4 { 5 float fah, cel; 6 printf("please

More information

C

C C 2017 3 14 1. 2. 3. 4. 2/95 C 1. 3/95 C I 1 // talkback.c: 2 #include 3 #include 4 #define DENSITY 62.4 5 int main(void) 6 { 7 float weight, volume; 8 int size; 9 unsigned long letters;

More information

C/C++基礎程式設計班

C/C++基礎程式設計班 C/C++ 基礎程式設計 字元與字串 講師 : 張傑帆 CSIE, NTU 人的出身並不重要, 你拿時間來做什麼才重要 It s not who you were at birth that matters, but what you do with the time you are given.-steve Jobs 課程大綱 字元 字串 作業 字元 在電腦的世界裡, 所有的一切都是以 0 與 1

More information

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F 1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET 2.0 2.0.NET Framework.NET Framework 2.0 ( 3).NET Framework 2.0.NET Framework ( System ) o o o o o o Boxing UnBoxing() o

More information

<4D F736F F D B0D3B77EC3FEA7DEC3C0C476C1C9A5BFA6A1B8D5C3442DB57BA6A1B35DAD702DBEC7ACEC2E646F6378>

<4D F736F F D B0D3B77EC3FEA7DEC3C0C476C1C9A5BFA6A1B8D5C3442DB57BA6A1B35DAD702DBEC7ACEC2E646F6378> 全國國高級中中等學校 105 學年度商商業類學學生技藝藝競賽 程式式設計 職職種 學學科 試試卷 崗位位編號 : 姓名 : 注意事項 : 請將答案案劃記於答案案卡, 未依依規定劃記者者不予計分分 試題說明 :( 選擇題每每題 4 分, 共 100 分 ) ( )1. 執行以下 Visual Basic 程式片段, 其結果為何?(A) 15 Dim i As Byte i = &HFC Console.WriteLine(Not

More information

PowerPoint Presentation

PowerPoint Presentation Visual Basic 2005 學 習 範 本 第 7 章 陣 列 的 活 用 7-1 陣 列 當 我 們 需 要 處 理 資 料 時, 都 使 用 變 數 來 存 放 資 料 因 為 一 個 變 數 只 能 代 表 一 個 資 料, 若 需 要 處 理 100 位 同 學 的 成 績 時, 便 要 使 用 100 個 不 同 的 變 數 名 稱, 這 不 但 會 增 加 變 數 名 稱 命 名

More information

三維空間之機械手臂虛擬實境模擬

三維空間之機械手臂虛擬實境模擬 VRML Model of 3-D Robot Arm VRML Model of 3-D Robot Arm MATLAB VRML MATLAB Simulink i MATLAB Simulink V-Realm Build Joystick ii Abstract The major purpose of this thesis presents the procedure of VRML

More information

投影片 1

投影片 1 NCKU Progrmming Contest Trining Course 08/05/09 Jheng-Hung Hong Deprtment of Computer Siene nd Informtion Engineering Ntionl Cheng Kung University Tinn, Tiwn NCKU CSIE Progrmming Contest Trining Course

More information

lnag_ch_v2.01.doc

lnag_ch_v2.01.doc 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. % Any line starting with "%" is a comment. % "\" (backslash) is a special Latex character which introduces a Latex %

More information

PowerPoint 簡報

PowerPoint 簡報 本周未安排實作輔導 預定 : 下周六 迴圈 LOOP 應用 判斷質數 (Prime number) 求兩個整數的最大公因數 (greatest common divisor, GCD) 判斷迴文 (palindrome) 搶答!! Q1 : 印出結果? int s,x; s=0; for(x=1;x

More information

Microsoft Word - Delta Controller ASCII_RTU_TC

Microsoft Word - Delta Controller ASCII_RTU_TC Delta Controller ASCII/RTU ( 適用台達變頻器 伺服驅動器 PLC 溫度控制器 ) 人機預設值通訊速率 :9600, 7, None, 2 (ASCII); 9600, 8, None, 2 (RTU) 控制器站號 :1 控制區 / 狀態區 :None/None 控制器接線的說明 Delta Servo a. RS-232(DOP-A/AE/AS, DOP-B 系列適用 )

More information

投影片 1

投影片 1 v. s. Character ( ) ( ) ( ) ( ) ( ) (1949 ) ( ) (1949-1964 ) ( )X (1965-1979) Snoopy ( )Y (1977-1984) ( ) A 34.3% A 28.1 A 1. ( ) A 2. ( ) A 3. ( ) 4. ( ) ( ) Hello Kitty 60% 1. 2. Kitty ( ) 1. ( ) KTV

More information

p.2 1. a. b. c. (i) (ii)

p.2 1. a. b. c. (i) (ii) p.1 p.2 1. a. b. c. (i) (ii) p.3 2. 1840 1860 1925 a. : b. : ( ) ( ) p.4 3. 1937 12 17 1990 12 31 12 19 12 28 12 28 p.5 a. i. ii. b. c. d. () p.6 4. 1918 2 26 3 614 2000 a. b. / p.7 5. 1 2 p.8 a. 1 2 2

More information

C/C++ 语言 - 循环

C/C++ 语言 - 循环 C/C++ Table of contents 7. 1. 2. while 3. 4. 5. for 6. 8. (do while) 9. 10. (nested loop) 11. 12. 13. 1 // summing.c: # include int main ( void ) { long num ; long sum = 0L; int status ; printf

More information

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc 2 5 8 11 0 13 1. 13 2. 15 3. 18 1 23 1. 23 2. 26 3. 28 2 36 1. 36 2. 39 3. 42 4. 44 5. 49 6. 51 3 57 1. 57 2. 60 3. 64 4. 66 5. 70 6. 75 7. 83 8. 85 9. 88 10. 98 11. 103 12. 108 13. 112 4 115 1. 115 2.

More information

######## First set of commands x <- 0.5; y <- 0 if (x>3) y <- 1 else y <- 2 ######## Second set of commands x <- 0.5; y <- 0 if (x>3) y <- 1 else ###

######## First set of commands x <- 0.5; y <- 0 if (x>3) y <- 1 else y <- 2 ######## Second set of commands x <- 0.5; y <- 0 if (x>3) y <- 1 else ### 流程控制 : if, for, while, repeat Textbook reading: Chapter 7. 條件執行 :if 指令或 if-else 指令. 當條件 A 為 TRUE 時, 執行命令 C 的語法為 if ( A ) C 當條件 A 為 TRUE 時執行命令 C, 否則執行命令 D 的語法為 if ( A ) C else D A simple example. x

More information

. (A) (B) (C) A (D) (E). (A)(B)(C)(D)(E) A

. (A) (B) (C) A (D) (E). (A)(B)(C)(D)(E) A . () () () () () (A) (B) (C) B (D) (E). (A) (B) (C) E (D) (E) (A) (B) (C) (D). () () () () E (A) (B) (C) (D) (E). C (A) (B) (C) (D) (E). (A) (B) (C) (D) D (E). () - () - () - () - () - D (A) (B) (C) (D)

More information

Microsoft PowerPoint - CH07 Arrays and Vectors [相容模式]

Microsoft PowerPoint - CH07 Arrays and Vectors [相容模式] CH7 陣列與向量 Array and Vectors 課程名稱 : 資管一程式設計任課教師 : 謝明哲單位職稱 : 台東大學資管系副教授電子郵件 :hmz@nttu.edu.tw hmz@nttu.edu.tw 2016 1 Outline 什麼是陣列? 陣列的運用 排序方式 多維陣列 hmz@nttu.edu.tw 2016 2 什麼是陣列? hmz@nttu.edu.tw 2016 3 陣列

More information

02

02 Thinking in C++: Volume One: Introduction to Standard C++, Second Edition & Volume Two: Practical Programming C++ C C++ C++ 3 3 C C class C++ C++ C++ C++ string vector 2.1 interpreter compiler 2.1.1 BASIC

More information

Microsoft Word C-A卷.docx

Microsoft Word C-A卷.docx 100 學年度資訊學院程式設計會考 (C) 101/05/5 題組 :A 選擇題及填充題, 請在答案卡上作答, 實作題請填寫於答案卷上, 並於實作題上方填寫班級 姓名 學號 一 選擇題題目 1. unsigned char 的最大值 (a) 127 (b) 255 (c) 512 (d) 1023 2. 下列何者為正確的變數名稱? (a) Android (b) C++ (c) I Phone (d)

More information

PowerPoint 簡報

PowerPoint 簡報 Python 介紹 課程目的 本章節的目的在於對 Python 語言進行基本的簡介, 讓同學對於 Python 語法有基礎的認知, 以方便後續實驗的進行 本章節的適用對象為不熟悉 Python 語法, 但曾學習過其他程式語言, 如 C, C++, Java, C# 等 Outline Python 簡介 安裝 Python 資料型態 變數與運算子 語法與函式 模組 認識 Python Python

More information

第二章

第二章 中 国 建 筑 股 份 有 限 公 司 内 部 控 制 手 册 (2009 年 版 ) 目 录 第 一 章 总 则 1 编 制 内 部 控 制 手 册 目 的 依 据 1 2 内 部 控 制 手 册 的 适 用 范 围 1 3 内 部 控 制 基 本 原 则 1 4 内 部 控 制 的 基 本 要 求 2 5 内 部 控 制 组 织 机 构 4 6 内 部 控 制 手 册 的 更 新 与 监 督 4

More information

運算子多載 Operator Overloading

運算子多載 Operator Overloading 多型 Polymorphism 講師 : 洪安 1 多型 編譯時期多型 ( 靜態多型 ) function overloading 如何正確呼叫同名的函數? 利用參數個數與型態 operator overloading 其實同 function overloading 執行時期多型 ( 或動態多型 ) 如何正確呼叫不同物件的相同名稱的成員函數 利用繼承與多型 2 子類別與父類別物件間的指定 (assignment)

More information

Learn_Perl 3-02.pdf

Learn_Perl 3-02.pdf 2 2. 1 h e l l o h e l l o 23 2 4 2.2 2.2.1 2.2.2 d o u b l e 1 e - 1 0 0 1 e 1 0 0 i n t e g e r 2 5 1.25 2.000 3.0 7.25e45 # 7.25 10 45-6.5e24 # 6.5 10 24 # -12e-24 # 12 10-24 # -1.2E-23 # -- E 2.2.3

More information

<5B BECBB0EDB8AEC1F25D312D34B0AD5FC3E2BCAEBCF6BEF7C0DAB7E F31702E504446>

<5B BECBB0EDB8AEC1F25D312D34B0AD5FC3E2BCAEBCF6BEF7C0DAB7E F31702E504446> : 2 = 3 4? 0 an ordered set of unambiguous, executable steps that produces a result and terminates in a finite time (computational theory) ( ) 5 6 (C-) int min, max; float degree, b; char ch, token; /,,,

More information

coverage2.ppt

coverage2.ppt Satellite Tool Kit STK/Coverage STK 82 0715 010-68745117 1 Coverage Definition Figure of Merit 2 STK Basic Grid Assets Interval Description 3 Grid Global Latitude Bounds Longitude Lines Custom Regions

More information

Historical Fund Prices_TC_mt_2017.pdf

Historical Fund Prices_TC_mt_2017.pdf 1. (i) (ii) 2. 5 1 3. 4. 5. 65 65 / 6. 7. / 8. 03/04/2017 19.1857 17.7658 16.8445 13.6299 11.6134 15.8544 20.1994 15.5516 7.3412 19.6477 9.6339 12.8183 11.3199 10.0279 12.8949 13.6338 10.0000 10.0000 05/04/2017

More information

電腦做什麼事~第七章

電腦做什麼事~第七章 Batteries included 1 built-ins #-*- coding: UTF-8 -*- from getpass import getpass data = {"kaiching":"0000"} def hello(name): " ", name, " " name = raw_input(" ") word = getpass(" ") if data.has_key(name):

More information

CC213

CC213 : (Ken-Yi Lee), E-mail: feis.tw@gmail.com 9 [P.11] : Dev C++ [P.12] : http://c.feis.tw [P.13] [P.14] [P.15] [P.17] [P.23] Dev C++ [P.24] [P.27] [P.34] C / C++ [P.35] 10 C / C++ C C++ C C++ C++ C ( ) C++

More information

= 3 + 1 7 = 22 7 3.14 = 3 + 1 7 + 1 15 +1 = 355 3.1415929 113 221221221221 136136136136 221000000000 221000000 221000 221 = 136000000000 136000000 136000 221 1000000000 1000000 1000 1 = 136 1000000000

More information

C/C++ - 字符串与字符串函数

C/C++ - 字符串与字符串函数 C/C++ Table of contents 1. 2. 3. 4. 1 char C 2 char greeting [50] = " How " " are " " you?"; char greeting [50] = " How are you?"; 3 printf ("\" Ready, go!\" exclaimed John."); " Ready, go!" exclaimed

More information

6CO2 6H 2O C6H 2O6 6O2 = = n i= P X i n H X - P X logp X i= i n X X X P i P 0 i l n i n n P i= i H X = - p log P n i= i i i + H X - P X logp X dx - 2 2 2 2 3 2 4 2 d( Q) d( Q) > 0 = 0 di di d(

More information