01 1.6 Spyder Anaconda Spyder Python Spyder Python Spyder Spyder 1.6.1 Spyder 開始 \ 所有程式 \ Anaconda3 (64-bit) \ Spyder Spyder IPython Python IPython Spyder <c:\users\ \.spyder-py3\temp. py> Python File \ New fle Python File \ Open Open file Spyder 1-13
Python File Explorer ( ) Spyder Spyder 檔案瀏覽器 檔案瀏覽器 File Explorer 檔案瀏覽器 Select directory 選擇資料夾 Run \ Run 1-14
01 1.6.2 Spyder Spyder IPython IPython Spyder Tab <loop.py> s Tab Tab Enter show 1.6.3 Spyder Python 1-15
Python Spyder F12 ( ) Spyder Variable explorer Spyder 1-16
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 abc 20 a = b = c = 20,, age 18name age, name = 18, " " del score del score 2-3
Python 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-18
02 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-19
03 3.2 3.2.1 Python 判斷式 ifelifelse 迴圈 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 ifelse if ifelse ifelse if : else: if else if else 3-5
04 4.2 for for for in : for 1 for n in range(3): # 0,1,2 2 print(n, end=",") # 0,1,2, for n 0 2 0, 1 n 1 2 1, 1 n 2 2 2, for for for range for 30 ( 2 31) for i in range(1,31): 4-5
Python 4.2.1 for ifelifelsefor for for for n = 0 for i in range(1,10001): for j in range(1,10001): n += 1 print(n) n+=1 (10000x10000) CPU (<fornest.py>) ch04\fornest.py 1 for i in range(1,6): # 5 2 print("",i,", ",i," ",end="") 3 for j in range(1,i+1): # 4 print("#", end="") 5 print() # 4-8
程式說明 1 2 3 4 5 for 5 04 for i j<i+1 i 1 i 2 i 5 i=1 j=1 i=2 j=1 j=2 i=3 j=1 j=2 j=3 i=4 j=1 j=2 j=3 j=4 i=5 j=1 j=2 j=3 j=4 j=5 c for (<ninenine.py>) 4-9
Python Enter 1 6 Enter (<randint.py>) [ENTER] ch07\randint.py 1 import random 2 3 while : 4 inkey = input(" [ENTER] [ENTER] :") 5 if len(inkey) > 0: 6 num = random.randint(1,6) 7 print(" " + str(num)) 8 else: 9 print(" ") 10 break 程式說明 1 3-10 5-7 8-10 Enter 1 6 Enter 7-26
07 (<randint_cl.py>) 7.4.4 choice choice random.choice( ) import random for i in range(5): # 5 5 print(random.choice("abcdefg"), end=",") #f,a,g,g,d, import random for i in range(5): # 5 5 print(random.choice([1,2,3,4,5,6,7]), end=",") #1,1,2,7,6, sample sample choice sample random.sample(, ) 7-27
Python import random print(random.sample("abcdefg",3)) #['f','b','g'] print(random.sample([1,2,3,4,5,6,7],3)) #[3,1,4] import random print(random.sample([1,2,3,4,5,6,7],8)) # sample 6 1 49 1 (<sample.py>) ch07\sample.py 1 import random 2 3 list1 = random.sample(range(1,50), 7) 4 special = list1.pop() 5 list1.sort() 6 print(" ", end="") 7 for i in range(6): 7-28