Try   HackMD

Python程式設計

一、何謂程式語言
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 →

程式語言(Programming languages)

是指人類與電腦之間進行溝通的語言,用來命令電腦如何工作的執行指令集合。

程式(Programs)

則是指使用特定語言組合形成指令與敘述來編寫程式碼。程式碼即為指示電腦解決問題的步驟。程式包含輸入、轉換邏輯及輸出,透過鍵盤或滑鼠將輸入的資料轉換成有用的資訊來輸出。

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 →


程式設計流程

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 →

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 →


程式語言的種類

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 →

  • 低階語言: 電腦直接看得懂,執行效率較高,但使用者不容易讀懂,使用0和1的二進位程式碼,如0101 0001 0010 0011。組合語言: 使用簡單的符號指令來代表0和1的二進位程式碼,需要組譯程式可以轉換成機器語言。(維基百科)
  • 高階語言: 較接近人類語言的程式語言,電腦不能直接讀懂,故需要使用翻譯的方式來轉換成機器碼,雖然效率較低,但適合使用者學習,常見的高階語言有BASIC、C/C++、C#、Java等。高階語言又分編譯式程式語言,如C/C+以及直譯式程式語言,如Python。

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 →
全世界最受歡迎的程式語言 2004-2019 (PYPL 排名)


二、認識Python
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 →

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 →

Python是一種廣泛使用的直譯式、進階和通用的程式語言。Python支援多種程式設計範式,包括函數式、指令式、反射式、結構化和物件導向程式設計。它擁有動態型別系統和垃圾回收功能,能夠自動管理記憶體使用,並且其本身擁有一個巨大而廣泛的標準庫。它的語言結構以及物件導向的方法,旨在幫助程式設計師為小型的和大型的專案編寫清晰的、合乎邏輯的程式碼。 (維基百科)

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 →
【python】4小時初學者Python教學


三、Python程式設計-print & input
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 →

線上程式編輯器: google_colab/ repl.it / onlinegdb


練習一: 讓程式輸出"Hello World"

使用輸出指令: print()

語法如下:

print ('Hello, World!')

執行結果如下:

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 →

練習二: 讓程式輸出'Hello World!'三次

語法如下:

print ('Hello World!'*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 →
重要說明:程式語言的基本概念之一[Assign指派=]

在程式語言的中, = 這個符號是代表指派(指定)的意思。
譬如說 a = b 代表的就是把 b (的內容)指定給 a (這個變數)。

在程式語言中等號(=)的概念和意義與數學算式不同,數學的等號代表等號的左右兩邊相等,程式語言中的「=」是將右邊的數值儲存到左邊的變數。若要比較等
號兩邊的數值是否相等,要使用「==」(後面會再說明,請見下方連結影片)

程式中的等號(影片說明)
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 →

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 →
從零開始學程式#3 等號

練習三: 試試看以下程式碼執行看看結果為何?

線上程式編輯器: google_colab/ repl.it / onlinegdb

a = 5 print(a) a = a + 10 print(a)

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 →
重要說明:程式語言的基本概念之二[變數Variables]

何謂變數? 簡單的來說變數就是拿來裝資料的容器,就像在數學中,我們也有假設變數這方法,如假設X=3,Y=4,Z=X+Y,那Z是多少?在程式設計中變數必須進行宣告,而Python程式中的變數宣告是最直觀與簡單的,如下說明:

宣告單個變數

  1. 我們先將整數5賦予如宣告的變數 a
  2. 輸出a的值
  3. 結果顯示為5

語法如下:

a = 5 print(a) #輸出得到結果5

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 放入宣告的變數 a
  2. 將數字 2 放入宣告的變數 b
  3. 將數字 3 放入宣告的變數 d
  4. 印出變數 a、b、d 的內容

語法如下:

a = 1 b = 2 c = 3 print(a) #輸出得到結果1 print(b) #輸出得到結果2 print(c) #輸出得到結果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 →
Python 變數 (Variables) 的必備概念 | 從零到一的 Python 入門教學

練習四:試試看以下程式碼執行看看結果為何?(想一想)

a = 1 b = c = a print(a) print(b) print(c)

四、資料型態: Python對應的資料型態
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 →

有關變數的型態不一定是數值,也可能是文字或是其他,以下即為程式語言的資料型態

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 →

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 →
Python 變數與資料型態 By 彭彭


線上程式編輯器: google_colab/ repl.it / onlinegdb

練習五: 請設計一個程式,讓使用者輸入名字後,電腦會將名字呈現在畫面上與使用者說Hello。

使用輸出指令: input及print()

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 →

語法如下:

name = input('請問你的名字是?') print('Hello',name,'您好!')

input 指令是讓使用者由鍵盤輸入資料,通常會用變數儲存該資料
語法如下: 變數 = input(['提示字串'])

  1. 程式中會出現各種符號,以下將分別說明各種符號的作用:
  2. 等號= 的作用:將等號右邊的數值儲存到等號左邊的變數。
  3. 小括號( ) 的作用:放入想要儲存的內容。
  4. 中括號[ ] 的作用:[ ] 中的內容可以擴充傳入的資料,也可以省略。
  5. 單引號' ' 的作用:' ' 中的內容會被程式視為字串,通常會在字串前後成對出現,字串的表示也可用雙引號" "。

print 指令為輸出資料,可以一次輸出多個項目,項目之間以「,」隔開
語法如下:
print(項目1 [, 項目2, , sep=分隔字元, end=結束字元])

print 指令中出現了sep、end,分別說明如下:

  1. sep 分隔字元:如果有多個項目,項目間使用分隔字元區隔,預設為一個空白字元。
  2. end 結束字元:輸出完成時自動加入的字元,預設為一個換行字元。

執行結果如下圖:


練習六: 值日生
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 →

請設計一個程式,讓使用者輸入值日生的名字後,電腦會將今天值日生的名字呈現在畫面上。

線上程式編輯器: google_colab/ repl.it / onlinegdb

(參考解答)


五、資料型態的轉換
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 →

資料型態相同的變數才能進行運算。對於某些資料型態,Python 能夠進行簡單的自動轉換。如果是整數與浮點數運算,系統會先將整數轉換成浮點數再運算,而最後的運算結果仍為浮點數。

說明:type()是python判斷資料型別的語法

number = 8 + 6.3 # 結果為 14.3,浮點數。 print(number) #輸出變數number的結果 print(type(number)) #輸出變數number的資料型態

如果系統無法自動進行資料型態轉換,就需使用下列的強制資料轉換指令。

舉例來說,如果對整數與字串做加法運算,會產生錯誤。

number1 = 56 + '23' # 錯誤,無法進行運算,因為'23'是字串無法直接與前面的數字相加。
number2 = 56 + int('23') # 正確,結果為 79,利用強制轉換int()將字串轉為整數,則可以跟前面數字相加。

練習七: 資料型態的練習

線上程式編輯器: google_colab/ repl.it / onlinegdb

1. 試著完成以下的程式碼並執行結果
說明:type()是python判斷資料型別的語法

a = '123' print(type(a)) b = 123 print(type(b)) d = 1.25 print(type(d))

2. 試著完成以下的程式碼並執行結果(注意觀察差異在哪)
int()是將()的內容強制轉換成整數

a = '123' print(a) print(a+a) b = 123+int(a) print(b)


六、算術運算符號
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 →

Python 中,可使用的算術運算符號有 6 種,如下表所示。


圖片來源: 翰林資訊科技教材

舉例來說:

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 →

print(15%7) # 結果為 1。
print(15//7) # 結果為 2。


線上程式編輯器: google_colab/ repl.it / onlinegdb

練習八:試著輸出下列程式碼,執行結果為何?

print(8/3) print(8%3) print(8//3) print(2**5)

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 →
Tips: Python 算術運算符號的優先順序:
 1.括號內優先。
 2.乘、除法。
 3.除法取商數。
 4.除法取餘數。
 5.加、減法。


練習九:求三數之和

線上程式編輯器: google_colab/ repl.it / onlinegdb

請試著練習以下的程式碼並執行結果


練習十:求(a+b)×c 的值

線上程式編輯器: google_colab/ repl.it / onlinegdb

請設計一個程式,讓使用者依序輸入 a、b、c 後,再輸出呈現(a+b)×c 的值。

(參考答案) 同學輸入及輸出的答案不可與老師的相同


練習十一:求平均數程式

請設計一個程式,讓使用者輸入兩個數字後,再呈現兩個數字的平均值。

說明: 數字與字串間的運算
加號不僅可用於數值運算,也可用於字串組合,但同學們必須謹記「只有資料型
態相同的變數才能進行運算」。因此,使用時需注意其所應用的資料型態,如下:

23 + 64 # 正確,結果為 87。
23 + '64' # 錯誤,無法進行運算。
'hello' + 34 # 錯誤,無法進行運算。
'hello' + '34' # 正確,結果為 hello34。
'hello' + str(34) # 正確,結果為 hello34。


請試著執行程式(x,y請自行輸入數值)

線上程式編輯器: google_colab/ repl.it / onlinegdb


練習十二: 剩餘的零用錢

請設計一個程式,讓使用者依序輸入今天的零用錢、早餐的費用及零食的費用,再呈現 剩餘的零用錢。(輸出結果應為"今天剩下X元")
(假設t為零用錢、a為早餐費用、b為零食的費用、z為剩下的錢)

(參考答案)同學輸入及輸出的答案不可與老師的相同