Try   HackMD


本著作係採用創用 CC 姓名標示-非商業性 3.0 台灣 授權條款授權. 在服用所有團隊內容前先看這篇不然 @yen0224 會打你屁屁,不遵守請立即離開,凡繼續觀看其他內容則視為已同意此申明條款

py課程大綱及教學講義

課程內容

  • 0.1 python introdution
  • 1.1 py overview
    • 1.1.1 py程式碼特色
    • 1.1.2 變數
    • 1.1.3 算術運算子
    • 1.1.4 邏輯運算子
  • 1.2 print
    • print()
    • print 字串/變數
    • 練習1
  • 1.3 input
    • input()
    • input例題
    • eval()
    • split()
    • input/output/eval/split例題
  • 1.4 if/ifelse/ifelif..else/巢狀if
    • if 例題
    • ifelse 例題
    • ifelifelse 例題
    • 巢狀if
    • if 實作
  • 1.5 while
    • while 介紹
    • while 例題
    • while 練習題
  • 1.6 for loop
    • for 介紹
    • for 例題
    • for 練習題
    • 雙層 for
    • 例題
    • 練習題

0.1 python introduction

Python是一種易於學習、功能強大且被廣泛使用的高階程式語言 Python設計哲學強調程式碼的可讀性與簡潔的語法,其優雅語法和動態類型使其成為大多數平台上許多領域程式編寫和快速應用程序開發時的理想程式語言。 因為Python的功能強大及應用範圍廣,讓全球知名企業、資料科學家及更領域的專家,紛紛投入開發,例如Wikipedia, Google , Yahoo!, CERN and NASA p.s 人工智慧、大數據常常運用到python

1.0 安裝python

Setup Python Environment

1.1.1 py程式碼特色

  • py程式碼架構
    • 縮排(一個tab)
for i in range(10): print(i)
  • #為單行註解
#在井字號後加入註解,註解的文字不會被程式碼編譯
  • '''或"""為多行註解
'''夾在三個雙引號或單引號中間可以多行註解 換行後還是在註解'''

1.1.2 變數

  • 我們可以使用指派運算子(=)(assignment operator) 來指派變數的值,而且是以最近一次指派的值為準
  • 可以不用宣告變數的型態

1.1.3 算術運算子

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

1.1.4 邏輯運算子

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

1.2 print()

1.2.1 print()

我們可以使用py內建print()函式(是一個由py開發者事先寫好的程式),在螢幕上印出指定的字串

  • 當要印出字串時,在字串前後加上兩個雙引號""或兩個單引號''
  • 以逗號間隔想要印出的內容
  • 在print()裡放入變數,程式編譯時會自動轉換成對應的值
  • 在print()內輸入數字運算時,程式會自動運算,並印出結果
>>> print("I", "am", "Jenny") I am Jenny >>> x=5 >>> y=7 >>> print('x=',x,'y =',y) x= 5 y= 7 >>>print(10/5) 2

1.2.2 print()的預設值

  • 當我們在使用print()語法時,程式執行時會預設sep=" "、end="/n",若我們想更改成其他值,我們可以使用以下語法。
>>> print("I", "am", "Jenny",sep=@,end="#") I@am@Jenny#

1.2.3 py實作1

印出下列數學式結果 (1) 57+38 (2) 3-6/3+2 (也可以試試添加sep=,end=,熟悉此語法)

1.3 input()

1.3.1 syntax

  • 我們可以使用 Python 內建的 input 函式取得使用者輸入的資料
  • 括號內""中為選擇性參數,用來設定提示文字
  • 語法如下:
score=input("輸入成績") print("成績為",score)

1.3.2 eval()

  • 我們可以使用Python內建的eval函式將括號內的字串轉換成數值
  • 語法如下:
score=eval(input("輸入成績")) score=score+10 print(score)

input:56

output:66

  • 想想看如果不加eval()會output出甚麼?

1.3.3 split()

  • 使用一些分隔符號來將一個字串分解成兩個以上字串
  • 可以在函式中指定分隔符號,若不指定分隔符號,split會使用所有的空白字元:換行符號、空格、及tab。 範例程式:
[a,b,c]=input().split() print(a) print(b) print(c)

input: python is fabulous

output: python is fabulous

1.3.4 實作

(1) 寫一程式輸入三角形的底和高,求其面積 Sample Input:5 10 Sample Output:三角形面積為25 (2) 寫一程式輸入班上5位同學成績,印出全班總分與平均。 Sample Input:100 100 100 100 100 Sample Output:全班總分為500分,平均為100

  • 補充(自己看啦沒時間講了)
>>> u = "www.doiido.com.cn" #使用默认分隔符 >>>print u.split() ['www.doiido.com.cn'] #以"."为分隔符 >>> print u.split('.') ['www', 'doiido', 'com', 'cn'] #分割0次 >>> print u.split('.',0) ['www.doiido.com.cn'] #分割一次 >>> print u.split('.',1) ['www', 'doiido.com.cn'] #分割两次 >>> print u.split('.',2) ['www', 'doiido', 'com.cn'] #分割两次,并取序列为1的项 >>> print u.split('.',2)[1] doiido (內容)

1.4 條件述句

1.4.1 ifelse..

  • syntax:
if condution: statement1(條件真實執行) else: statement2(條件假時執行)
  • 範例程式
score =eval(input("請輸入數學分數")) if score>=60: ​ print("及格") else: ​ print("不及格")

1.4.2 實作

(1) 寫一程式,輸入X的值,依其實際值印出「X是基數」或「X是偶數」 Sample Input:5 Sample Output:X是基數 (2) 寫一程式輸入X值,輸出Y值。若輸入的X值介於50到70(含)之間,將X的值指定給Y,否則Y的值為100。 Sample Input:65 Sample Output:65

1.4.3 巢狀if (超過兩個以上情況)

  • 範例程式
score =eval(input("請輸入數學分數")) if score>=90: print("優等") else: if score >= 80: print("甲等") else: if score>=70: print("乙等") else: if score >= 60: print("丙等") else: print("不及格")
  • ifelifelse
  • syntax
if condition 1: statements1 elif condition 2: statements2 elif condition 3: statements3 … else: statementsN+1
  • 範例程式:
score =eval(input("請輸入數學分數")) if score>=90: print("優等") elif score >= 80: print("甲等") elif score>=70: print("乙等") elif score >= 60: print("丙等") elif: print("不及格")
  • 想想看為什麼甲等不用輸入<90的條件

1.4.4 實作

(1) 當兵條件 身高介於 150 到 190( 含 之間;體重介於 50 到 90( 含 間。 寫一程式輸入身高與體重,印出是否需要當兵。

1.5 while

1.5.1 while

有時我們需要非一次性的東西,我們需要迴圈,而python最簡單的迴圈機制就是while。

  • syntax
while condition1: statements1 #當符合條件condition1時執行statement1
  • 範例程式:
i= 0 while i<5: print(i) i= i + 1
  • break

如果你希望迴圈在某個情況時停止,但不確定那件事情甚麼時候發生,可以使用無窮迴圈和一個break陳述式。

  • 範例程式
while 1:#while 1為條件恆為真的意思 ​ stuff=input() ​ if stuff="quit": ​ break print(stuff)

1.5.2 實作

(1)寫一程式,持續輸入班上每一位同學的成績,若沒打成績直接enter時,代表成績輸入已結束。請印出全班的同學人數與成績總分。 (2)猜密碼遊戲,寫一程式,當輸入值符合54624時,結束程式。 Sample Input:54578 Sample Output:錯誤請重新輸入密碼(不斷迴圈直至密碼正確) Sample Input:54624 Sample Output:答對了

1.6 for loop

1.6.1 for loop

for用來針對可迴圈(loop)的物件進行重複運算

  • syntax:
for i in range(start,stop,step): #start:起始值 stop:停止值 step:每次變動值
  • example:
for i in range (1,5,1): print(i)

output: 1 2 3 4

for i in range (10,5,-1): print(i)

output: 10 9 8 7 6

1.6.2 for loop 的預設值(default)

當未指定時,預設start=0,step=1,

for i in range(8): print(i,end=" ")

output: 0 1 2 3 4 5 6 7

for i in range(1,5): print(i,end=" ")

output: 1 2 3 4

1.6.3 for 例題

(1) 運用迴圈計算求S S=2+4+6+8+……+30 (2) 運用迴圈計算求S S=3 *9 *15 *21 (3) 寫一程式,印出 1~100 間,所有 3 的倍數之值。

1.6.4 雙層for迴圈

  • syntax
for i in range(1,10): statements1 #s1 for j in range(7,4,-1): statements2 #s2
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • example 寫一程式 輸入全班 10 位同學的三次考試成績,求 (1)每位同學的平均分數。 (2)印出第一次的全班最高分 。 (3)第二次的不及格人數
max_1=0 fail_2=0 for i in range(1,11,1) sum=0 for j in range(1,4,1): score= eval(input('第'+str(i)+'位同學第+'str(j)+'次成績:')) if (j==1 and score>max_1): max_1=score if (j==2 and score<60): fail_2=fail_2+1 sum=sum+score print('==>第 str(i) 位同學平均為 :',sum/3) print("第一次最高分", max_1,"第二次不及格人數 :",fail_2)

1.6.5 雙層for練習題 (1) S=1/3 + 3/7 + 5/11 + 7/15 (用單層for) (2) S=(3+5)+(3+5+7)+(3+5+7+9)


參考資料

國立成大大學資訊工程學系陳培殷教授上課用PPT

還有一些網路上的參考資料,但現在找不太到在哪沒辦法列ಥ_ಥ