<style> .reveal section img { border: none; } </style> ![](https://imgur.com/vPQ7yke.png =600x) --- # 講者 ![](https://imgur.com/fsdnn6m.png =350x) 皮皮:Yu-Ren Pan <span style="font-size:.7em">臺北市立大學 數學系</span> --- # 程式語言 指示程式運作的「劇本」 ---- ![](https://imgur.com/mk8dWp2.png =450x) ---- ![](https://imgur.com/B791BhS.png =600x) --- # 純文字 ---- ## 文字編輯器 - Notepad++ - VIM - Visual Studio Code - 真男人都用 Notepad,沒有 ++ ---- ## 副檔名 ### `.py` --- # 安裝 Python3 ---- <style> .reveal pre { font-size: 1.01em; width: fit-content; } </style> ## 互動界面 - 終端機、命令列 ```shell $> python3 ``` - IDLE(整合開發與學習環境) ---- ```python $> python3 Python 3.7.0 Type ... information. >>> ``` --- # 算數 ```python >>> 1 + 3 4 >>> 11 / 2 5.5 >>> 5 ** 3 125 ``` ---- ```python + # 加 - # 減 * # 乘 / # 除 // # 整數除法 % # 取餘數 ** # 次方 ``` ---- ```python (10 + 3) ** 2 13 ** 2 169 ``` --- # 變數 把資料記下來 ---- ```python >>> a = 10 + 5 >>> a 15 >>> a + 87 102 ``` ---- ```python >>> a = 10 + 5 >>> a 15 >>> a = a + 1 >>> a 16 ``` ---- ```python >>> pi = 3.1415926535 >>> r = 14 >>> pi * r ** 2 615.752160086 ``` --- # 字串 把一堆字串起來 ---- ```python >>> kidd = "我叫做基德,是個" >>> >>> kidd + '律師' '我叫做基德,是個律師' ``` ---- ## 型態轉換 ```python >>> 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` | | 接收輸入 | 輸入的東西 | --- # 輸出輸入 ```python print('Hello World') name = input() print('Hi', name) ``` --- # 是非對錯 ### `True` & `False` ---- ## 關係 ```python == # 相等 != # 不相等 > # 大於 >= # 大於等於 ``` ---- ## 邏輯 ```python not # 非 and # 且 or # 或 ``` --- # 如果 ---- ## `if` 結構 ```python if 18.5 <= age < 24: print('你的 BMI …') print('… 正常!') ``` - 冒號 - 縮排(4個空白) --- # 列表 清單人人愛 ---- ## List ```python >>> names = ['NTU', 'OSC'] >>> names[0] 'NTU' >>> >>> first = [1, 'a', ['一', '壹']] ``` ---- ```python >>> seven = [1, 4, 2, 8, 5, 7] >>> 3 in seven False >>> max(seven) 8 >>> sorted(seven) [1, 2, 4, 5, 7, 8] ``` ---- ```python >>> a = [1, 5, 3, 7] >>> a.append(4) >>> a [1, 5, 3, 7, 4] ``` --- # 物件 屬性與方法 --- # 一個一個來 ---- ## `for` 迴圈 ```python five = [1, 2, 3, 4, 5] for i in five: print(i) ``` ```python 1 2 3 4 5 ``` ---- ```python for i in range(100): print(i) ``` ```python 0 1 ... 98 99 ``` --- # Python 函式庫 內建工具箱 ---- ## 數學 ```python >>> import math >>> math.pi 3.141592653589793 >>> math.degrees(math.pi) 180 ``` ---- ## 隨機 ```python >>> import random >>> random.randint(1, 6) 4 ``` ---- ## 時間 ```python >>> import time >>> time.strftime('%Y/%m/%d %H:%M:%S') '2018/9/28 20:45:01' >>> time.sleep(3) >>> ``` --- # 第三方函式庫 更多工具箱! ---- ## PyPI (the Python Package Index) ```shell $> [sudo] pip install ... $> [sudo] pip3 install ... ``` --- ## Request HTTP 連線函式庫 ![](http://docs.python-requests.org/en/master/_static/requests-sidebar.png =300x) ---- ```shell $> pip install requests ``` ```python >>> import requests >>> url = 'https://ntuosc.org' >>> response = requests.get(url) >>> >>> '臺灣大學開源社' in response.text True ``` --- ## Pillow 影像處理函式庫(PIL)的分支 ![](https://raw.githubusercontent.com/python-pillow/pillow-logo/master/Pillow%20Logo.png =400x) ---- ```shell $> pip install Pillow ``` ```python >>> from PIL import Image >>> img = Image.open('Avatar.png') >>> img.show() >>> >>> img.resize([300, 300]) <PIL.Image.Image ... > ``` ---- ```python >>> gray = img.convert('L') >>> img90 = img.rotate(90) >>> >>> gray.save('grayAvatar.png') >>> img90.save('Avatar90.png') ``` ![](https://imgur.com/tz6RKHS.png =200x) ![](https://imgur.com/8deiOq8.png =200x) --- ```python >>> from io import BytesIO >>> import requests >>> from PIL import Image ``` ```python >>> 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') ``` ---- ![](https://imgur.com/mRDrtZj.png =600x) --- # 附錄:推薦網站 - 良葛格學習筆記 | 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/# ---
{"title":"NTUOSC Python 9/28 社課簡報","tags":"NTUOSC, Python","slideOptions":{"theme":"white"}}
    1017 views