Python p Python Cross Platform Easy Learning Clear

  • Slides: 84
Download presentation

德光資訊科技 吳蓓怡 Python p. 為何要學Python? • 跨平台 (Cross Platform) • 易學易用 (Easy Learning) •

德光資訊科技 吳蓓怡 Python p. 為何要學Python? • 跨平台 (Cross Platform) • 易學易用 (Easy Learning) • 語法結構清晰 (Clear Syntax and Structure) • 延伸與內嵌 (Extended and Embedded) • 開放源碼 (Open Source) 3

德光資訊科技 吳蓓怡 Python p. Python Shell 6

德光資訊科技 吳蓓怡 Python p. Python Shell 6

德光資訊科技 吳蓓怡 Python p. 整數除法 // • 14//4 3 1425//27 52 3 52 4

德光資訊科技 吳蓓怡 Python p. 整數除法 // • 14//4 3 1425//27 52 3 52 4 ) 14 27 ) 1425 12 135 2 75 54 21 取餘數 % • 14 % 4 2 218 % 5 3 43 4 ) 14 5 ) 218 12 20 2 28 15 3 18

德光資訊科技 吳蓓怡 Python p. 40 條件式敘述(Conditional Statements) • 若有更多種判斷,要怎麼寫呢? 多擇一執行 • elif敘述 if score

德光資訊科技 吳蓓怡 Python p. 40 條件式敘述(Conditional Statements) • 若有更多種判斷,要怎麼寫呢? 多擇一執行 • elif敘述 if score == 100: print (“恭喜分” elif score >= 60: print “pass” else: print “fail” 0 60 100

德光資訊科技 吳蓓怡 Python p. 48 閏年判斷 解答 • year=2020 自行輸入年份,語法如下 year=int(input(“請輸入年分,可算出是否為閏年”)) • if year%400==0:

德光資訊科技 吳蓓怡 Python p. 48 閏年判斷 解答 • year=2020 自行輸入年份,語法如下 year=int(input(“請輸入年分,可算出是否為閏年”)) • if year%400==0: print("This year is a leap year") • elif year%100==0 and year%400!=0: • print("This year is not a leap year. ") • elif year%4==0 and year%100!=0: • print("This year is a leap year") • elif year%4!=0: • print("This year is not a leap year. ") • else: • print("This year is a leap year. ") •

德光資訊科技 吳蓓怡 Python p. 50 迴圈 (Loops) • #while 迴圈 1+2+3…. . +10的總和為多少? i

德光資訊科技 吳蓓怡 Python p. 50 迴圈 (Loops) • #while 迴圈 1+2+3…. . +10的總和為多少? i = 1 sum = 0 while i <= 10: sum = sum + i #也可以寫成 sum += i i = i+1 # 讓 i 值 +1 Print (sum) • #for 迴圈 (走訪序列的元素,一個元素一行印出) for j in [30, 40, 100, 205]: print(j) for k in "Hello World": print(k) for m in range(20): # range最後一個數字 20不會被印出來 print(m)

德光資訊科技 吳蓓怡 Python p. 56 日期函數 date • import datetime • print(datetime. date. today())

德光資訊科技 吳蓓怡 Python p. 56 日期函數 date • import datetime • print(datetime. date. today()) #匯入datatime 指令模塊 #印出今天年月日 • print(datetime. now()) #印今天年月日. 時分秒微秒

德光資訊科技 吳蓓怡 Python p. 61 繪圖 教學 1 教學 2 • • • •

德光資訊科技 吳蓓怡 Python p. 61 繪圖 教學 1 教學 2 • • • • import turtle as tu tu. color(“red”) for j in range(4): tu. pensize(j+3) tu. circle(100, 90) tu. right(90) tu. circle(100, 90) tu. color(“green”) tu. forward(200) tu. right(90) tu. forward(200) 寫法二 import turtle as tu # 迴圈做四次 即 j= 0, 1, 2, 3 # 筆的粗細 #畫圓,下面三列內縮,即屬於for迴圈 # 右轉 # 畫圓

德光資訊科技 吳蓓怡 Python p. 63 棒棒糖解答 • • • • import turtle as tu

德光資訊科技 吳蓓怡 Python p. 63 棒棒糖解答 • • • • import turtle as tu 寫法二 import turtle as tu. color(“red”) for j in range(____): # 四次 即 j= 0, 1, 2, 3 tu. pensize(j+3) # 筆的粗細 tu. circle(100, 100) #畫圓,下面三列內縮,即屬於for迴圈 tu. right(90) # 右轉 tu. circle(100, 100) # 畫圓 tu. color(“green”) tu. right(90) tu. forward(200) tu. right(90) tu. forward(20) tu

德光資訊科技 吳蓓怡 Python p. 使用函式 • 要怎麼畫出下圖? forward(100) right(90) penup() goto(50, 50) pendown() forward(100)

德光資訊科技 吳蓓怡 Python p. 使用函式 • 要怎麼畫出下圖? forward(100) right(90) penup() goto(50, 50) pendown() forward(100) right(90) 69

德光資訊科技 吳蓓怡 Python p. 70 使用函式 forward(100) right(90) penup() goto(50, 50) pendown() forward(100) right(90)

德光資訊科技 吳蓓怡 Python p. 70 使用函式 forward(100) right(90) penup() goto(50, 50) pendown() forward(100) right(90) 注意這邊要 空四格 def square(): forward(100) right(90) 牛刀小試 練習一下編寫square函式 注意這裏 要有冒號

德光資訊科技 吳蓓怡 Python p. 72 使用函式 • 有了square函式,但要怎麼畫出下圖? forward(100) right(90) forward(200) right(90) OK 不OK!!!

德光資訊科技 吳蓓怡 Python p. 72 使用函式 • 有了square函式,但要怎麼畫出下圖? forward(100) right(90) forward(200) right(90) OK 不OK!!!

德光資訊科技 吳蓓怡 Python p. 使用函式 forward(100) right(90) forward(200) right(90) def square(s): forward(s) right(90) 73

德光資訊科技 吳蓓怡 Python p. 使用函式 forward(100) right(90) forward(200) right(90) def square(s): forward(s) right(90) 73

75 德光資訊科技 吳蓓怡 Python p. 使用for迴圈 def square(s): forward(s) 1 right(90) forward(s) 2 right(90)

75 德光資訊科技 吳蓓怡 Python p. 使用for迴圈 def square(s): forward(s) 1 right(90) forward(s) 2 right(90) forward(s) 3 right(90) forward(s) 4 right(90) 注意這裏 要有冒號 for i in range(4): forward(s) right(90) 注意這邊要 空四格

德光資訊科技 吳蓓怡 Python p. 76 使用for迴圈 range([start], stop[, step]) • 產生一個從 start 到 stop

德光資訊科技 吳蓓怡 Python p. 76 使用for迴圈 range([start], stop[, step]) • 產生一個從 start 到 stop 的串列(list) • start 可省略,預設為 0 • step 可省略,預設為 1 for i in range(4): forward(s) right(90) for i in [0, 1, 2, 3]: forward(s) right(90) 牛刀小試 請用for迴圈畫出一個”正五邊形”、”正六邊形 ” 相當於 i=1 forward(s) right(90) i=2 forward(s) right(90) i=3 forward(s) right(90) i=4 forward(s) right(90) 達到重複 四次

德光資訊科技 吳蓓怡 Python p. 78 圓形 • circle ( radius ) :畫出半徑為 radius 的圓

德光資訊科技 吳蓓怡 Python p. 78 圓形 • circle ( radius ) :畫出半徑為 radius 的圓 • circle (50) • circle ( radius, extent ):畫出半徑為radius,角度為extent的弧 • circle( 50, 45 ) circle (50, 90) circle (50, 180) circle (100, -180)

德光資訊科技 吳蓓怡 Python p. Turtle Olympics 牛刀小試 79

德光資訊科技 吳蓓怡 Python p. Turtle Olympics 牛刀小試 79

德光資訊科技 吳蓓怡 Python p. 利用簡單圖案作圖 def petal(s): circle(s, 90) left(90) circle(s, 90) def flower(s):

德光資訊科技 吳蓓怡 Python p. 利用簡單圖案作圖 def petal(s): circle(s, 90) left(90) circle(s, 90) def flower(s): for i in range(4): petal(s) def fish(s): circle(s, 90) left(120) circle(s, 90) 牛刀小試 請畫出 10朵花和魚,隨機決定出現的位置和大小 83