講者


皮皮:Yu-Ren Pan
臺北市立大學 數學系

程式語言

指示程式運作的「劇本」

純文字

文字編輯器

  • Notepad++
  • VIM
  • Visual Studio Code
  • 真男人都用 Notepad,沒有 ++

副檔名

.py

安裝

Python3

互動界面

  • 終端機、命令列
    ​​​​$> python3   
    
  • IDLE(整合開發與學習環境)
$> python3
Python 3.7.0
Type ... information. 
>>> 

算數

>>> 1 + 3
4
>>> 11 / 2  
5.5
>>> 5 ** 3
125
 +   # 加
 -   # 減
 *   # 乘
 /   # 除
 //  # 整數除法 
 %   # 取餘數
 **  # 次方
 (10 + 3) ** 2 
    13    ** 2
       169

變數

把資料記下來

>>> a = 10 + 5 
>>> a
15
>>> a + 87
102
>>> a = 10 + 5 
>>> a
15
>>> a = a + 1
>>> a
16
>>> pi = 3.1415926535 
>>> r = 14
>>> pi * r ** 2
615.752160086

字串

把一堆字串起來

>>> kidd = "我叫做基德,是個" 
>>> 
>>> kidd + '律師'
'我叫做基德,是個律師'

型態轉換

>>> a = "94"
>>> a + "87"
'9487' 
>>> int(a) + int("87") 
181
>>> '森' + str(77)
'森77'

函式 Function

數學裡的函數

\(f(x) = x^2 + 1\)

\(f(3) = 10\)

\(f(5) + 1 = 26 + 1 = 27\)

程式語言裡的函式

函式名稱 輸入參數 執行函式 回傳結果
int 字串 轉換 整數
str 數字 轉換 字串
print 東西 印出東西 None
input 接收輸入 輸入的東西

輸出輸入

print('Hello World') 

name = input()
print('Hi', name)

是非對錯

True & False

關係

 ==  # 相等
 !=  # 不相等
 >   # 大於
 >=  # 大於等於 

邏輯

 not  # 非 
 and  # 且
 or   # 或

如果

if 結構

if 18.5 <= age < 24: 
    print('你的 BMI …') 
    print('… 正常!') 
  • 冒號
  • 縮排(4個空白)

列表

清單人人愛

List

>>> names = ['NTU', 'OSC']
>>> names[0]
'NTU'
>>> 
>>> first = [1, 'a', ['一', '壹']] 
>>> seven = [1, 4, 2, 8, 5, 7] 
>>> 3 in seven
False
>>> max(seven)
8
>>> sorted(seven)
[1, 2, 4, 5, 7, 8]
>>> a = [1, 5, 3, 7] 
>>> a.append(4)
>>> a
[1, 5, 3, 7, 4]

物件

屬性與方法

一個一個來

for 迴圈

five = [1, 2, 3, 4, 5] 
for i in five:
    print(i)
1   
2   
3   
4   
5   
for i in range(100): 
    print(i)
0    
1    
...   
98   
99   

Python 函式庫

內建工具箱

數學

>>> import math
>>> math.pi
3.141592653589793
>>> math.degrees(math.pi) 
180

隨機

>>> import random
>>> random.randint(1, 6) 
4

時間

>>> import time
>>> time.strftime('%Y/%m/%d %H:%M:%S') 
'2018/9/28 20:45:01'
>>> time.sleep(3)
>>> 

第三方函式庫

更多工具箱!

PyPI

(the Python Package Index)

$> [sudo] pip install ...

$> [sudo] pip3 install ... 

Request

HTTP 連線函式庫

$> pip install requests    
>>> import requests
>>> url = 'https://ntuosc.org'
>>> response = requests.get(url)
>>> 
>>> '臺灣大學開源社' in response.text 
True

Pillow

影像處理函式庫(PIL)的分支

$> pip install Pillow    
>>> from PIL import Image
>>> img = Image.open('Avatar.png') 
>>> img.show()
>>> 
>>> img.resize([300, 300])
<PIL.Image.Image ... > 
>>> gray = img.convert('L')
>>> img90 = img.rotate(90)
>>> 
>>> gray.save('grayAvatar.png')
>>> img90.save('Avatar90.png')

 

>>> from io import BytesIO 
>>> import requests
>>> from PIL import Image
>>> url = 'https://imgur.com/vPQ7yke.png' 
>>> img_bytes = requests.get(url).content
>>> img = Image.open(BytesIO(img_bytes))
>>> img = img.convert('L')
>>> img.save('grayPython.png')

附錄:推薦網站

  • 良葛格學習筆記 | Python 學習筆記(中文)
    https://openhome.cc/Gossip/Python/
  • Python 官方文件(英文)
    https://docs.python.org/3/
  • Python 官方教學文件(英文)
    https://docs.python.org/3/tutorial/

附錄:參考網站

  • PyPI(英文)
    https://pypi.python.org/pypi
  • Requests 官方網站(英文,有簡體中文翻譯)
    http://docs.python-requests.org/en/master/
  • Pillow 官方文件(英文)
    https://pillow.readthedocs.io/en/4.3.x/#

1
{"metaMigratedAt":"2023-06-14T18:14:52.762Z","metaMigratedFrom":"YAML","title":"NTUOSC Python 9/28 社課簡報","breaks":true,"slideOptions":"{\"theme\":\"white\"}","contributors":"[{\"id\":\"5e2f40a8-9959-4c4c-a7da-08c8ade0cf1f\",\"add\":4965,\"del\":152}]"}
   changed 4 years ago 1611 views