Python程式設計
Lesson 1: 簡介
Last updated: 2024/2/22
大綱
- 寫在前面
- 開發環境
- Python程式初體驗
寫在前面
實務問題記帳: 紙筆 vs. 試算表

實務問題點餐: 紙筆 vs. 平板

https://cdn2.ettoday.net/images/852/d852921.jpg
https://s4.itho.me/sites/default/files/images/682-%E5%B0%81%E9%9D%A2%E6%95%85%E4%BA%8B-P31-(600)-1.png

解決問題5步驟
1. 確認問題
2. 產生解決問題的方法
3. 評估方法的優劣
4. 決定採用哪一種方法
5. 解決了嗎?
No
Algorithm
發音: Al-go-ri-thm
演算法: 完成一件工作的一系列步驟
Say It With ME
Program
發音: Pro-gram
程式: 編程完成的演算法(可在3C裝置上執行)
問題 畫圖紙程式設計
問題 畫圖紙程式設計
由星號出發
演算法(步驟)
向右移動一格
塗滿空格
向右移動一格
向下移動一格
塗滿空格

如果有5個程式指令:
上 下 左 右
填滿
程式
右
填滿
右
下
填滿
各式各樣的軟體

軟體: 由程式語言寫成

該先學哪一種程式語言?

2024最受歡迎程式語言
指標:網路教學文件多寡

2021最受歡迎程式語言

指標:網路教學文件多寡
2024最受歡迎程式語言
指標:網路教學文件/ 程式高手 / 協力廠商

2021最受歡迎程式語言

指標:網路教學文件/ 程式高手 / 協力廠商
2019最受歡迎程式語言
https://cdn-images-1.medium.com/max/2600/1*cWW2X0SCMeft-bmYjWcvMA.jpeg

2018最受歡迎程式語言
https://cdn-images-1.medium.com/max/2600/1*cWW2X0SCMeft-bmYjWcvMA.jpeg

開發環境
開發環境版本眾多

https://image.slidesharecdn.com/pythonflavors-140328184249-phpapp01/95/python-flavors-1-638.jpg?cb=1431970980
因為Python為規格書
有許多不同「實作版本」!
開發環境該用哪個版本?
https://image.slidesharecdn.com/pythonflavors-140328184249-phpapp01/95/python-flavors-1-638.jpg?cb=1431970980
「實作版本」名稱

執行環境(虛擬機器)
實作所用語言
初學者:差別不大
該問的問題是:如何編輯程式?如何執行程式
開發環境所需工具

https://fatemasaifee.files.wordpress.com/2014/08/2-2.png?w=676
程式碼
可執行碼
執行環境
編輯器
編譯器
直譯器
虛擬機器
整合開發環境 (IDE)
IDE較為方便!
開發環境
整合開發環境:Anaconda

- 前置安裝: Python官方實作版
- 編輯器 + 直譯器
Spyder
VS Code
jupyter notebook




開發環境下載Python

目前最新版本 3.12.X
找X86-64版本
開發環境安裝python
勾選「加入路徑」

開發環境 測試:使用命令列
開啟命令提示字元,輸入python

quit(): 退出python環境
函式、以括弧結尾
開發環境 測試:使用IDLE

或是執行python內建工具: IDLE

安裝Anaconda

Python程式
初體驗
開啟Anaconda Navigator

開啟
Jupyter Notebook執行

Jupyter Notebook: 在瀏覽器內執行Python程式
❸ 進入互動模式
❶
❷

Jupyter Notebook執行

❶ 檔案名稱
❷ 編輯單元(cell)
❸ 選單與工具列
Python程式在此輸入執行
副檔名為.ipynb
Jupyter Notebook執行


綠色: 編輯模式
藍色: 命令模式(執行...)
快捷鍵(Windows) | 作用 |
---|---|
Enter | 進入編輯模式 |
Esc | 進入命令模式 |
Ctrl-Enter | 執行目前的單元(cell) |
Shift-Enter | 執行目前單元,切換至下一個新單元(命令模式) |
Alt-Enter | 執行目前單元,切換至下一個新單元(編輯模式) |
命令模式 | 作用 |
---|---|
a | 加入上方單元 |
b | 加入下方單元 |
x | 剪下單元 |
c | 複製單元 |
v | 貼上單元 |
問題敘述
寫一個程式做「基本資料調查」:
- 螢幕輸出「請問貴姓大名?」
- 等使用者輸入姓名後,將姓名在螢幕上。
- 螢幕輸出「請問年紀?」,
- 等使用者輸入年紀後,將年紀在螢幕上。
- 螢幕輸出「請問體重?」
- 等使用者輸入體重後,將體重在螢幕上。
如何用python輸出文字? 使用print() 函式
如何讓使用者輸入姓名等? 使用input() 函式
第一個Python程式

第一個Python程式

第一個Python程式
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
name = input('請問貴姓大名?')
print('你好,', name)
y = int(input('請問年紀?'))
print('原來你', y, '歲')
w = float(input('請問體重?'))
print('體重為', w)

如何查詢函式功能?

- 在python環境內,輸入help(函式名稱)
函式說明
程式註解(1/2)
- 註解: #字號之後該行的文字
- 註解僅是說明,不會執行
name = input('請問貴姓大名?') # 輸入姓名,存入name
print('你好,', name)
程式註解(2/2)
- 註解: 三個單引號 或 雙引號
- 大段落註解
'''
程式範例ch1_1.py
作者:王小明
學號:WH001
'''
name = input('請問貴姓大名?') # 輸入姓名,存入name
print('你好,', name)
Python程式設計
By Leuo-Hong Wang
Python程式設計
Lesson 1: 簡介
- 1,271