Python Ch 6-1 Homework

CH 6-1 串列(列表) list Ex 6-1-1 有一串列  list-a = [1,2,3,5,7,9] 請加入數值6後, 將串列重新排列輸出 Ex 6-1-2 請利用串列寫程式, 依次輸入10個數字, 最後列印該串列, 並計算平均. Ex 6-1-3 有一串列  list-b = [,2,3,5,7,9] Ex 5-1-4   Ex 5-1-5   Ex 6-1-6 請畫出以下的圖形

Python Ch 6-1 Homework Read More »

Python Chapter 6

Chapter 6 再談資料類型 6-1 -1 串列(列表) list: 屬於集合的一種 #list列表 [ ] # 範例6-1-1 列表 [ ]fruits = [“apple”,”orange”,”banana”,”pear”]print(fruits[0]) #appleprint(fruits[1]) #orangefor i in fruits: print(i) #加入元素 appendfruits = [“apple”,”orange”,”banana”,”pear”]fruits.append(“pineapple”)print(fruits) # [‘apple’, ‘orange’, ‘banana’, ‘pear’, ‘pineapple’] #移除元素 removefruits = [“apple”,”orange”,”banana”,”pear”,”pineapple”]fruits.remove(“orange”)print(fruits) # [‘apple’, ‘banana’, ‘pear’, ‘pineapple’] #找出元素位置 indexfruits = [“apple”,”orange”,”banana”,”pear”,”pineapple”]print(fruits.index(“banana”)) # 2 # 可加入重複元素 appendfruits = [“apple”,”orange”,”banana”,”pear”,”pineapple”]fruits.append(“orange”)fruits.append(“orange”)print(fruits) #

Python Chapter 6 Read More »

Python Chapter 5

Chapter 5 繪圖-使用turtle 5-1 turtle的基本方法/指令 method 5-1-1. 可參考以下連結文件  https://docs.python.org/3/library/turtle.html#turtle-methods #基本指令 forward(), right(), left(), up(), circle(), down() import turtle as t #呼叫 turtle 模組t.clear() #清除畫布螢幕t.reset() #重置, 畫筆停在原點座標(預設是畫布的中心點)t.Pen() #提筆t.forward(200) #預設箭頭向右,向右畫長度200像素的線t.done() #畫圖結束 # 範例5-1-1 畫正方形import turtle as tt.pen()t.forward(200)t.left(90)t.forward(200)t.left(90)t.forward(200)t.left(90)t.forward(200)t.done() 將範例5-1-1改用迴圈方式重寫 # 範例5-1-2 畫正方形import turtle as tt.pen()for i in range(4): t.forward(200) t.left(90)t.done() # 範例5-1-3 畫五角星import turtle as tt.clear()t.reset()t.pen()for i in

Python Chapter 5 Read More »

Python Ch 4-2 Homework

CH 4-2 for 迴圈 作業練習 Ex 4-2-1 請寫程式,以遞增的方式,計算從 1 到 100 的奇數和。 Ex 4-2-2 請寫程式,以遞減的方式,計算從 100 (或99) 到 1 的偶數和。 Ex 4-2-3 試問下列程式的執行結果。 total=0for i in range(50,-20,-10): total +=iprint(‘total=’,total) Ex 4-2-4 試撰寫一程式,求 2~500 之間所有的質數。 Ex 4-2-5 請將範例 4-2-9 的九九乘法表程式,改以 while 巢狀迴圈撰寫。 Ex 4-2-6 由亂數產生器產生一個 1~100 的整數當作答案,然後讓使用者猜此數字,當猜的數字太大,就顯示”你猜的答案太大了, 請再試一次”;當猜的數字太小,就顯示”你猜的答案太小了, 請再試一次”;直到猜到正確數字, 終止程式並顯示”恭喜你,猜對了”。 Ex 4-2-7 請查看範例 4-2-3 產生的六個亂數,原程式是有可能重複出現相同數字。試修正此問題,避免產生重複數字。

Python Ch 4-2 Homework Read More »

Python Chapter 4-2

Chapter 4 迴圈敘述 Loop 4-2 For Loop 4-2-1. for x in range(start,end,step) 當  start ≦  x  ≦ end-1,   間距是 step,  反覆執行指令。 必要值: end        預設start=0      預設step=1   #範例4-2-1#using for statementfor i in range(1,13,2): print(i, end=’ ‘)print()for j in range(1,6): print(j, end=’ ‘)print()for k in range(8): print(k, end=’ ‘)print()for m in range(10,28,3): print(m,

Python Chapter 4-2 Read More »

Python Ch 4-1 Homework

CH 4-1 while 迴圈 作業練習 Ex 4-1-1 參考範例 4-1-2, 寫一段程式,輸入 被除數與除數,只利用加減法求商數到小數第一位。(提示: 先將被除數乘以10倍, 運算結束後, 再將商數除以10) Ex 4-1-2 請寫一個程式,可輸入任意次數的整數,直到輸入值為 0 就結束,並印出輸入數字的平均 (必須先算出總和與次數)。 Ex 4-1-3 請寫程式,以遞增的方式,計算從 1 到 100 的奇數和。 Ex 4-1-4 請寫程式,以遞減的方式,計算從 100 (或99) 到 1 的奇數和。 Ex 4-1-5 請寫程式,輸入兩個正整數,以輾轉相除法的方式求最大公因數 (參考範例4-1-5, 將其優化)。 最後顯示如:   (270,75) = 15 Ex 4-1-6 請寫程式,i 從 1 開始累加奇數到變數total,直到總和大於等於5050時結束,最後印出 i 與 total 的值。

Python Ch 4-1 Homework Read More »

Python Chapter 4-1

Chapter 4 迴圈敘述 Loop 4-1 While Loop 4-1-1. While Loop : 依照設定的條件,只要條件成立(True),迴圈就會重複執行。 #範例4-1-1#using while statementi = 1total = 0while (i<=100): total += i i += 1print(‘sum of 1 to 100 is’, total) 假如Python沒有除法的運算, 以下的程式是利用 while迴圈 與 加減法, 就可以做到除法運算。 #範例4-1-2 以加減法運算除法a = 8 #被除數b = 3 #除數q = 0 #商while (a>=b): a=a-b q+=1 print(a,q)print(‘商=’,q,’ ,餘數=’,a) 數學題目: 有個級數

Python Chapter 4-1 Read More »