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 Fruit Animal a p p l e b e a r c 2-2
02 2.1.2 Python = score 80 score = 80 Python score (int) fruit = " " #fruit a b c 20 a = b = c = 20,, age 18 name age, name = 18, " " del score del score 2-3
Python 2.1.3 Python Python Python acos and array asin assert atan break class close continue cos Data def del e elif else except exec exp fabs float finally floor for from global if import in input int is lambda log log10 not open or pass pi print raise range return sin sqrt tan try type while write zeros Python 3.x 7eleven George&Mary George Mary if & Python 2-4
02 2.4.2 == 1 2!= 1 2 > 1 2 < 1 2 >= 1 2 <= 1 2 (6+9==2+13) (8+9==2+13) (8+9!=2+13) (6+9!=2+13) (8+9>2+13) (6+9>2+13) (5+9<2+13) (8+9<2+13) (6+9>=2+13) (3+9>=2+13) (3+9<=2+13) (8+9<=2+13) = == = a=5 a 5 == a==5 a 5 2.4.3 not and not(3>5) not(5>3) (5>3) and (9>6) (5>3) and (9<6) (5<3) and (9>6) (5<3) and (9<6) 2-17
Python or (5>3) or (9>6) (5>3) or (9<6) (5<3) or (9>6) (5<3) or (9<6) and and or or 2.4.4 i 3 i = i + 3 = 2-18
02 i += 3 # i = i + 3 i -= 3 # i = i 3 i 10 += i += 5 15 -= i -= 5 5 *= i *= 5 50 /= i /= 5 2 %= i %= 5 0 //= i //= 5 2 **= i **= 3 1000 *(1+ ) 2% 6 (<complex.py>) ch02\complex.py 1 deposit = int(input(" ")) 2 times = 1.02 ** 6 3 deposit *= times 4 print("6 " + str(deposit)) 2-19
Chapter 03 判斷式 3.1 Python 3.1.1 Python 3.1.2 Tab 3.2 3.2.1 3.2.2 if 3.2.3 if else 3.2.4 if elif else 3.2.5 Python
Python 3.1 Python C Java {} if(score>=60) { grade = " "; } sum = sum + score 3.1.1 Python Python : 1 Tab 4 if(score>=60): grade = " " sum = sum + score Tab 4 Python Tab 4 Tab 3.1.2 Tab Tab Tab 4 Tab Python -t Tab -tt 3-2
03 3.2 3.2.1 Python 判斷式 if elif else 迴圈 4 for while 3.2.2 if if if if ( ): ( ) if : 3-3
Python x>2 x>2 or x<5 if ( ): 1234 (<password1.py>) ch03\password1.py 1 pw = input(" ") 2 if(pw=="1234"): 3 print(" ") 3-4
程式說明 2-3 3 03 1234 3 if Tab 4 if 2-3 if(pw=="1234"): print(" ") 3.2.3 if else if if else if else if ( ): else: if else if else 3-5
Python 1234 (<password2.py>) ch03\password2.py 1 pw = input(" ") 2 if(pw=="1234"): 3 print(" ") 4 else: 5 print(" ") 程式說明 2-3 3 4-5 5 4 else: 60!! (<score.py>) 3-6
Chapter 07 函式與套件 7.1 7.1.1 7.1.2 7.1.3 7.2 7.2.1 7.2.2 7.2.3 7.3 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 import 7.4.2 7.4.3 7.4.4 7.5 7.5.1 7.5.2 7.5.3 Python
07 7.4.3 randint randint.randint(, ) ( ) ( ) import random as r for i in range(0,5): # 5 5 print(r.randint(1,10), end=",") #9,8,1,10,4, 1 10 randrange randrange randint.randrange(, [, ]) ( ) ( ) 1 import random as r for i in range(0,5): # 5 5 print(r.randrange(0,12,2), end=",") #8,0,10,6,6, 0 2 12 ( ) 0 2 4 6 8 10 7-25
Python random random 0 1.random() import random as r print(r.random()) #0.5236730771512399 uniform uniform.uniform(, ) import random as r print(r.uniform(3,10)) #6.063374013178429 [ENTER] 1 6 [ENTER] (<randint.py>) [ENTER] 7-26
Python 7.4.4 choice choice.choice( ) import random as r for i in range(0,5): # 5 5 print(r.choice("abcdefg"), end=",") #f,a,g,g,d, import random as r for i in range(0,5): # 5 5 print(r.choice([1,2,3,4,5,6,7]), end=",") #1,1,2,7,6, sample sample choice sample.sample(, ) import random as r print(r.sample("abcdefg",3)) #['f', 'b', 'g'] print(r.sample([1,2,3,4,5,6,7],3)) #[3, 1, 4] 7-28
07 import random as r print(r.sa mple([1,2,3,4,5,6,7],8)) # sample 6 1 49 1 (<sample.py>) ch07\sample.py 1 import random as r 2 3 list1 = r.sample(range(1,50), 7) 4 special = list1.pop() 5 list1.sort() 6 print(" ", end="") 7 for i in range(0,6): 8 if i == 5: print(str(list1[i])) 9 else: print(str(list1[i]), end=", ") 10 print(" " + str(special)) 7-29
Appendix A MTA Python 程式設計核心能力國際認證模擬試題 A n d i ninin n Python
Python ( MTA Python ) 4. 5 = 5 = 60 5 17 = 120 17 = 180 def ticket _ fee(age, school): fee = 0 (1) fee = 60 (2) (3) fee = 120 else: fee = 180 return fee ( )(1) A. if age >= 5 and school == : B. if age >= 5 and school == : C. if age <= 17 ( )(2) A. if age >= 5 and school == : B. if age >= 5 and school == : C. if age <= 17 ( )(3) A. if age >= 5 and school == : B. if age >= 5 and school == : C. if age <= 17 5. Python 1 2 2 nu m = int(input(" : ")) digits = "0" if num > 0: (1) digits = "1" A-14