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的創始人為Guido van Rossum。1989年聖誕節期間,在阿姆斯特丹,Guido為了打發聖誕節的無趣,決心開發一個新的腳本解釋程序,作為ABC 語言的一種繼承。之所以選中Python(大蟒蛇的意思)作為該程式語言的名字,是因為他是一個叫Monty Python的喜劇團體的愛好者。
Python開發環境設定
線上版本:在網路環境上撰寫程式
推薦 https://repl.it/
申請 repl.it 帳號即可
離線版本:在本機環境上撰寫程式
推薦使用Visual Studio Code
-
請下載Visual Studio Code與Python,並且都安裝好
-
確認Python可順利執行
2-1. 開啟新檔案(New File)
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 →
2-2. 先存檔,並請重新命名、選擇正確的檔案格式(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 →
2-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 →
2-4. 在Visual Studio Code按下Ctrl + F5,或是右上角的run按鈕,執行程式碼
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 →
2-5. 輸出結果請看下方Terminal視窗(紅框處)
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 IDLE
這是Python內建的執行環境,可以直接輸入Python語法執行,不過不適合多行程式碼,我們還是使用自己的IDE,並且編譯程式碼執行結果。
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使用縮排來定義程式碼區塊,也就是執行的順序,同一個層級縮排的程式碼,會被視為同一系列的工作
- 建議使用
4個空白
來縮排,但如果選擇好的IDE,也可使用Tab鍵
例如:
Python的資料輸入與輸出
資料輸出
使用print()
資料輸入
- 使用者手動輸入:使用
input()
- 必須使用變數來承接使用者輸入的資料(變數在CH2會正式介紹)
- 括號裡面可以加入提示使用者的訊息
- 從檔案輸入資料:使用
open()
,readline()
- 請注意存放資料的檔案大小,當使用
open()
時,會佔用相當大小的記憶體空間
首先使用test.txt
來存放資料
接著使用test.py來讀取處理資料
第一個程式:Hello World!
專題需求
請設計一個程式,在螢幕畫面上輸出文字訊息
程式開發原則
不管你學習什麼程式語言,都會從一個簡單的範例開始,最常見的就是Hello World這個程式。不過我們不該只以完成滿足功能需求的程式而滿足,而是要以良好思考與執行習慣來設計撰寫程式。
有良好的思考紀律,未來面對更大的問題照樣迎刃而解。
無論遇到什麼題目,請按照以下步驟進行:
回到這個專題,我們可以針對題目描述來分析
1. 確定問題需求、拆解問題、擬定解決方案
分析問題敘述,得到以下想法:
- 正確地選用Python語法,讓文字訊息輸出至螢幕上,顯示給我們看
- 思考並搜尋Python有什麼語法,可以輸出文字訊息
- 從Python官方文件,我們找到print()這個語法可以讓我們輸出文字訊息
💡Tips: Python官方文件及範例
https://www.python.org/dev/peps/pep-3105/
2. 確認開發環境
在這步驟,確認可以正常執行Python程式的電腦,是否都已設定完成,以下建議兩個解決方案,Windows/MacOS電腦皆適用,擇一使用即可:
- 電腦可以連上網路
開啟網路瀏覽器Chrome,輸入網址 www.repl.it ,申請帳號,登入後即可開始開發程式。
- 電腦不需連上網路
請下載MU Editor,安裝後開啟即可開始開發程式。
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 →
4. 編譯、執行程式碼,並確認結果是否正確
-
按下run
按鈕,執行程式碼

-
確認輸出結果
5. 若執行結果不正確,思考問題點,回到第三步檢查修正程式碼
以上,就完成了我們第一個Python程式,請務必熟悉步驟。
隨堂練習
試試看,只可以用一次print()
,讓螢幕以下方格式輸出內容
💡Tips: 輸出文字換行的Python語法是什麼?
參考程式碼:https://repl.it/@ChaoyenWu/BewitchedSlightTrialsoftware