├── LICENSE ├── README.md └── documents ├── .ipynb_checkpoints ├── Class Preface-checkpoint.ipynb ├── Class01 Variable-checkpoint.ipynb ├── Class02 Operator-checkpoint.ipynb ├── Class03 Data Structure-checkpoint.ipynb ├── Class04 String-checkpoint.ipynb ├── Class05 List-checkpoint.ipynb ├── Class06 Tuple-checkpoint.ipynb ├── Class07 Range-checkpoint.ipynb ├── Class08 Set-checkpoint.ipynb ├── Class09 Dictionary-checkpoint.ipynb ├── Class10 If-else-checkpoint.ipynb ├── Class11 While-loop-checkpoint.ipynb ├── Class12 For-loop-checkpoint.ipynb ├── Class13 Function-checkpoint.ipynb ├── Class14 Input-output-checkpoint.ipynb ├── Class15 Class-checkpoint.ipynb ├── Lesson00 Preface-checkpoint.ipynb ├── Lesson01 Variable-checkpoint.ipynb ├── Lesson02 Operator-checkpoint.ipynb ├── Lesson03 Data Structure-checkpoint.ipynb ├── Lesson04 String-checkpoint.ipynb ├── Lesson05 List-checkpoint.ipynb ├── Lesson06 Tuple-checkpoint.ipynb ├── Lesson07 Range-checkpoint.ipynb ├── Lesson08 Set-checkpoint.ipynb ├── Lesson09 Dictionary-checkpoint.ipynb ├── Lesson10 If-else-checkpoint.ipynb ├── Lesson11 While-loop-checkpoint.ipynb ├── Lesson12 For-loop-checkpoint.ipynb ├── Lesson13 Function-checkpoint.ipynb ├── Lesson14 Input-output-checkpoint.ipynb └── Lesson15 Class-checkpoint.ipynb ├── Lesson00 Preface.ipynb ├── Lesson01 Variable.ipynb ├── Lesson02 Operator.ipynb ├── Lesson03 Data Structure.ipynb ├── Lesson04 String.ipynb ├── Lesson05 List.ipynb ├── Lesson06 Tuple.ipynb ├── Lesson07 Range.ipynb ├── Lesson08 Set.ipynb ├── Lesson09 Dictionary.ipynb ├── Lesson10 If-else.ipynb ├── Lesson11 While-loop.ipynb ├── Lesson12 For-loop.ipynb ├── Lesson13 Function.ipynb ├── Lesson14 Input-output.ipynb └── Lesson15 Class.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Python-Basics 2 | ![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png) 3 |
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
4 |

5 |
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的第一堂課,Python基礎教學。
6 |
7 | 8 | ## 系列課程說明 9 | - 《Python 從商業分析到人工智慧》系列課程將透過Google colab進行學習。(Google Colab是什麼?) 10 | 11 | - 本系列課程將免費提供給中文使用者學習資料科學,但不提供第三方做商業用途。(商業合作請洽AI . FREE Team) 12 | 13 | - 本系列課程將從基礎 Python的使用,到人工智慧的開發,讀者歡迎追蹤粉絲專頁並加入自由團隊-學習社團。 14 | 15 |
16 | 17 | ## 本課程說明 - Python Basics 18 | 1. 本課程提供 Python 初學者 進行基礎語法的學習。(學過其他程式語言 or 無程式語言基礎者皆適用) 19 | 20 | 2. 學習本課程不須建置系統環境,僅需要網路連線 & Google Chrome瀏覽器即可開始學習。 21 | 22 | 3. 課程學習過程中,若發現BUG或任何問題,歡迎與自由團隊詢問!(粉絲專頁 or ai.free.team@gmail.com) 23 | 24 | 4. 課程作者/貢獻者: 25 | - Chen Ken (Github) 26 | - Lee Ovien (Github) 27 | - Chuang Shawn (Github) 28 | 29 |
30 | 31 | ## 課程文件 32 | 33 | #### Python Basics 課程清單 34 | - Lesson00 Preface 課程前言 35 | 36 | - Lesson01 Variable 變數 37 | 38 | - Lesson02 Operator 運算子 39 | 40 | - Lesson03 Data Structure 資料結構 41 | 42 | - Lesson04 String 字串 43 | 44 | - Lesson05 List 列表 45 | 46 | - Lesson06 Tuple 元組 47 | 48 | - Lesson07 Range 範圍 49 | 50 | - Lesson08 Set 集合 51 | 52 | - Lesson09 Dictionary 字典 53 | 54 | - Lesson10 If-else 條件語法 55 | 56 | - Lesson11 While 迴圈 57 | 58 | - Lesson12 For 迴圈 59 | 60 | - Lesson13 Function 函式 61 | 62 | - Lesson14 Input/Output 輸入/輸出 63 | 64 | - Lesson15 Class 類別 65 | 66 |
67 | 68 | ## License 授權 69 | 70 | 71 | Creative Commons License (CC BY-NC-SA 4.0)
本教學課程適用 Attribution-NonCommercial-ShareAlike 4.0 International 授權方式。 72 | 73 | ※ 轉載、改作、分享請附上以下內容: 74 | 75 | - 本課程由 AI . FREE Team 原創開發。如有轉載、改作、分享,請註明出處。 76 | 77 | - The course is AI . FREE Team original production. If reproduced, modified, or shared, please cite the source. 78 | 79 | - (source: https://github.com/AI-FREE-Team/Python-Basics ) 80 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class Preface-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "Rib_rBYNuDI3" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 13 | "
\n", 14 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 0 前言 (1-0)。
\n", 15 | "
\n", 16 | "\n", 17 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n", 18 | "\n", 19 | "\n", 20 | "\n", 21 | "\n", 22 | "\n" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": { 28 | "colab_type": "text", 29 | "id": "0LEJHkfYJTNU" 30 | }, 31 | "source": [ 32 | "# Preface 前言" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": { 38 | "colab_type": "text", 39 | "id": "lo-XP-5gAYLW" 40 | }, 41 | "source": [ 42 | "## 1. Why Python? 為何選擇 Python?" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": { 48 | "colab_type": "text", 49 | "id": "yf6Hp8n9GXAg" 50 | }, 51 | "source": [ 52 | "在眾多的程式語言當中,我們選擇 Python 做為初學者入門數據分析、人工智慧的語言,而不是 R, C, Matlab or Java 等其他語言,相對這些語言來說,Python 有以下優勢:\n", 53 | "\n", 54 | "\n", 55 | "1. 學習快速 (Python 屬於高階語言,較能使開發者快速讀懂程式碼)\n", 56 | "2. 資源豐富 (Python 的開源開發社群廣大,許多國際企業也都公開自家的開源套件;例如:Google, Facebook)\n", 57 | "3. 用途廣泛 (Python 不僅能作為商業分析、開發 AI 的工具、應用在科研領域,還能做為開發網頁的框架,如Django...)\n", 58 | "\n" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": { 64 | "colab_type": "text", 65 | "id": "D0XaUjY4JiZ9" 66 | }, 67 | "source": [ 68 | "## 2. Why Colab? 為何選擇 Colab 介面進行學習?" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": { 74 | "colab_type": "text", 75 | "id": "Bp2ZDcoyJtnu" 76 | }, 77 | "source": [ 78 | "Colab 為 Google 推出的免費雲端 Python 程式語言開發介面 (基於Jupyter notebook environment 開發),優點如下:\n", 79 | "\n", 80 | "\n", 81 | "1. 不用花費大量時間設定「開發環境」\n", 82 | "2. 提供許多整合性服務 (與Google Drive連結、Github開發社群、相關 Python 開源套件導入...)\n", 83 | "3. 提供免費雲端運算資源 (GPU, TPU 能使開發 AI 運算速度更快)\n", 84 | "4. 高度便利性 (可隨時隨地學習 -- 在有網路 & 電腦的狀況下)\n", 85 | "\n", 86 | "提供給讀者們: Google 官方 Colab 介紹文件 (https://colab.research.google.com/notebooks/welcome.ipynb)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": { 92 | "colab_type": "text", 93 | "id": "QA90qYogNbjg" 94 | }, 95 | "source": [ 96 | "# 第一堂課 Python Basics 基礎教學" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": { 102 | "colab_type": "text", 103 | "id": "sZPa1lEfnDcw" 104 | }, 105 | "source": [ 106 | "## 範例教學 - Print\n" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": { 112 | "colab_type": "text", 113 | "id": "xl_obAOHOAQz" 114 | }, 115 | "source": [ 116 | "一如程式語言學習的傳統,我們先從 Print 開始;Print 就如字面上意思,將東西列印出來。 (請以 Ctrl + Enter 執行下方程式語言列)\n", 117 | "\n", 118 | "> Print(\"Hello World!\")\n", 119 | "\n", 120 | "\n" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 1, 126 | "metadata": { 127 | "colab": {}, 128 | "colab_type": "code", 129 | "id": "jOBwRHanNk7Q" 130 | }, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "Hello World!\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "print(\"Hello World!\")" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": { 147 | "colab_type": "text", 148 | "id": "XF4mEjBHOtQs" 149 | }, 150 | "source": [ 151 | "執行成功後,你將看到:\n", 152 | "\n", 153 | "```\n", 154 | "Hello World!\n", 155 | "```\n", 156 | " * \"print()\" 的使用為程式語言開發的基礎工具,還有許多不同的使用方式,請多嘗試看看不同使用方法。\n", 157 | "\n", 158 | "\n" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "## 課程文件\n", 166 | "\n", 167 | "#### Python Basics 課程清單\n", 168 | "- Lesson00 Preface 課程前言 (We are here now! --本篇課程--)\n", 169 | "\n", 170 | "- Lesson01 Variable 變數 (Next Chapter! --下一堂課程--)\n", 171 | "\n", 172 | "- Lesson02 Operator 運算子\n", 173 | "\n", 174 | "- Lesson03 Data Structure 資料結構\n", 175 | "\n", 176 | "- Lesson04 String 字串\n", 177 | "\n", 178 | "- Lesson05 List 列表\n", 179 | "\n", 180 | "- Lesson06 Tuple 元組\n", 181 | "\n", 182 | "- Lesson07 Range 範圍\n", 183 | "\n", 184 | "- Lesson08 Set 集合\n", 185 | "\n", 186 | "- Lesson09 Dictionary 字典\n", 187 | "\n", 188 | "- Lesson10 If-else 條件語法\n", 189 | "\n", 190 | "- Lesson11 While 迴圈\n", 191 | "\n", 192 | "- Lesson12 For 迴圈\n", 193 | "\n", 194 | "- Lesson13 Function 函式\n", 195 | "\n", 196 | "- Lesson14 Input/Output 輸入/輸出\n", 197 | "\n", 198 | "- Lesson15 Class 類別" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [] 207 | } 208 | ], 209 | "metadata": { 210 | "accelerator": "GPU", 211 | "colab": { 212 | "collapsed_sections": [ 213 | "lo-XP-5gAYLW", 214 | "D0XaUjY4JiZ9", 215 | "sZPa1lEfnDcw", 216 | "z8a-OLPrnKUQ" 217 | ], 218 | "name": "AI . FREE Team - Python 從商業分析到人工智慧_第一堂課.ipynb", 219 | "provenance": [], 220 | "toc_visible": true, 221 | "version": "0.3.2" 222 | }, 223 | "kernelspec": { 224 | "display_name": "Python 3", 225 | "language": "python", 226 | "name": "python3" 227 | }, 228 | "language_info": { 229 | "codemirror_mode": { 230 | "name": "ipython", 231 | "version": 3 232 | }, 233 | "file_extension": ".py", 234 | "mimetype": "text/x-python", 235 | "name": "python", 236 | "nbconvert_exporter": "python", 237 | "pygments_lexer": "ipython3", 238 | "version": "3.7.3" 239 | } 240 | }, 241 | "nbformat": 4, 242 | "nbformat_minor": 1 243 | } 244 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class01 Variable-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "Rib_rBYNuDI3" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 01 變數 (1-01)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n", 19 | "\n", 20 | "\n", 21 | "\n", 22 | "\n", 23 | "\n" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "colab_type": "text", 30 | "id": "z8a-OLPrnKUQ" 31 | }, 32 | "source": [ 33 | "## Variables 變數認識" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": { 39 | "colab_type": "text", 40 | "id": "JN8bNrZYoBZU" 41 | }, 42 | "source": [ 43 | "變數是什麼呢?相信大家並不陌生,這邊舉 BMI 計算為例,讓大家初步認識變數在程式語言開發中的使用。" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": { 49 | "colab_type": "text", 50 | "id": "ERLX_MhjoMoj" 51 | }, 52 | "source": [ 53 | "\n", 54 | "\n", 55 | "* **先訂定第一個變數 height(高度) 為 1.8 (m)** (請按 Ctrl + enter 執行下列)\n", 56 | "\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 0, 62 | "metadata": { 63 | "colab": {}, 64 | "colab_type": "code", 65 | "id": "zK8aYkk2Omty" 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "height = 1.8" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": { 75 | "colab_type": "text", 76 | "id": "UgH2R4qlpwyv" 77 | }, 78 | "source": [ 79 | "* **訂定第二個變數 weight(體重) 為 80 (kg**) (請按 Ctrl + enter 執行下列)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 0, 85 | "metadata": { 86 | "colab": {}, 87 | "colab_type": "code", 88 | "id": "3tyHvKRYmZNV" 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "weight = 80" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": { 98 | "colab_type": "text", 99 | "id": "fnqaHdCTqasC" 100 | }, 101 | "source": [ 102 | "* 套入計算 BMI 的公式: **體重(kg) / 身高的平方(m * m)** (請按 Ctrl + enter 執行下列)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 0, 108 | "metadata": { 109 | "colab": {}, 110 | "colab_type": "code", 111 | "id": "wsF0GziCqWhK" 112 | }, 113 | "outputs": [], 114 | "source": [ 115 | "BMI = weight / (height*height)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": { 121 | "colab_type": "text", 122 | "id": "mb6QzyrMrEjj" 123 | }, 124 | "source": [ 125 | "* 查看計算結果 (以print來進行查看)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 0, 131 | "metadata": { 132 | "colab": { 133 | "base_uri": "https://localhost:8080/", 134 | "height": 68 135 | }, 136 | "colab_type": "code", 137 | "executionInfo": { 138 | "elapsed": 1258, 139 | "status": "ok", 140 | "timestamp": 1565280877753, 141 | "user": { 142 | "displayName": "電機工程系莊翔宇", 143 | "photoUrl": "", 144 | "userId": "06232090834761520364" 145 | }, 146 | "user_tz": -480 147 | }, 148 | "id": "m8u7au2PrDBM", 149 | "outputId": "93360dbe-8475-4ea0-ad92-bf3d6de448a6" 150 | }, 151 | "outputs": [ 152 | { 153 | "name": "stdout", 154 | "output_type": "stream", 155 | "text": [ 156 | "您的身高為: 1.8m (公尺)\n", 157 | "您的體重為: 80kg (公斤)\n", 158 | "您的BMI為: 24.691358024691358\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "print(\"您的身高為: \" + str(height) + \"m (公尺)\")\n", 164 | "print(\"您的體重為: \" + str(weight) + \"kg (公斤)\")\n", 165 | "print(\"您的BMI為: \" + str(BMI))" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": { 171 | "colab_type": "text", 172 | "id": "LKacVqLosLLx" 173 | }, 174 | "source": [ 175 | "* 以上簡單製作出 BMI 的計算工具,請嘗試看看修改身高 or 體重,來體會如何使用變數!" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": { 181 | "colab_type": "text", 182 | "id": "1_praOaBX-i7" 183 | }, 184 | "source": [ 185 | "* 變數可以是任何型態,且大小寫有區分。" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 0, 191 | "metadata": { 192 | "colab": { 193 | "base_uri": "https://localhost:8080/", 194 | "height": 51 195 | }, 196 | "colab_type": "code", 197 | "executionInfo": { 198 | "elapsed": 1151, 199 | "status": "ok", 200 | "timestamp": 1565280883892, 201 | "user": { 202 | "displayName": "電機工程系莊翔宇", 203 | "photoUrl": "", 204 | "userId": "06232090834761520364" 205 | }, 206 | "user_tz": -480 207 | }, 208 | "id": "rF-pX6KbIMRL", 209 | "outputId": "3c196431-51d0-474a-8f79-7bca1c3c52ce" 210 | }, 211 | "outputs": [ 212 | { 213 | "name": "stdout", 214 | "output_type": "stream", 215 | "text": [ 216 | "Hello Word\n", 217 | "a A\n" 218 | ] 219 | } 220 | ], 221 | "source": [ 222 | "str1='Hello'\n", 223 | "str2='Word'\n", 224 | "i=100\n", 225 | "a='a'\n", 226 | "A='A'\n", 227 | "print(str1+' '+str2)\n", 228 | "print(a,A)" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": { 234 | "colab_type": "text", 235 | "id": "ybAqJR3JKXQR" 236 | }, 237 | "source": [ 238 | "* 變數開頭不能為數字,不能使用保留字,ex:if、for、True、in、return..。" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 0, 244 | "metadata": { 245 | "colab": { 246 | "base_uri": "https://localhost:8080/", 247 | "height": 131 248 | }, 249 | "colab_type": "code", 250 | "executionInfo": { 251 | "elapsed": 1209, 252 | "status": "error", 253 | "timestamp": 1565280887074, 254 | "user": { 255 | "displayName": "電機工程系莊翔宇", 256 | "photoUrl": "", 257 | "userId": "06232090834761520364" 258 | }, 259 | "user_tz": -480 260 | }, 261 | "id": "tuAC8WFtKkth", 262 | "outputId": "1cc9b45a-5388-4bb9-b55f-8e53748c41a3" 263 | }, 264 | "outputs": [ 265 | { 266 | "ename": "SyntaxError", 267 | "evalue": "ignored", 268 | "output_type": "error", 269 | "traceback": [ 270 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m if=2\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 271 | ] 272 | } 273 | ], 274 | "source": [ 275 | "5=5\n", 276 | "if=2\n", 277 | "for=3" 278 | ] 279 | }, 280 | { 281 | "cell_type": "markdown", 282 | "metadata": { 283 | "colab_type": "text", 284 | "id": "TtEknGI82pkG" 285 | }, 286 | "source": [ 287 | "* 以上這些為python內建的變數,所以當你是用這些當做變數時,將不會具有變數的屬性(賦值、運算等等的...)" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": { 293 | "colab_type": "text", 294 | "id": "4n-jQS9-3MDx" 295 | }, 296 | "source": [ 297 | "* 而這些內建的變數,將在後面的章節逐一介紹" 298 | ] 299 | } 300 | ], 301 | "metadata": { 302 | "accelerator": "GPU", 303 | "colab": { 304 | "collapsed_sections": [], 305 | "name": "course1:Variables.ipynb", 306 | "provenance": [], 307 | "toc_visible": true, 308 | "version": "0.3.2" 309 | }, 310 | "kernelspec": { 311 | "display_name": "Python 3", 312 | "language": "python", 313 | "name": "python3" 314 | }, 315 | "language_info": { 316 | "codemirror_mode": { 317 | "name": "ipython", 318 | "version": 3 319 | }, 320 | "file_extension": ".py", 321 | "mimetype": "text/x-python", 322 | "name": "python", 323 | "nbconvert_exporter": "python", 324 | "pygments_lexer": "ipython3", 325 | "version": "3.7.3" 326 | } 327 | }, 328 | "nbformat": 4, 329 | "nbformat_minor": 1 330 | } 331 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class02 Operator-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "Bi-PSCAZKjdk" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the first course of《Python: from business analytics to Artificial Intelligence》--【Python Basics】, by AI . FREE Team.
\n", 14 | "\n", 15 | "
\n", 16 | "\n", 17 | "
歡迎大家來到 AI . FREE Team 《Pyhon 從商業分析到人工智慧》的第一堂課,Python 基礎教學!
\n", 18 | "\n", 19 | "
\n", 20 | "\n", 21 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "_DeL-QEpKohy" 29 | }, 30 | "source": [ 31 | "# 運算子(Operator)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "RbtpSepcLFaX" 39 | }, 40 | "source": [ 41 | "* 這個章節,我們將教導什麼事運算子,並用一些實例來引導大家了解這個單元" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": { 47 | "colab_type": "text", 48 | "id": "8E9DzMEPKodT" 49 | }, 50 | "source": [ 51 | "* 基本運算" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 0, 57 | "metadata": { 58 | "colab": {}, 59 | "colab_type": "code", 60 | "id": "pPUp6RjVKU_C" 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "a=1\n", 65 | "b=2\n", 66 | "c=a+b\n", 67 | "d=a-b\n", 68 | "e=a*b" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": { 74 | "colab_type": "text", 75 | "id": "BTErLy4qLcBC" 76 | }, 77 | "source": [ 78 | "* 並透過print()來檢查" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 0, 84 | "metadata": { 85 | "colab": {}, 86 | "colab_type": "code", 87 | "id": "G68ma_l4LbWV" 88 | }, 89 | "outputs": [], 90 | "source": [ 91 | "print(\"a\",a)\n", 92 | "print(\"b\",b)\n", 93 | "print(\"c\",c)\n", 94 | "print(\"d\",d)\n", 95 | "print(\"e\",e)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": { 101 | "colab_type": "text", 102 | "id": "5cTk0UziKzjd" 103 | }, 104 | "source": [ 105 | "* 除法需要特別注意,/為取商,%為取餘數。" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 0, 111 | "metadata": { 112 | "colab": {}, 113 | "colab_type": "code", 114 | "id": "r28ZmHbjK5bp" 115 | }, 116 | "outputs": [], 117 | "source": [ 118 | "a=2/3\n", 119 | "b=2%3\n", 120 | "print(\"a\",a)\n", 121 | "print(\"b\",b)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": { 127 | "colab_type": "text", 128 | "id": "h6k1giEoK9gv" 129 | }, 130 | "source": [ 131 | "* 邏輯運算,回傳為 True 或 False。" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 0, 137 | "metadata": { 138 | "colab": {}, 139 | "colab_type": "code", 140 | "id": "KVK9XssqLAr2" 141 | }, 142 | "outputs": [], 143 | "source": [ 144 | "a==b #a是否等於b\n", 145 | "a>b #a是否大於b\n", 146 | "a=b #a是否大於等於b\n", 148 | "a<=b #a是否小於等於b\n", 149 | "a!=b #a是否不等於b" 150 | ] 151 | } 152 | ], 153 | "metadata": { 154 | "colab": { 155 | "collapsed_sections": [], 156 | "name": "course2:Operator.ipynb", 157 | "provenance": [], 158 | "version": "0.3.2" 159 | }, 160 | "kernelspec": { 161 | "display_name": "Python 3", 162 | "language": "python", 163 | "name": "python3" 164 | }, 165 | "language_info": { 166 | "codemirror_mode": { 167 | "name": "ipython", 168 | "version": 3 169 | }, 170 | "file_extension": ".py", 171 | "mimetype": "text/x-python", 172 | "name": "python", 173 | "nbconvert_exporter": "python", 174 | "pygments_lexer": "ipython3", 175 | "version": "3.7.3" 176 | } 177 | }, 178 | "nbformat": 4, 179 | "nbformat_minor": 1 180 | } 181 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class05 List-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "S4F7QCIWGiLv" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the first course of《Python: from business analytics to Artificial Intelligence》--【Python Basics】, by AI . FREE Team.
\n", 14 | "\n", 15 | "
\n", 16 | "\n", 17 | "
歡迎大家來到 AI . FREE Team 《Pyhon 從商業分析到人工智慧》的第一堂課,Python 基礎教學!
\n", 18 | "\n", 19 | "
\n", 20 | "\n", 21 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "TLv_LsGDGma8" 29 | }, 30 | "source": [ 31 | "# List(陣列)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "VuApaOlPHnxt" 39 | }, 40 | "source": [ 41 | "* 就像C一樣,陣列是Python中最基本的數據結構。陣列中的每個元素都分配一個數字-它的位置或索引,第一個索引是0,第二個索引是1,依此類推。\n", 42 | "* Python有6個陣列的內置類型,但最常見的是列表和元組。\n", 43 | "* 陣列都可以進行的操作包括索引,切片,加,乘,檢查成員。" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 0, 49 | "metadata": { 50 | "colab": { 51 | "base_uri": "https://localhost:8080/", 52 | "height": 68 53 | }, 54 | "colab_type": "code", 55 | "executionInfo": { 56 | "elapsed": 1281, 57 | "status": "ok", 58 | "timestamp": 1566054826104, 59 | "user": { 60 | "displayName": "電機工程系李佑謙", 61 | "photoUrl": "", 62 | "userId": "05735385076343474033" 63 | }, 64 | "user_tz": -480 65 | }, 66 | "id": "TTxT3byHFrgC", 67 | "outputId": "6db99c59-00af-4734-caa8-bd18adb7ca2b" 68 | }, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "['C', 'Pyhotn', 1969, 1991]\n", 75 | "[1, 2, 3, 4, 5]\n", 76 | "['A', 'B', 'C', 'D']\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "list1 = ['C','Pyhotn',1969,1991]\n", 82 | "print(list1)\n", 83 | "list2 = [1, 2, 3, 4, 5 ]\n", 84 | "print(list2)\n", 85 | "list3 = [\"A\",\"B\",\"C\",\"D\"]\n", 86 | "print(list3)\n", 87 | "# 與字串的索引一樣,陣列索引從0開始。" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": { 93 | "colab_type": "text", 94 | "id": "A32JEE98H1CU" 95 | }, 96 | "source": [ 97 | "* 使用下標索引來訪問陣列中的值,同樣你也可以使用方括號的形式截取字符,如下所示:\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 0, 103 | "metadata": { 104 | "colab": { 105 | "base_uri": "https://localhost:8080/", 106 | "height": 51 107 | }, 108 | "colab_type": "code", 109 | "executionInfo": { 110 | "elapsed": 696, 111 | "status": "ok", 112 | "timestamp": 1566054980894, 113 | "user": { 114 | "displayName": "電機工程系李佑謙", 115 | "photoUrl": "", 116 | "userId": "05735385076343474033" 117 | }, 118 | "user_tz": -480 119 | }, 120 | "id": "Wg0XxkeiJXcW", 121 | "outputId": "98e3a185-ab69-4114-8757-9a12019b9def" 122 | }, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "list1[0]: C\n", 129 | "list2[1:5]: [2, 3, 4, 5]\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "print(\"list1[0]: \",list1[0])\n", 135 | "print(\"list2[1:5]: \",list2[1:5])" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": { 141 | "colab_type": "text", 142 | "id": "KapqHdhLKYm7" 143 | }, 144 | "source": [ 145 | "## 更新陣列元素" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": { 151 | "colab_type": "text", 152 | "id": "ySs9bYqNJwTr" 153 | }, 154 | "source": [ 155 | "* 你可以對陣列的數據項進行修改或更新,你也可以使用append()方法來添加列表項,如下所示:" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 0, 161 | "metadata": { 162 | "colab": { 163 | "base_uri": "https://localhost:8080/", 164 | "height": 34 165 | }, 166 | "colab_type": "code", 167 | "executionInfo": { 168 | "elapsed": 646, 169 | "status": "ok", 170 | "timestamp": 1566055355761, 171 | "user": { 172 | "displayName": "電機工程系李佑謙", 173 | "photoUrl": "", 174 | "userId": "05735385076343474033" 175 | }, 176 | "user_tz": -480 177 | }, 178 | "id": "7tllHerYJ3Fx", 179 | "outputId": "b5bf1eec-2a3b-435e-bf1d-c9118e3a9903" 180 | }, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "['1991', 'Python']\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "test=[] ## 空列表\n", 192 | "test.append('1991') ## 使用 append() 添加元素\n", 193 | "test.append('Python')\n", 194 | "print(test)" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": { 200 | "colab_type": "text", 201 | "id": "ZIEm2EhfKVPv" 202 | }, 203 | "source": [ 204 | "## 刪除陣列元素" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 0, 210 | "metadata": { 211 | "colab": { 212 | "base_uri": "https://localhost:8080/", 213 | "height": 51 214 | }, 215 | "colab_type": "code", 216 | "executionInfo": { 217 | "elapsed": 669, 218 | "status": "ok", 219 | "timestamp": 1566055365291, 220 | "user": { 221 | "displayName": "電機工程系李佑謙", 222 | "photoUrl": "", 223 | "userId": "05735385076343474033" 224 | }, 225 | "user_tz": -480 226 | }, 227 | "id": "ZxhqPrS0KlGn", 228 | "outputId": "6e6959b4-d142-4511-f153-6995d768df3c" 229 | }, 230 | "outputs": [ 231 | { 232 | "name": "stdout", 233 | "output_type": "stream", 234 | "text": [ 235 | "['Python']\n", 236 | "[]\n" 237 | ] 238 | } 239 | ], 240 | "source": [ 241 | "del(test[0])\n", 242 | "print(test)\n", 243 | "del(test[0])\n", 244 | "print(test)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": { 250 | "colab_type": "text", 251 | "id": "-8YuT7T7LIa3" 252 | }, 253 | "source": [ 254 | "## Pyhton操作符號" 255 | ] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": { 260 | "colab_type": "text", 261 | "id": "FmOg8DHZLQxv" 262 | }, 263 | "source": [ 264 | "* 陣列對 + 和 * 的操作符與字符串相似。+ 號用於組合陣列,* 號用於重複陣列。\n", 265 | "\n", 266 | "* 如下所示:" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 0, 272 | "metadata": { 273 | "colab": { 274 | "base_uri": "https://localhost:8080/", 275 | "height": 136 276 | }, 277 | "colab_type": "code", 278 | "executionInfo": { 279 | "elapsed": 654, 280 | "status": "ok", 281 | "timestamp": 1566055671134, 282 | "user": { 283 | "displayName": "電機工程系李佑謙", 284 | "photoUrl": "", 285 | "userId": "05735385076343474033" 286 | }, 287 | "user_tz": -480 288 | }, 289 | "id": "ZuFDWjzZLOe8", 290 | "outputId": "ad4b4d9c-4b99-4751-8411-31ff24e9af88" 291 | }, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "3\n", 298 | "[1, 2, 3, 4, 5, 6]\n", 299 | "['Hi!', 'Hi!', 'Hi!', 'Hi!']\n", 300 | "True\n", 301 | "1\n", 302 | "2\n", 303 | "3\n" 304 | ] 305 | } 306 | ], 307 | "source": [ 308 | "print(len([1, 2, 3]))\t# 長度\n", 309 | "\n", 310 | "print([1, 2, 3] + [4, 5, 6])\t# 組合\n", 311 | "\n", 312 | "print(['Hi!'] * 4) #\t重複\n", 313 | "\n", 314 | "print(3 in [1, 2, 3]) #\t元素是否存在於列表中 \n", 315 | "\n", 316 | "for x in [1, 2, 3]: \n", 317 | " print(x) #\t1 2 3\t迭代" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": { 323 | "colab_type": "text", 324 | "id": "QTD-iDLBMTMi" 325 | }, 326 | "source": [ 327 | "## Python陣列截取" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": { 333 | "colab_type": "text", 334 | "id": "JyE8HOU_MdbG" 335 | }, 336 | "source": [ 337 | "* Python的陣列截取實例如下:" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 0, 343 | "metadata": { 344 | "colab": { 345 | "base_uri": "https://localhost:8080/", 346 | "height": 68 347 | }, 348 | "colab_type": "code", 349 | "executionInfo": { 350 | "elapsed": 664, 351 | "status": "ok", 352 | "timestamp": 1566055843494, 353 | "user": { 354 | "displayName": "電機工程系李佑謙", 355 | "photoUrl": "", 356 | "userId": "05735385076343474033" 357 | }, 358 | "user_tz": -480 359 | }, 360 | "id": "1hCrJ4QgMlBI", 361 | "outputId": "538cca3f-1bbe-4792-e6b0-2d9285c8bc99" 362 | }, 363 | "outputs": [ 364 | { 365 | "name": "stdout", 366 | "output_type": "stream", 367 | "text": [ 368 | "cat\n", 369 | "book\n", 370 | "['book', 'cat']\n" 371 | ] 372 | } 373 | ], 374 | "source": [ 375 | "list1 = ['apple','book','cat']\n", 376 | "print(list1[2]) # 讀取陣列中第三個元素\n", 377 | "print(list1[-2]) # 讀取陣列中倒數第二個元素\n", 378 | "print(list1[1:]) # 從第二個元素開始截取陣列" 379 | ] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "metadata": { 384 | "colab_type": "text", 385 | "id": "BF6ydYAOPOFA" 386 | }, 387 | "source": [ 388 | "* 已完成此章節,恭喜!" 389 | ] 390 | } 391 | ], 392 | "metadata": { 393 | "colab": { 394 | "collapsed_sections": [], 395 | "name": "course5:List.ipynb", 396 | "provenance": [], 397 | "version": "0.3.2" 398 | }, 399 | "kernelspec": { 400 | "display_name": "Python 3", 401 | "language": "python", 402 | "name": "python3" 403 | }, 404 | "language_info": { 405 | "codemirror_mode": { 406 | "name": "ipython", 407 | "version": 3 408 | }, 409 | "file_extension": ".py", 410 | "mimetype": "text/x-python", 411 | "name": "python", 412 | "nbconvert_exporter": "python", 413 | "pygments_lexer": "ipython3", 414 | "version": "3.7.3" 415 | } 416 | }, 417 | "nbformat": 4, 418 | "nbformat_minor": 1 419 | } 420 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class06 Tuple-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "S4F7QCIWGiLv" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the first course of《Python: from business analytics to Artificial Intelligence》--【Python Basics】, by AI . FREE Team.
\n", 14 | "\n", 15 | "
\n", 16 | "\n", 17 | "
歡迎大家來到 AI . FREE Team 《Pyhon 從商業分析到人工智慧》的第一堂課,Python 基礎教學!
\n", 18 | "\n", 19 | "
\n", 20 | "\n", 21 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "NEXeKL7S78Ab" 29 | }, 30 | "source": [ 31 | "# Tuple ( 元組 )" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "9m6dunQa8b-S" 39 | }, 40 | "source": [ 41 | "\n", 42 | "\n", 43 | "* 跟前一堂課提到的List (串列) 很像。\n", 44 | "* 那麼你也許會好奇這堂課說的跟List (串列) 有甚麼不同呢?\n", 45 | "* tuple 的元素值不可以修改! 不可以刪除! 因此沒有像list有一些改變元素值的方法( 例如:list.pop()、list.reverse()......等 )。\n" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": { 51 | "colab_type": "text", 52 | "id": "chHGjhunFktm" 53 | }, 54 | "source": [ 55 | "* 以下是一些基本的宣告方式" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 0, 61 | "metadata": { 62 | "colab": { 63 | "base_uri": "https://localhost:8080/", 64 | "height": 85 65 | }, 66 | "colab_type": "code", 67 | "executionInfo": { 68 | "elapsed": 660, 69 | "status": "ok", 70 | "timestamp": 1566139127173, 71 | "user": { 72 | "displayName": "電機工程系莊翔宇", 73 | "photoUrl": "", 74 | "userId": "06232090834761520364" 75 | }, 76 | "user_tz": -480 77 | }, 78 | "id": "UHZig47U71HF", 79 | "outputId": "13ea4870-52b1-4e9b-c048-f637543e0a65" 80 | }, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "tuple1: () 0\n", 87 | "tuple2: () 0\n", 88 | "\n", 89 | "tuple3: (1,) 1\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "tuple1 = ()\n", 95 | "print(\"tuple1: \",tuple1, len(tuple1))\n", 96 | "tuple2 = tuple()\n", 97 | "print(\"tuple2: \",tuple2, len(tuple2))\n", 98 | "print()\n", 99 | "tuple3 = (1,)\n", 100 | "print(\"tuple3: \",tuple3, len(tuple3))\n" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 0, 106 | "metadata": { 107 | "colab": { 108 | "base_uri": "https://localhost:8080/", 109 | "height": 136 110 | }, 111 | "colab_type": "code", 112 | "executionInfo": { 113 | "elapsed": 667, 114 | "status": "ok", 115 | "timestamp": 1566139130486, 116 | "user": { 117 | "displayName": "電機工程系莊翔宇", 118 | "photoUrl": "", 119 | "userId": "06232090834761520364" 120 | }, 121 | "user_tz": -480 122 | }, 123 | "id": "hfEVF4hGCx8V", 124 | "outputId": "aef80721-357b-4eb8-c07a-b69067bfe7a9" 125 | }, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "tuple4: (1, 2, 3) 3\n", 132 | "tuple4: 1 2 3\n", 133 | "tuple4[0:2]: (1, 2)\n", 134 | "\n", 135 | "二維的宣告方式\n", 136 | "tuple5: ((1, 2, 3), (4, 5, 6)) 2\n", 137 | "tuple[1][2]: 6\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "tuple4 = (1,2,3)\n", 143 | "print(\"tuple4: \",tuple4, len(tuple4))\n", 144 | "print(\"tuple4: \",tuple4[0], tuple4[1],tuple4[2])\n", 145 | "print(\"tuple4[0:2]: \",tuple4[0:2])\n", 146 | "print(\"\\n二維的宣告方式\")\n", 147 | "tuple5 = ((1,2,3),(4,5,6))\n", 148 | "print(\"tuple5: \",tuple5, len(tuple5))\n", 149 | "print(\"tuple[1][2]: \",tuple5[1][2])" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": { 155 | "colab_type": "text", 156 | "id": "PP7ERo70GPD7" 157 | }, 158 | "source": [ 159 | "* 來試試看list()可以用的方式,使用在 Tuple 上" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 0, 165 | "metadata": { 166 | "colab": { 167 | "base_uri": "https://localhost:8080/", 168 | "height": 182 169 | }, 170 | "colab_type": "code", 171 | "executionInfo": { 172 | "elapsed": 643, 173 | "status": "error", 174 | "timestamp": 1566139132722, 175 | "user": { 176 | "displayName": "電機工程系莊翔宇", 177 | "photoUrl": "", 178 | "userId": "06232090834761520364" 179 | }, 180 | "user_tz": -480 181 | }, 182 | "id": "XnPhmtjBEF3U", 183 | "outputId": "2e5dd204-e25b-4d97-803a-845c4fb6447b" 184 | }, 185 | "outputs": [ 186 | { 187 | "ename": "AttributeError", 188 | "evalue": "ignored", 189 | "output_type": "error", 190 | "traceback": [ 191 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 192 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 193 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mtest\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m## 空元組\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtest\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'1991'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m## 使用 append() 添加元素\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 194 | "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "test = () ## 空元組\n", 200 | "test.append('1991') ## 使用 append() 添加元素" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": { 206 | "colab_type": "text", 207 | "id": "uaNvHTSOGv9G" 208 | }, 209 | "source": [ 210 | "\n", 211 | "\n", 212 | "* 執行後發現 **AttributeError: 'tuple' object has no attribute 'append'**\n", 213 | "* 可知 tuple 沒有這個方法\n", 214 | "* 那麼要怎麼修改/新增值呢\n", 215 | "\n" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 0, 221 | "metadata": { 222 | "colab": { 223 | "base_uri": "https://localhost:8080/", 224 | "height": 68 225 | }, 226 | "colab_type": "code", 227 | "executionInfo": { 228 | "elapsed": 874, 229 | "status": "ok", 230 | "timestamp": 1566139134715, 231 | "user": { 232 | "displayName": "電機工程系莊翔宇", 233 | "photoUrl": "", 234 | "userId": "06232090834761520364" 235 | }, 236 | "user_tz": -480 237 | }, 238 | "id": "so3GMmpdGNue", 239 | "outputId": "edcd1fe1-9f41-46a5-b5b2-d5700fc51187" 240 | }, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "(12, 34.56)\n", 247 | "('abc', 'xyz')\n", 248 | "(12, 34.56, 'abc', 'xyz')\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "tup1 = (12, 34.56)\n", 254 | "print(tup1)\n", 255 | "tup2 = ('abc', 'xyz')\n", 256 | "print(tup2)\n", 257 | "tup3 = tup1 + tup2\n", 258 | "print(tup3)" 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "metadata": { 264 | "colab_type": "text", 265 | "id": "OuP_GoaKItOh" 266 | }, 267 | "source": [ 268 | "* 而移除則是用 del\n", 269 | "\n" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 0, 275 | "metadata": { 276 | "colab": { 277 | "base_uri": "https://localhost:8080/", 278 | "height": 233 279 | }, 280 | "colab_type": "code", 281 | "executionInfo": { 282 | "elapsed": 612, 283 | "status": "error", 284 | "timestamp": 1566139135078, 285 | "user": { 286 | "displayName": "電機工程系莊翔宇", 287 | "photoUrl": "", 288 | "userId": "06232090834761520364" 289 | }, 290 | "user_tz": -480 291 | }, 292 | "id": "LptUY2OBIKcv", 293 | "outputId": "19f6af74-7e49-4656-aace-4ef5fd5f8dea" 294 | }, 295 | "outputs": [ 296 | { 297 | "name": "stdout", 298 | "output_type": "stream", 299 | "text": [ 300 | "('physics', 'chemistry', 1997, 2000)\n", 301 | "After deleting tup : \n" 302 | ] 303 | }, 304 | { 305 | "ename": "NameError", 306 | "evalue": "ignored", 307 | "output_type": "error", 308 | "traceback": [ 309 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 310 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 311 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mtup\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"After deleting tup : \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtup\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#刪除之後 tup會是not defined\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 312 | "\u001b[0;31mNameError\u001b[0m: name 'tup' is not defined" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "tup = ('physics', 'chemistry', 1997, 2000)\n", 318 | "print(tup)\n", 319 | "del tup\n", 320 | "print(\"After deleting tup : \")\n", 321 | "print(tup) #刪除之後 tup會是not defined" 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": { 327 | "colab_type": "text", 328 | "id": "aYgy8esWdSlE" 329 | }, 330 | "source": [ 331 | "## tuple 的優點" 332 | ] 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "metadata": { 337 | "colab_type": "text", 338 | "id": "kfEeqlABJw82" 339 | }, 340 | "source": [ 341 | "* 也由於 tuple 的元素不可修改,結構簡單,因此此資料型態的大小比 list 來得小。\n" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 0, 347 | "metadata": { 348 | "colab": { 349 | "base_uri": "https://localhost:8080/", 350 | "height": 51 351 | }, 352 | "colab_type": "code", 353 | "executionInfo": { 354 | "elapsed": 583, 355 | "status": "ok", 356 | "timestamp": 1566139135464, 357 | "user": { 358 | "displayName": "電機工程系莊翔宇", 359 | "photoUrl": "", 360 | "userId": "06232090834761520364" 361 | }, 362 | "user_tz": -480 363 | }, 364 | "id": "KhIwmgshI5mT", 365 | "outputId": "c9e3cbfa-804b-41f5-d5ae-714ddf4a5ebe" 366 | }, 367 | "outputs": [ 368 | { 369 | "name": "stdout", 370 | "output_type": "stream", 371 | "text": [ 372 | "list 資料大小: 9088\n", 373 | "tuple 資料大小: 8024\n" 374 | ] 375 | } 376 | ], 377 | "source": [ 378 | "listA = list(range(1000))\n", 379 | "tupleA = tuple(range(1000))\n", 380 | "\n", 381 | "print (\"list 資料大小:\", listA.__sizeof__())\n", 382 | "print (\"tuple 資料大小:\", tupleA.__sizeof__())" 383 | ] 384 | } 385 | ], 386 | "metadata": { 387 | "colab": { 388 | "collapsed_sections": [], 389 | "name": "course6:Tuple.ipynb", 390 | "provenance": [], 391 | "version": "0.3.2" 392 | }, 393 | "kernelspec": { 394 | "display_name": "Python 3", 395 | "language": "python", 396 | "name": "python3" 397 | }, 398 | "language_info": { 399 | "codemirror_mode": { 400 | "name": "ipython", 401 | "version": 3 402 | }, 403 | "file_extension": ".py", 404 | "mimetype": "text/x-python", 405 | "name": "python", 406 | "nbconvert_exporter": "python", 407 | "pygments_lexer": "ipython3", 408 | "version": "3.7.3" 409 | } 410 | }, 411 | "nbformat": 4, 412 | "nbformat_minor": 1 413 | } 414 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class07 Range-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "S4F7QCIWGiLv" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the first course of《Python: from business analytics to Artificial Intelligence》--【Python Basics】, by AI . FREE Team.
\n", 14 | "\n", 15 | "
\n", 16 | "\n", 17 | "
歡迎大家來到 AI . FREE Team 《Pyhon 從商業分析到人工智慧》的第一堂課,Python 基礎教學!
\n", 18 | "\n", 19 | "
\n", 20 | "\n", 21 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "F8f-ETEwOhkA" 29 | }, 30 | "source": [ 31 | "# Ranges ( 範圍 )\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "wgnzZIUAOoLD" 39 | }, 40 | "source": [ 41 | "\n", 42 | "\n", 43 | "* Range 內的元素是不可變的,並且可以使用索引,通常用於迴圈(Loop)\n", 44 | "* 宣告方式為 range( start , stop, step )\n", 45 | "* start : 計數從 start 為起點。如果沒有數字就是從0開始。\n", 46 | "* stop : 計數到 stop 為終點。但不包括 stop 的數字。\n", 47 | "* step : 移動步長,沒有數字就視為 1 。\n" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 16, 53 | "metadata": { 54 | "colab": { 55 | "base_uri": "https://localhost:8080/", 56 | "height": 221 57 | }, 58 | "colab_type": "code", 59 | "executionInfo": { 60 | "elapsed": 841, 61 | "status": "ok", 62 | "timestamp": 1566141675125, 63 | "user": { 64 | "displayName": "電機工程系莊翔宇", 65 | "photoUrl": "", 66 | "userId": "06232090834761520364" 67 | }, 68 | "user_tz": -480 69 | }, 70 | "id": "-MZd6NbXNsf5", 71 | "outputId": "843e604e-9121-4e1f-8f12-dfd99b6f2c90" 72 | }, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "0,1,2,3,4,\n", 79 | "0 ~ 4,但不包括5\n", 80 | "\n", 81 | "3,4,5,6,7,8,9,\n", 82 | "從3開始,但不包括10\n", 83 | "\n", 84 | "3,5,7,9,\n", 85 | "從3開始,但不包括10,每次跳2\n", 86 | "\n", 87 | "可以遞增也可以遞減\n", 88 | "10,5,0,-5,-10,-15,-20,-25,\n", 89 | "從10開始,但不包括-30,每次跳-5\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "for i in range(5):\n", 95 | " print(i,end=\",\")\n", 96 | "print()\n", 97 | "print(\"0 ~ 4,但不包括5\")\n", 98 | "\n", 99 | "print()\n", 100 | "for i in range(3,10):\n", 101 | " print(i,end=\",\")\n", 102 | "print() \n", 103 | "print(\"從3開始,但不包括10\")\n", 104 | "\n", 105 | "print()\n", 106 | "for i in range(3,10,2):\n", 107 | " print(i,end=\",\")\n", 108 | "print() \n", 109 | "print(\"從3開始,但不包括10,每次跳2\")\n", 110 | "\n", 111 | "print()\n", 112 | "print(\"可以遞增也可以遞減\")\n", 113 | "for i in range(10,-30,-5):\n", 114 | " print(i,end=\",\")\n", 115 | "print() \n", 116 | "print(\"從10開始,但不包括-30,每次跳-5\")" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": { 122 | "colab_type": "text", 123 | "id": "xToljETJSwbX" 124 | }, 125 | "source": [ 126 | "* 來看看其他用法" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 11, 132 | "metadata": { 133 | "colab": { 134 | "base_uri": "https://localhost:8080/", 135 | "height": 119 136 | }, 137 | "colab_type": "code", 138 | "executionInfo": { 139 | "elapsed": 630, 140 | "status": "ok", 141 | "timestamp": 1566141387574, 142 | "user": { 143 | "displayName": "電機工程系莊翔宇", 144 | "photoUrl": "", 145 | "userId": "06232090834761520364" 146 | }, 147 | "user_tz": -480 148 | }, 149 | "id": "P0L0yXFNQ9Hj", 150 | "outputId": "a6307d57-76f7-49e4-9027-0aea99d564a8" 151 | }, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "[[0, 'jane']]\n", 158 | "[[0, 'jane'], [1, 'john']]\n", 159 | "[[0, 'jane'], [1, 'john'], [2, 'mark']]\n", 160 | "[[0, 'jane'], [1, 'john'], [2, 'mark'], [3, 'harry']]\n", 161 | "[[0, 'jane'], [1, 'john'], [2, 'mark'], [3, 'harry'], [4, 'mike']]\n", 162 | "[[0, 'jane'], [1, 'john'], [2, 'mark'], [3, 'harry'], [4, 'mike'], [5, 'ed']]\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "words = ['jane','john','mark','harry','mike','ed']\n", 168 | "wordlist = []\n", 169 | "for i in range(len(words)):\n", 170 | " wordlist.append([i,words[i]])\n", 171 | " print(wordlist)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": { 177 | "colab_type": "text", 178 | "id": "qc2eXly9TXk7" 179 | }, 180 | "source": [ 181 | "* 也可以把 range 宣告成變數" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 15, 187 | "metadata": { 188 | "colab": { 189 | "base_uri": "https://localhost:8080/", 190 | "height": 136 191 | }, 192 | "colab_type": "code", 193 | "executionInfo": { 194 | "elapsed": 695, 195 | "status": "ok", 196 | "timestamp": 1566141598136, 197 | "user": { 198 | "displayName": "電機工程系莊翔宇", 199 | "photoUrl": "", 200 | "userId": "06232090834761520364" 201 | }, 202 | "user_tz": -480 203 | }, 204 | "id": "ckJcY8e0TRBu", 205 | "outputId": "23af5576-7ea9-4f20-e4b0-fe648249c523" 206 | }, 207 | "outputs": [ 208 | { 209 | "name": "stdout", 210 | "output_type": "stream", 211 | "text": [ 212 | "range(0, 10, 2)\n", 213 | "[0, 2, 4, 6, 8]\n", 214 | "True\n", 215 | "1\n", 216 | "6\n", 217 | "range(0, 6, 2)\n", 218 | "8\n" 219 | ] 220 | } 221 | ], 222 | "source": [ 223 | "r = range(0,10,2)\n", 224 | "print(r)\n", 225 | "print(list(r))\n", 226 | "print(2 in r)\n", 227 | "print(r.index(2))\n", 228 | "print(r[3])\n", 229 | "print(r[:3])\n", 230 | "print(r[-1])" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 0, 236 | "metadata": { 237 | "colab": {}, 238 | "colab_type": "code", 239 | "id": "2ZLG-WJVTfOZ" 240 | }, 241 | "outputs": [], 242 | "source": [] 243 | } 244 | ], 245 | "metadata": { 246 | "colab": { 247 | "collapsed_sections": [], 248 | "name": "course7: Ranges.ipynb", 249 | "provenance": [], 250 | "version": "0.3.2" 251 | }, 252 | "kernelspec": { 253 | "display_name": "Python 3", 254 | "language": "python", 255 | "name": "python3" 256 | }, 257 | "language_info": { 258 | "codemirror_mode": { 259 | "name": "ipython", 260 | "version": 3 261 | }, 262 | "file_extension": ".py", 263 | "mimetype": "text/x-python", 264 | "name": "python", 265 | "nbconvert_exporter": "python", 266 | "pygments_lexer": "ipython3", 267 | "version": "3.7.3" 268 | } 269 | }, 270 | "nbformat": 4, 271 | "nbformat_minor": 1 272 | } 273 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class08 Set-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "eIYX18qMb_Ug" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the first course of《Python: from business analytics to Artificial Intelligence》--【Python Basics】, by AI . FREE Team.
\n", 14 | "\n", 15 | "
\n", 16 | "\n", 17 | "
歡迎大家來到 AI . FREE Team 《Pyhon 從商業分析到人工智慧》的第一堂課,Python 基礎教學!
\n", 18 | "\n", 19 | "
\n", 20 | "\n", 21 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "g_qwNxi1cBz6" 29 | }, 30 | "source": [ 31 | "# Set ( 元組 )" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "zh8NQDatdJ9t" 39 | }, 40 | "source": [ 41 | "* set() 函數創建一個無序不重複元素集,可進行關係測試,刪除重複數據,還可以計算交集、差集、並集等。" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 0, 47 | "metadata": { 48 | "colab": {}, 49 | "colab_type": "code", 50 | "id": "4pNYZD3jbj8p" 51 | }, 52 | "outputs": [], 53 | "source": [ 54 | "# class set([iterable])\n", 55 | "# iterable -- 可迭代對象對象;" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": { 61 | "colab_type": "text", 62 | "id": "cPj-rQaVg23D" 63 | }, 64 | "source": [ 65 | "## 實例" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": { 71 | "colab_type": "text", 72 | "id": "unbQorm4eufQ" 73 | }, 74 | "source": [ 75 | "* 以下實例展示了 set 的使用方法:" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 0, 81 | "metadata": { 82 | "colab": { 83 | "base_uri": "https://localhost:8080/", 84 | "height": 102 85 | }, 86 | "colab_type": "code", 87 | "executionInfo": { 88 | "elapsed": 649, 89 | "status": "ok", 90 | "timestamp": 1566145137680, 91 | "user": { 92 | "displayName": "電機工程系李佑謙", 93 | "photoUrl": "", 94 | "userId": "05735385076343474033" 95 | }, 96 | "user_tz": -480 97 | }, 98 | "id": "wtt9P1TehM6_", 99 | "outputId": "afd4b6e9-d0fe-46f0-d6b4-a68b5b84f91c" 100 | }, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 107 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 108 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 109 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 110 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "x = set('runoob')\n", 116 | "y = set('google')\n", 117 | "print(x,y)\n", 118 | "\n", 119 | "x, y\n", 120 | "(set(['b','r','u','o','n']), set(['e','o','g','l'])) # 重複的被刪除\n", 121 | "print(x,y)\n", 122 | "\n", 123 | "x & y # 交集\n", 124 | "set(['o'])\n", 125 | "print(x,y)\n", 126 | "\n", 127 | "x | y # 並集\n", 128 | "set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u'])\n", 129 | "print(x,y)\n", 130 | "\n", 131 | "x - y # 差集\n", 132 | "set(['r','b','u','n'])\n", 133 | "print(x,y)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": { 139 | "colab_type": "text", 140 | "id": "glPs_dsjhLtx" 141 | }, 142 | "source": [ 143 | "* 恭喜!完成本章節" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 0, 149 | "metadata": { 150 | "colab": {}, 151 | "colab_type": "code", 152 | "id": "WWuRos0rhvod" 153 | }, 154 | "outputs": [], 155 | "source": [] 156 | } 157 | ], 158 | "metadata": { 159 | "colab": { 160 | "collapsed_sections": [], 161 | "name": "course8:set.ipynb", 162 | "provenance": [], 163 | "version": "0.3.2" 164 | }, 165 | "kernelspec": { 166 | "display_name": "Python 3", 167 | "language": "python", 168 | "name": "python3" 169 | }, 170 | "language_info": { 171 | "codemirror_mode": { 172 | "name": "ipython", 173 | "version": 3 174 | }, 175 | "file_extension": ".py", 176 | "mimetype": "text/x-python", 177 | "name": "python", 178 | "nbconvert_exporter": "python", 179 | "pygments_lexer": "ipython3", 180 | "version": "3.7.3" 181 | } 182 | }, 183 | "nbformat": 4, 184 | "nbformat_minor": 1 185 | } 186 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class10 If-else-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "N177jyz4StvH" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the first course of《Python: from business analytics to Artificial Intelligence》--【Python Basics】, by AI . FREE Team.
\n", 14 | "\n", 15 | "
\n", 16 | "\n", 17 | "
歡迎大家來到 AI . FREE Team 《Pyhon 從商業分析到人工智慧》的第一堂課,Python 基礎教學!
\n", 18 | "\n", 19 | "
\n", 20 | "\n", 21 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "-F8EhN_TTmxn" 29 | }, 30 | "source": [ 31 | "# If else(條件語法)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "MB7lAcJETyza" 39 | }, 40 | "source": [ 41 | "Python條件語法是通過一條或多條語句的執行結果(True或者False)來決定執行的代碼。\n", 42 | "\n", 43 | "可以通過下圖來簡單瞭解條件語句的執行過程:" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": { 49 | "colab_type": "text", 50 | "id": "YJl_C-y3T7i5" 51 | }, 52 | "source": [ 53 | "![替代文字](https://www.tutorialspoint.com/python/images/if_else_statement.jpg)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": { 59 | "colab_type": "text", 60 | "id": "DFjlhBS1UGPa" 61 | }, 62 | "source": [ 63 | "Python程序語言指定任何非0和非空(null)值爲true,0 或者 null爲false。\n", 64 | "\n", 65 | "Python 編程中 if 語句用於控制程序的執行,基本形式爲:\n", 66 | "\n", 67 | "\n", 68 | "```\n", 69 | "if 判斷條件:\n", 70 | " 執行語句……\n", 71 | "else:\n", 72 | " 執行語句……\n", 73 | "```\n", 74 | "\n" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": { 80 | "colab_type": "text", 81 | "id": "qnJcDIIeVU13" 82 | }, 83 | "source": [ 84 | "其中\"判斷條件\"成立時(非零),則執行後面的語句,而執行內容可以多行,以同樣的縮排來區分表示同一範圍。\n", 85 | "\n", 86 | "else 爲可選語句,當上述條件不成立時執行內容則可以執行相關語句。" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 2, 92 | "metadata": { 93 | "colab": { 94 | "base_uri": "https://localhost:8080/", 95 | "height": 51 96 | }, 97 | "colab_type": "code", 98 | "executionInfo": { 99 | "elapsed": 656, 100 | "status": "ok", 101 | "timestamp": 1567266304265, 102 | "user": { 103 | "displayName": "電機工程系李佑謙", 104 | "photoUrl": "", 105 | "userId": "05735385076343474033" 106 | }, 107 | "user_tz": -480 108 | }, 109 | "id": "BSWF8OPpSZu-", 110 | "outputId": "771c3e01-d57e-42dc-e717-5b11210bce7b" 111 | }, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "hi!\n", 118 | "None\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "x = 1\n", 124 | "if x == 1: # 判斷變量是否爲 1\n", 125 | " print('hi!') # 並輸出信息\n", 126 | " \n", 127 | "else:\n", 128 | " print('None')\n", 129 | "\n", 130 | "x = 2\n", 131 | "if x == 1: \n", 132 | " print('hi!') \n", 133 | "else:\n", 134 | " print('None')" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": { 140 | "colab_type": "text", 141 | "id": "cl0STcCEWBsW" 142 | }, 143 | "source": [ 144 | "if 語句的判斷條件可以用>(大於)、<(小於)、==(等於)、>=(大於等於)、<=(小於等於)來表示其關係。\n", 145 | "\n", 146 | "當判斷條件爲多個值時,可以使用以下形式:\n", 147 | "\n", 148 | "\n", 149 | "```\n", 150 | "if 判斷條件1:\n", 151 | " 執行語句1……\n", 152 | "elif 判斷條件2:\n", 153 | " 執行語句2……\n", 154 | "elif 判斷條件3:\n", 155 | " 執行語句3……\n", 156 | "else:\n", 157 | " 執行語句4……\n", 158 | "```\n", 159 | "\n" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 3, 165 | "metadata": { 166 | "colab": { 167 | "base_uri": "https://localhost:8080/", 168 | "height": 34 169 | }, 170 | "colab_type": "code", 171 | "executionInfo": { 172 | "elapsed": 699, 173 | "status": "ok", 174 | "timestamp": 1567266437408, 175 | "user": { 176 | "displayName": "電機工程系李佑謙", 177 | "photoUrl": "", 178 | "userId": "05735385076343474033" 179 | }, 180 | "user_tz": -480 181 | }, 182 | "id": "4cfe0V-tWIWv", 183 | "outputId": "8a97ee29-db26-48e8-bf99-9963271e5205" 184 | }, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "twenty-one\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "number = 21 \n", 196 | "if number == 18: # 判斷number的值\n", 197 | " print('eighteen') \n", 198 | "elif number == 21:\n", 199 | " print('twenty-one') \n", 200 | "elif number == 1:\n", 201 | " print('one') \n", 202 | "else:\n", 203 | " print('not') # 條件均不成立時輸出" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": { 209 | "colab_type": "text", 210 | "id": "ERnTmg27XKUO" 211 | }, 212 | "source": [ 213 | "由於 python 並不支持 switch 語句,所以多個條件判斷,只能用 elif 來實現,如果判斷需要多個條件需同時判斷時,可以使用 or (或),表示兩個條件有一個成立時判斷條件成功;使用 and (與)時,表示只有兩個條件同時成立的情況下,判斷條件才成功。" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 4, 219 | "metadata": { 220 | "colab": { 221 | "base_uri": "https://localhost:8080/", 222 | "height": 68 223 | }, 224 | "colab_type": "code", 225 | "executionInfo": { 226 | "elapsed": 677, 227 | "status": "ok", 228 | "timestamp": 1567266766421, 229 | "user": { 230 | "displayName": "電機工程系李佑謙", 231 | "photoUrl": "", 232 | "userId": "05735385076343474033" 233 | }, 234 | "user_tz": -480 235 | }, 236 | "id": "AFWeJ8-_XJmQ", 237 | "outputId": "86022692-15e7-4da3-b4bc-a584dce34c33" 238 | }, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "between 0 and 10\n", 245 | "undefine\n", 246 | "undefine\n" 247 | ] 248 | } 249 | ], 250 | "source": [ 251 | "num = 9\n", 252 | "if num >= 0 and num <= 10: # 判斷值是否在0~10之間\n", 253 | " print('between 0 and 10')\n", 254 | "# 輸出結果:between 0 and 10\n", 255 | " \n", 256 | "num = 10\n", 257 | "if num < 0 or num > 10: # 判斷值是否在小於0或大於10\n", 258 | " print('less than 0 or more than 10')\n", 259 | "else:\n", 260 | " print('undefine')\n", 261 | "# 輸出結果: undefine\n", 262 | " \n", 263 | "num = 8\n", 264 | "# 判斷值是否在0~5或者10~15之間\n", 265 | "if (num >= 0 and num <= 5) or (num >= 10 and num <= 15): \n", 266 | " print('(more or equal 0 and less or equal than 5) or (more or equal 10 and less or equal than 15)')\n", 267 | "else:\n", 268 | " print('undefine')\n", 269 | "# 輸出結果: undefine" 270 | ] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "metadata": { 275 | "colab_type": "text", 276 | "id": "7VtH-IM0YQgP" 277 | }, 278 | "source": [ 279 | "當if有多個條件時可使用括號來區分判斷的先後順序,括號中的判斷優先執行,此外 and 和 or 的優先級低於>(大於)、<(小於)等判斷符號,即大於和小於在沒有括號的情況下會比與或要優先判斷。" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": { 285 | "colab_type": "text", 286 | "id": "ju1S-GY7YTwD" 287 | }, 288 | "source": [ 289 | "## 簡單的語句組" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": { 295 | "colab_type": "text", 296 | "id": "1Yti37DdYXg8" 297 | }, 298 | "source": [ 299 | "你也可以在同一行的位置上使用if條件判斷語句,如下實例:" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 6, 305 | "metadata": { 306 | "colab": { 307 | "base_uri": "https://localhost:8080/", 308 | "height": 51 309 | }, 310 | "colab_type": "code", 311 | "executionInfo": { 312 | "elapsed": 629, 313 | "status": "ok", 314 | "timestamp": 1567266894695, 315 | "user": { 316 | "displayName": "電機工程系李佑謙", 317 | "photoUrl": "", 318 | "userId": "05735385076343474033" 319 | }, 320 | "user_tz": -480 321 | }, 322 | "id": "AaOsVZ9NYkTl", 323 | "outputId": "30f5e1a9-3dc9-42ce-cef2-b5c499520c31" 324 | }, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "變量 var 的值爲50\n", 331 | "Good bye!\n" 332 | ] 333 | } 334 | ], 335 | "source": [ 336 | "var = 50 \n", 337 | " \n", 338 | "if ( var == 50 ) : print(\"變量 var 的值爲50\") \n", 339 | " \n", 340 | "print(\"Good bye!\")" 341 | ] 342 | } 343 | ], 344 | "metadata": { 345 | "colab": { 346 | "collapsed_sections": [], 347 | "name": "If_else.ipynb", 348 | "provenance": [], 349 | "version": "0.3.2" 350 | }, 351 | "kernelspec": { 352 | "display_name": "Python 3", 353 | "language": "python", 354 | "name": "python3" 355 | }, 356 | "language_info": { 357 | "codemirror_mode": { 358 | "name": "ipython", 359 | "version": 3 360 | }, 361 | "file_extension": ".py", 362 | "mimetype": "text/x-python", 363 | "name": "python", 364 | "nbconvert_exporter": "python", 365 | "pygments_lexer": "ipython3", 366 | "version": "3.7.3" 367 | } 368 | }, 369 | "nbformat": 4, 370 | "nbformat_minor": 1 371 | } 372 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class12 For-loop-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "rGCLGcuEPVSa" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the first course of《Python: from business analytics to Artificial Intelligence》--【Python Basics】, by AI . FREE Team.
\n", 14 | "\n", 15 | "
\n", 16 | "\n", 17 | "
歡迎大家來到 AI . FREE Team 《Pyhon 從商業分析到人工智慧》的第一堂課,Python 基礎教學!
\n", 18 | "\n", 19 | "
\n", 20 | "\n", 21 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "EmvrmGKYPZz3" 29 | }, 30 | "source": [ 31 | "# For迴圈" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "jKOZ696hFiiu" 39 | }, 40 | "source": [ 41 | "* 就像while迴圈一樣 for循環可以遍歷任何序列的項目,如一個列表或者一個字符串。" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": { 47 | "colab_type": "text", 48 | "id": "FHG_5fQXH1RZ" 49 | }, 50 | "source": [ 51 | "* for循環的語法格式如下:" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 0, 57 | "metadata": { 58 | "colab": {}, 59 | "colab_type": "code", 60 | "id": "QZPQ2d5UPR-F" 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "#for iterating_var in sequence:\n", 65 | "# statements(s)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": { 71 | "colab_type": "text", 72 | "id": "E6dHdcF1IAv7" 73 | }, 74 | "source": [ 75 | "![替代文字](http://www.runoob.com/wp-content/uploads/2013/11/python_for_loop.jpg)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 0, 81 | "metadata": { 82 | "colab": { 83 | "base_uri": "https://localhost:8080/", 84 | "height": 221 85 | }, 86 | "colab_type": "code", 87 | "executionInfo": { 88 | "elapsed": 893, 89 | "status": "ok", 90 | "timestamp": 1566658988248, 91 | "user": { 92 | "displayName": "電機工程系李佑謙", 93 | "photoUrl": "", 94 | "userId": "05735385076343474033" 95 | }, 96 | "user_tz": -480 97 | }, 98 | "id": "C5da9vhyIbB4", 99 | "outputId": "73652b42-db7e-4861-f205-4be6209dbe60" 100 | }, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "當前字母 : N\n", 107 | "當前字母 : e\n", 108 | "當前字母 : w\n", 109 | "當前字母 : \n", 110 | "當前字母 : Y\n", 111 | "當前字母 : o\n", 112 | "當前字母 : r\n", 113 | "當前字母 : k\n", 114 | "當前水果 : Detroit\n", 115 | "當前水果 : Arizona\n", 116 | "當前水果 : Georgia\n", 117 | "finish\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "for ex in 'New York': # 第一個實例\n", 123 | " print('當前字母 :',ex)\n", 124 | " \n", 125 | "fruits = ['Detroit','Arizona','Georgia']\n", 126 | "for fruit in fruits: # 第二個實例\n", 127 | " print('當前地名 :',fruit)\n", 128 | " \n", 129 | "print(\"finish\")" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": { 135 | "colab_type": "text", 136 | "id": "9JigJ0O_J6n_" 137 | }, 138 | "source": [ 139 | "## 通過序列索引迭代" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": { 145 | "colab_type": "text", 146 | "id": "b2Zg4xH_KAQd" 147 | }, 148 | "source": [ 149 | "* 另外一種執行循環的遍歷方式是通過索引,如下實例:" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 0, 155 | "metadata": { 156 | "colab": { 157 | "base_uri": "https://localhost:8080/", 158 | "height": 85 159 | }, 160 | "colab_type": "code", 161 | "executionInfo": { 162 | "elapsed": 678, 163 | "status": "ok", 164 | "timestamp": 1566659431765, 165 | "user": { 166 | "displayName": "電機工程系李佑謙", 167 | "photoUrl": "", 168 | "userId": "05735385076343474033" 169 | }, 170 | "user_tz": -480 171 | }, 172 | "id": "me4EMzYkIgjh", 173 | "outputId": "d061ca19-60cd-4b88-ca93-a17b1e9143e5" 174 | }, 175 | "outputs": [ 176 | { 177 | "name": "stdout", 178 | "output_type": "stream", 179 | "text": [ 180 | "當前國家 : New Jersey\n", 181 | "當前國家 : Nevada\n", 182 | "當前國家 : Montana\n", 183 | "finish!\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "state = ['New Jersey', 'Nevada', 'Montana']\n", 189 | "for index in range(len(state)):\n", 190 | " print('當前國家 :', state[index])\n", 191 | " \n", 192 | "print(\"finish!\")" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": { 198 | "colab_type": "text", 199 | "id": "skzu2KQtIFof" 200 | }, 201 | "source": [ 202 | "* 以上實例我們使用了內置函數 len() 和 range(),函數 len() 返回列表的長度,即元素的個數。 range返回一個序列的數。" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": { 208 | "colab_type": "text", 209 | "id": "uiOgQauwLnXY" 210 | }, 211 | "source": [ 212 | "## 循環使用 else 語句" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": { 218 | "colab_type": "text", 219 | "id": "AYXO0UP1Lsdy" 220 | }, 221 | "source": [ 222 | "* 在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是通過 break 跳出而中斷的)的情況下執行,while … else 也是一樣。" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 0, 228 | "metadata": { 229 | "colab": { 230 | "base_uri": "https://localhost:8080/", 231 | "height": 357 232 | }, 233 | "colab_type": "code", 234 | "executionInfo": { 235 | "elapsed": 674, 236 | "status": "ok", 237 | "timestamp": 1566659661038, 238 | "user": { 239 | "displayName": "電機工程系李佑謙", 240 | "photoUrl": "", 241 | "userId": "05735385076343474033" 242 | }, 243 | "user_tz": -480 244 | }, 245 | "id": "hu7ybwNSL130", 246 | "outputId": "53336914-d848-4923-cdac-adf5b3e48fe3" 247 | }, 248 | "outputs": [ 249 | { 250 | "name": "stdout", 251 | "output_type": "stream", 252 | "text": [ 253 | "0 是一個質數\n", 254 | "1 是一個質數\n", 255 | "2 是一個質數\n", 256 | "3 是一個質數\n", 257 | "4 等於 2 * 2\n", 258 | "5 是一個質數\n", 259 | "6 等於 2 * 3\n", 260 | "7 是一個質數\n", 261 | "8 等於 2 * 4\n", 262 | "9 等於 3 * 3\n", 263 | "10 等於 2 * 5\n", 264 | "11 是一個質數\n", 265 | "12 等於 2 * 6\n", 266 | "13 是一個質數\n", 267 | "14 等於 2 * 7\n", 268 | "15 等於 3 * 5\n", 269 | "16 等於 2 * 8\n", 270 | "17 是一個質數\n", 271 | "18 等於 2 * 9\n", 272 | "19 是一個質數\n" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "for num in range(0,20): # 迭代 10 到 20 之間的數字\n", 278 | " for i in range(2,num): # 根據因子迭代\n", 279 | " if num%i == 0: # 確定第一個因子\n", 280 | " j=num/i # 計算第二個因子\n", 281 | " print('%d 等於 %d * %d' % (num,i,j))\n", 282 | " break # 跳出當前循環\n", 283 | " else: # 循環的 else 部分\n", 284 | " print(num, '是一個質數')" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": { 290 | "colab_type": "text", 291 | "id": "nqDzLTZ4MVln" 292 | }, 293 | "source": [ 294 | "* 恭喜完成本章節!" 295 | ] 296 | } 297 | ], 298 | "metadata": { 299 | "colab": { 300 | "collapsed_sections": [], 301 | "name": "course12:For_Loop.ipynb", 302 | "provenance": [], 303 | "version": "0.3.2" 304 | }, 305 | "kernelspec": { 306 | "display_name": "Python 3", 307 | "language": "python", 308 | "name": "python3" 309 | }, 310 | "language_info": { 311 | "codemirror_mode": { 312 | "name": "ipython", 313 | "version": 3 314 | }, 315 | "file_extension": ".py", 316 | "mimetype": "text/x-python", 317 | "name": "python", 318 | "nbconvert_exporter": "python", 319 | "pygments_lexer": "ipython3", 320 | "version": "3.7.3" 321 | } 322 | }, 323 | "nbformat": 4, 324 | "nbformat_minor": 1 325 | } 326 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Class15 Class-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "vAHJ2WG5QN1I" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the first course of《Python: from business analytics to Artificial Intelligence》--【Python Basics】, by AI . FREE Team.
\n", 14 | "\n", 15 | "
\n", 16 | "\n", 17 | "
歡迎大家來到 AI . FREE Team 《Pyhon 從商業分析到人工智慧》的第一堂課,Python 基礎教學!
\n", 18 | "\n", 19 | "
\n", 20 | "\n", 21 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "4VkEHs-aRGbW" 29 | }, 30 | "source": [ 31 | "# Class(類別)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "Csvd5juOEG91" 39 | }, 40 | "source": [ 41 | "Python 是物件導向的程式語言 ( Object-oriented programming ),在python中幾乎所有東西都是物件,具有其屬性跟方法。\n", 42 | "\n", 43 | "而類別就像是建構函數,由開發者去創造一個物件的\"藍圖\"。" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": { 49 | "colab_type": "text", 50 | "id": "mrJqg1-mV2WM" 51 | }, 52 | "source": [ 53 | "## 建立基本屬性" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 0, 59 | "metadata": { 60 | "colab": { 61 | "base_uri": "https://localhost:8080/", 62 | "height": 34 63 | }, 64 | "colab_type": "code", 65 | "executionInfo": { 66 | "elapsed": 758, 67 | "status": "ok", 68 | "timestamp": 1567135560014, 69 | "user": { 70 | "displayName": "電機工程系莊翔宇", 71 | "photoUrl": "", 72 | "userId": "06232090834761520364" 73 | }, 74 | "user_tz": -480 75 | }, 76 | "id": "t4uVmD67W--c", 77 | "outputId": "b31a34e4-9191-4519-9e9a-274328c5d712" 78 | }, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "apple\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "class fruit():\n", 90 | " def __init__(self, kind):\n", 91 | " self.kind = kind\n", 92 | " \n", 93 | "a = fruit(\"apple\") #建立一個名叫dog的Animal實體(物件)\n", 94 | "print(a.kind)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": { 100 | "colab_type": "text", 101 | "id": "QU3rdrMLWH4-" 102 | }, 103 | "source": [ 104 | "建構一個類別 fruit\n", 105 | "\n", 106 | "代表之後的大家都可以取用這個class的屬性\n", 107 | "\n", 108 | "\n", 109 | "\n", 110 | "`def __init__(self):`\n", 111 | "\n", 112 | "* 這邊代表宣告時會自動執行的函式\n", 113 | "\n", 114 | "**也就是宣告類別的 \"起手式 \"**\n", 115 | "\n", 116 | "所以一般會拿來放基礎的屬性設定\n", 117 | "\n", 118 | "如上面範例,添加了一個 kind 屬性" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 0, 124 | "metadata": { 125 | "colab": { 126 | "base_uri": "https://localhost:8080/", 127 | "height": 68 128 | }, 129 | "colab_type": "code", 130 | "executionInfo": { 131 | "elapsed": 746, 132 | "status": "ok", 133 | "timestamp": 1567135560015, 134 | "user": { 135 | "displayName": "電機工程系莊翔宇", 136 | "photoUrl": "", 137 | "userId": "06232090834761520364" 138 | }, 139 | "user_tz": -480 140 | }, 141 | "id": "PSLTbrNeYHAT", 142 | "outputId": "6728b541-b80c-472a-9355-73e2dd1a0441" 143 | }, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "Amy\n", 150 | "17\n", 151 | "18\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "# 建構一個 family 類別\n", 157 | "class family():\n", 158 | " # 函式 __init__ 會自動執行\n", 159 | " def __init__(self,name,age):\n", 160 | " # self是class本身,所以第一個不用更動,而這邊新增兩個屬性,一個是名字,另一個是年齡\n", 161 | " self.name = name\n", 162 | " self.age = age\n", 163 | " \n", 164 | "Amy = family('Amy',17)\n", 165 | "print(Amy.name)\n", 166 | "print(Amy.age)\n", 167 | "\n", 168 | "# 可以透過這樣修改實體的屬性\n", 169 | "Amy.age = 18\n", 170 | "print(Amy.age)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": { 176 | "colab_type": "text", 177 | "id": "FgIBoDl3Y_KK" 178 | }, 179 | "source": [ 180 | "## 取用自己的方法\n" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 0, 186 | "metadata": { 187 | "colab": { 188 | "base_uri": "https://localhost:8080/", 189 | "height": 34 190 | }, 191 | "colab_type": "code", 192 | "executionInfo": { 193 | "elapsed": 729, 194 | "status": "ok", 195 | "timestamp": 1567135560016, 196 | "user": { 197 | "displayName": "電機工程系莊翔宇", 198 | "photoUrl": "", 199 | "userId": "06232090834761520364" 200 | }, 201 | "user_tz": -480 202 | }, 203 | "id": "W_lidOmsNm9D", 204 | "outputId": "6f307f24-ab1c-4b70-c386-c7d40324e6ec" 205 | }, 206 | "outputs": [ 207 | { 208 | "data": { 209 | "text/plain": [ 210 | "'ken'" 211 | ] 212 | }, 213 | "execution_count": 3, 214 | "metadata": { 215 | "tags": [] 216 | }, 217 | "output_type": "execute_result" 218 | } 219 | ], 220 | "source": [ 221 | "class family():\n", 222 | " def __init__(self,name,age):\n", 223 | " self.name = name\n", 224 | " self.age = age\n", 225 | " def Name(self):\n", 226 | " return self.name\n", 227 | " \n", 228 | "Ken = family('ken',22)\n", 229 | "Ken.Name()" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 0, 235 | "metadata": { 236 | "colab": { 237 | "base_uri": "https://localhost:8080/", 238 | "height": 68 239 | }, 240 | "colab_type": "code", 241 | "executionInfo": { 242 | "elapsed": 717, 243 | "status": "ok", 244 | "timestamp": 1567135560017, 245 | "user": { 246 | "displayName": "電機工程系莊翔宇", 247 | "photoUrl": "", 248 | "userId": "06232090834761520364" 249 | }, 250 | "user_tz": -480 251 | }, 252 | "id": "RXP7gGoOTclx", 253 | "outputId": "4bc19ede-056e-47d8-98cb-7cfbacce3bf8" 254 | }, 255 | "outputs": [ 256 | { 257 | "name": "stdout", 258 | "output_type": "stream", 259 | "text": [ 260 | "0\n", 261 | "0\n", 262 | "24.691358024691358\n" 263 | ] 264 | } 265 | ], 266 | "source": [ 267 | "class Human:\n", 268 | " def __init__(self,h=0,w=0):\n", 269 | " # 可在參數後面帶值\n", 270 | " self.height=h\n", 271 | " self.weight=w\n", 272 | " def BMI(self):\n", 273 | " return self.weight / ((self.height/100)**2)\n", 274 | "\n", 275 | "\n", 276 | "steve = Human() \n", 277 | "# 這樣即使在宣告的時候沒有帶參數也可以建立一個實體\n", 278 | "print(steve.height)\n", 279 | "print(steve.height)\n", 280 | " \n", 281 | "luffy = Human(180,80)\n", 282 | "print(luffy.BMI())" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": { 288 | "colab_type": "text", 289 | "id": "Fumeo85ra7xe" 290 | }, 291 | "source": [ 292 | "## 繼承\n", 293 | "在創造類別的時候可以\" 繼承 \" 父類別,用法是在建構類別的時候透過 super().__ init__() 帶入父類別。\n", 294 | "\n", 295 | "```\n", 296 | "class Woman(Human):\n", 297 | " def __init__(self,h,w,bust=0,waist=0,hip=0):\n", 298 | " super().__init__(h,w)\n", 299 | "```\n", 300 | "\n", 301 | "\n", 302 | "這種關係就像是父與子,\" Human \"有的屬性及方法會被\" Woman \"繼承。" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 0, 308 | "metadata": { 309 | "colab": { 310 | "base_uri": "https://localhost:8080/", 311 | "height": 51 312 | }, 313 | "colab_type": "code", 314 | "executionInfo": { 315 | "elapsed": 704, 316 | "status": "ok", 317 | "timestamp": 1567135560019, 318 | "user": { 319 | "displayName": "電機工程系莊翔宇", 320 | "photoUrl": "", 321 | "userId": "06232090834761520364" 322 | }, 323 | "user_tz": -480 324 | }, 325 | "id": "ISFnk5xvUtdx", 326 | "outputId": "b8a8752c-5aa0-42c3-cead-5ff4e6a3419f" 327 | }, 328 | "outputs": [ 329 | { 330 | "name": "stdout", 331 | "output_type": "stream", 332 | "text": [ 333 | "19.834710743801654\n", 334 | "bust= 83,waist= 64,hip= 84\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "class Woman(Human):\n", 340 | " def __init__(self,h,w,bust=0,waist=0,hip=0):\n", 341 | " super().__init__(h,w)\n", 342 | " self.bust=bust\n", 343 | " self.waist=waist\n", 344 | " self.hip=hip\n", 345 | " def printBWH(self):\n", 346 | " print(\"bust= {},waist= {},hip= {}\".format(self.bust,self.waist,self.hip))\n", 347 | " \n", 348 | "nana = Woman(165,54,83,64,84)\n", 349 | "# 父類別 的方法 BMI()\n", 350 | "print(nana.BMI())\n", 351 | "# 子類別 的方法 printBWH()\n", 352 | "nana.printBWH()" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 0, 358 | "metadata": { 359 | "cellView": "both", 360 | "colab": {}, 361 | "colab_type": "code", 362 | "id": "KIEJD5CgWMH_" 363 | }, 364 | "outputs": [], 365 | "source": [ 366 | "#@title\n" 367 | ] 368 | } 369 | ], 370 | "metadata": { 371 | "colab": { 372 | "collapsed_sections": [], 373 | "name": "course15:Class.ipynb", 374 | "provenance": [], 375 | "version": "0.3.2" 376 | }, 377 | "kernelspec": { 378 | "display_name": "Python 3", 379 | "language": "python", 380 | "name": "python3" 381 | }, 382 | "language_info": { 383 | "codemirror_mode": { 384 | "name": "ipython", 385 | "version": 3 386 | }, 387 | "file_extension": ".py", 388 | "mimetype": "text/x-python", 389 | "name": "python", 390 | "nbconvert_exporter": "python", 391 | "pygments_lexer": "ipython3", 392 | "version": "3.7.3" 393 | } 394 | }, 395 | "nbformat": 4, 396 | "nbformat_minor": 1 397 | } 398 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Lesson00 Preface-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "Rib_rBYNuDI3" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 13 | "
\n", 14 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 0 前言 (1-0)。
\n", 15 | "
\n", 16 | "\n", 17 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n", 18 | "\n", 19 | "\n", 20 | "\n", 21 | "\n", 22 | "\n" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": { 28 | "colab_type": "text", 29 | "id": "0LEJHkfYJTNU" 30 | }, 31 | "source": [ 32 | "# Preface 前言" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": { 38 | "colab_type": "text", 39 | "id": "lo-XP-5gAYLW" 40 | }, 41 | "source": [ 42 | "## 1. Why Python? 為何選擇 Python?" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": { 48 | "colab_type": "text", 49 | "id": "yf6Hp8n9GXAg" 50 | }, 51 | "source": [ 52 | "在眾多的程式語言當中,我們選擇 Python 做為初學者入門數據分析、人工智慧的語言,而不是 R, C, Matlab or Java 等其他語言,相對這些語言來說,Python 有以下優勢:\n", 53 | "\n", 54 | "\n", 55 | "1. 學習快速 (Python 屬於高階語言,較能使開發者快速讀懂程式碼)\n", 56 | "2. 資源豐富 (Python 的開源開發社群廣大,許多國際企業也都公開自家的開源套件;例如:Google, Facebook)\n", 57 | "3. 用途廣泛 (Python 不僅能作為商業分析、開發 AI 的工具、應用在科研領域,還能做為開發網頁的框架,如Django...)\n", 58 | "\n" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": { 64 | "colab_type": "text", 65 | "id": "D0XaUjY4JiZ9" 66 | }, 67 | "source": [ 68 | "## 2. Why Colab? 為何選擇 Colab 介面進行學習?" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": { 74 | "colab_type": "text", 75 | "id": "Bp2ZDcoyJtnu" 76 | }, 77 | "source": [ 78 | "Colab 為 Google 推出的免費雲端 Python 程式語言開發介面 (基於Jupyter notebook environment 開發),優點如下:\n", 79 | "\n", 80 | "\n", 81 | "1. 不用花費大量時間設定「開發環境」\n", 82 | "2. 提供許多整合性服務 (與Google Drive連結、Github開發社群、相關 Python 開源套件導入...)\n", 83 | "3. 提供免費雲端運算資源 (GPU, TPU 能使開發 AI 運算速度更快)\n", 84 | "4. 高度便利性 (可隨時隨地學習 -- 在有網路 & 電腦的狀況下)\n", 85 | "\n", 86 | "提供給讀者們: Google 官方 Colab 介紹文件 (https://colab.research.google.com/notebooks/welcome.ipynb)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": { 92 | "colab_type": "text", 93 | "id": "QA90qYogNbjg" 94 | }, 95 | "source": [ 96 | "# 第一堂課 Python Basics 基礎教學" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": { 102 | "colab_type": "text", 103 | "id": "sZPa1lEfnDcw" 104 | }, 105 | "source": [ 106 | "## 範例教學 - Print\n" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": { 112 | "colab_type": "text", 113 | "id": "xl_obAOHOAQz" 114 | }, 115 | "source": [ 116 | "一如程式語言學習的傳統,我們先從 Print 開始;Print 就如字面上意思,將東西列印出來。 (請以 Ctrl + Enter 執行下方程式語言列)\n", 117 | "\n", 118 | "> Print(\"Hello World!\")\n", 119 | "\n", 120 | "\n" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 1, 126 | "metadata": { 127 | "colab": {}, 128 | "colab_type": "code", 129 | "id": "jOBwRHanNk7Q" 130 | }, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "Hello World!\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "print(\"Hello World!\")" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": { 147 | "colab_type": "text", 148 | "id": "XF4mEjBHOtQs" 149 | }, 150 | "source": [ 151 | "執行成功後,你將看到:\n", 152 | "\n", 153 | "```\n", 154 | "Hello World!\n", 155 | "```\n", 156 | " * \"print()\" 的使用為程式語言開發的基礎工具,還有許多不同的使用方式,請多嘗試看看不同使用方法。\n", 157 | "\n", 158 | "\n" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "## 課程文件\n", 166 | "\n", 167 | "#### Python Basics 課程清單\n", 168 | "- Lesson00 Preface 課程前言 (We are here now! --本篇課程--)\n", 169 | "\n", 170 | "- Lesson01 Variable 變數 (Next Chapter! --下一堂課程--)\n", 171 | "\n", 172 | "- Lesson02 Operator 運算子\n", 173 | "\n", 174 | "- Lesson03 Data Structure 資料結構\n", 175 | "\n", 176 | "- Lesson04 String 字串\n", 177 | "\n", 178 | "- Lesson05 List 列表\n", 179 | "\n", 180 | "- Lesson06 Tuple 元組\n", 181 | "\n", 182 | "- Lesson07 Range 範圍\n", 183 | "\n", 184 | "- Lesson08 Set 集合\n", 185 | "\n", 186 | "- Lesson09 Dictionary 字典\n", 187 | "\n", 188 | "- Lesson10 If-else 條件語法\n", 189 | "\n", 190 | "- Lesson11 While 迴圈\n", 191 | "\n", 192 | "- Lesson12 For 迴圈\n", 193 | "\n", 194 | "- Lesson13 Function 函式\n", 195 | "\n", 196 | "- Lesson14 Input/Output 輸入/輸出\n", 197 | "\n", 198 | "- Lesson15 Class 類別" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [] 207 | } 208 | ], 209 | "metadata": { 210 | "accelerator": "GPU", 211 | "colab": { 212 | "collapsed_sections": [ 213 | "lo-XP-5gAYLW", 214 | "D0XaUjY4JiZ9", 215 | "sZPa1lEfnDcw", 216 | "z8a-OLPrnKUQ" 217 | ], 218 | "name": "AI . FREE Team - Python 從商業分析到人工智慧_第一堂課.ipynb", 219 | "provenance": [], 220 | "toc_visible": true, 221 | "version": "0.3.2" 222 | }, 223 | "kernelspec": { 224 | "display_name": "Python 3", 225 | "language": "python", 226 | "name": "python3" 227 | }, 228 | "language_info": { 229 | "codemirror_mode": { 230 | "name": "ipython", 231 | "version": 3 232 | }, 233 | "file_extension": ".py", 234 | "mimetype": "text/x-python", 235 | "name": "python", 236 | "nbconvert_exporter": "python", 237 | "pygments_lexer": "ipython3", 238 | "version": "3.7.3" 239 | } 240 | }, 241 | "nbformat": 4, 242 | "nbformat_minor": 1 243 | } 244 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Lesson01 Variable-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "Rib_rBYNuDI3" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 01 變數 (1-01)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n", 19 | "\n", 20 | "\n", 21 | "\n", 22 | "\n", 23 | "\n" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "colab_type": "text", 30 | "id": "z8a-OLPrnKUQ" 31 | }, 32 | "source": [ 33 | "## Variables 變數認識" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": { 39 | "colab_type": "text", 40 | "id": "JN8bNrZYoBZU" 41 | }, 42 | "source": [ 43 | "變數是什麼呢?相信大家並不陌生,這邊舉 BMI 計算為例,讓大家初步認識變數在程式語言開發中的使用。" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": { 49 | "colab_type": "text", 50 | "id": "ERLX_MhjoMoj" 51 | }, 52 | "source": [ 53 | "\n", 54 | "\n", 55 | "* **訂定第一個變數 height(高度) 為 1.8 (m)**\n", 56 | "* **訂定第二個變數 weight(體重) 為 80 (kg**) \n", 57 | "* 套入計算 BMI 的公式: **體重(kg) / 身高的平方(m * m)** \n", 58 | "* 查看計算結果 (以print來進行查看)\n", 59 | "\n", 60 | "※ **(請按 Ctrl + enter 執行下列)**\n" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 3, 66 | "metadata": { 67 | "colab": {}, 68 | "colab_type": "code", 69 | "id": "zK8aYkk2Omty" 70 | }, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "您的身高為: 1.8m (公尺)\n", 77 | "您的體重為: 80kg (公斤)\n", 78 | "您的BMI為: 24.691358024691358\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "# 訂定第一個變數 height(高度)\n", 84 | "height = 1.8\n", 85 | "\n", 86 | "# 訂定第二個變數 weight(體重)\n", 87 | "weight = 80\n", 88 | "\n", 89 | "# 套入計算 BMI 的公式: 體重(kg) / 身高的平方(m * m)\n", 90 | "BMI = weight / (height*height)\n", 91 | "\n", 92 | "#將計算結果以 Print 呈現出來\n", 93 | "print(\"您的身高為: \" + str(height) + \"m (公尺)\")\n", 94 | "print(\"您的體重為: \" + str(weight) + \"kg (公斤)\")\n", 95 | "print(\"您的BMI為: \" + str(BMI))" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": { 101 | "colab_type": "text", 102 | "id": "LKacVqLosLLx" 103 | }, 104 | "source": [ 105 | "* 以上簡單製作出 BMI 的計算工具,請嘗試看看修改身高 or 體重,來體會如何使用變數!" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": { 111 | "colab_type": "text", 112 | "id": "1_praOaBX-i7" 113 | }, 114 | "source": [ 115 | "* 變數可以是任何型態,且大小寫有區分。" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 4, 121 | "metadata": { 122 | "colab": { 123 | "base_uri": "https://localhost:8080/", 124 | "height": 51 125 | }, 126 | "colab_type": "code", 127 | "executionInfo": { 128 | "elapsed": 1151, 129 | "status": "ok", 130 | "timestamp": 1565280883892, 131 | "user": { 132 | "displayName": "電機工程系莊翔宇", 133 | "photoUrl": "", 134 | "userId": "06232090834761520364" 135 | }, 136 | "user_tz": -480 137 | }, 138 | "id": "rF-pX6KbIMRL", 139 | "outputId": "3c196431-51d0-474a-8f79-7bca1c3c52ce" 140 | }, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "Hello World\n", 147 | "a A\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "str1='Hello'\n", 153 | "str2='World'\n", 154 | "i=100\n", 155 | "a='a'\n", 156 | "A='A'\n", 157 | "\n", 158 | "print(str1+' '+str2)\n", 159 | "print(a,A)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": { 165 | "colab_type": "text", 166 | "id": "ybAqJR3JKXQR" 167 | }, 168 | "source": [ 169 | "* 變數開頭不能為數字,不能使用保留字,ex:if、for、True、in、return..。" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 0, 175 | "metadata": { 176 | "colab": { 177 | "base_uri": "https://localhost:8080/", 178 | "height": 131 179 | }, 180 | "colab_type": "code", 181 | "executionInfo": { 182 | "elapsed": 1209, 183 | "status": "error", 184 | "timestamp": 1565280887074, 185 | "user": { 186 | "displayName": "電機工程系莊翔宇", 187 | "photoUrl": "", 188 | "userId": "06232090834761520364" 189 | }, 190 | "user_tz": -480 191 | }, 192 | "id": "tuAC8WFtKkth", 193 | "outputId": "1cc9b45a-5388-4bb9-b55f-8e53748c41a3" 194 | }, 195 | "outputs": [ 196 | { 197 | "ename": "SyntaxError", 198 | "evalue": "ignored", 199 | "output_type": "error", 200 | "traceback": [ 201 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m if=2\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "5=5\n", 207 | "if=2\n", 208 | "for=3" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": { 214 | "colab_type": "text", 215 | "id": "TtEknGI82pkG" 216 | }, 217 | "source": [ 218 | "* 以上這些為python內建的變數,所以當你是用這些當做變數時,將不會具有變數的屬性(賦值、運算等等的...)" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": { 224 | "colab_type": "text", 225 | "id": "4n-jQS9-3MDx" 226 | }, 227 | "source": [ 228 | "* 而這些內建的變數,將在後面的章節逐一介紹" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "## 課程文件\n", 236 | "\n", 237 | "#### Python Basics 課程清單\n", 238 | "- Lesson00 Preface 課程前言 (Last Chapter! --上一堂課程--)\n", 239 | "\n", 240 | "- Lesson01 Variable 變數 (We are here now! --本篇課程--)\n", 241 | "\n", 242 | "- Lesson02 Operator 運算子 (Next Chapter! --下一堂課程--)\n", 243 | "\n", 244 | "- Lesson03 Data Structure 資料結構\n", 245 | "\n", 246 | "- Lesson04 String 字串\n", 247 | "\n", 248 | "- Lesson05 List 列表\n", 249 | "\n", 250 | "- Lesson06 Tuple 元組\n", 251 | "\n", 252 | "- Lesson07 Range 範圍\n", 253 | "\n", 254 | "- Lesson08 Set 集合\n", 255 | "\n", 256 | "- Lesson09 Dictionary 字典\n", 257 | "\n", 258 | "- Lesson10 If-else 條件語法\n", 259 | "\n", 260 | "- Lesson11 While 迴圈\n", 261 | "\n", 262 | "- Lesson12 For 迴圈\n", 263 | "\n", 264 | "- Lesson13 Function 函式\n", 265 | "\n", 266 | "- Lesson14 Input/Output 輸入/輸出\n", 267 | "\n", 268 | "- Lesson15 Class 類別" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [] 277 | } 278 | ], 279 | "metadata": { 280 | "accelerator": "GPU", 281 | "colab": { 282 | "collapsed_sections": [], 283 | "name": "course1:Variables.ipynb", 284 | "provenance": [], 285 | "toc_visible": true, 286 | "version": "0.3.2" 287 | }, 288 | "kernelspec": { 289 | "display_name": "Python 3", 290 | "language": "python", 291 | "name": "python3" 292 | }, 293 | "language_info": { 294 | "codemirror_mode": { 295 | "name": "ipython", 296 | "version": 3 297 | }, 298 | "file_extension": ".py", 299 | "mimetype": "text/x-python", 300 | "name": "python", 301 | "nbconvert_exporter": "python", 302 | "pygments_lexer": "ipython3", 303 | "version": "3.7.3" 304 | } 305 | }, 306 | "nbformat": 4, 307 | "nbformat_minor": 1 308 | } 309 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Lesson02 Operator-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "Bi-PSCAZKjdk" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 02 運算子 (1-02)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "_DeL-QEpKohy" 26 | }, 27 | "source": [ 28 | "# 運算子(Operator)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "RbtpSepcLFaX" 36 | }, 37 | "source": [ 38 | "* 這個章節,我們將教導什麼事運算子,並用一些實例來引導大家了解這個單元" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": { 44 | "colab_type": "text", 45 | "id": "8E9DzMEPKodT" 46 | }, 47 | "source": [ 48 | "* 基本運算" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 1, 54 | "metadata": { 55 | "colab": {}, 56 | "colab_type": "code", 57 | "id": "pPUp6RjVKU_C" 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "a = 1\n", 62 | "b = 2\n", 63 | "c = a + b\n", 64 | "d = a - b\n", 65 | "e = a * b" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": { 71 | "colab_type": "text", 72 | "id": "BTErLy4qLcBC" 73 | }, 74 | "source": [ 75 | "* 並透過print()來觀察以上計算結果" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 4, 81 | "metadata": { 82 | "colab": {}, 83 | "colab_type": "code", 84 | "id": "G68ma_l4LbWV" 85 | }, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "a = 1\n", 92 | "b = 2\n", 93 | "c = 3\n", 94 | "d = -1\n", 95 | "e = 2\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "print(\"a =\",a)\n", 101 | "print(\"b =\",b)\n", 102 | "print(\"c =\",c)\n", 103 | "print(\"d =\",d)\n", 104 | "print(\"e =\",e)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": { 110 | "colab_type": "text", 111 | "id": "5cTk0UziKzjd" 112 | }, 113 | "source": [ 114 | "* 其中常見的運算需要特別注意: / 為直接相除、 // 為取商、 % 為取餘數。" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 8, 120 | "metadata": { 121 | "colab": {}, 122 | "colab_type": "code", 123 | "id": "r28ZmHbjK5bp" 124 | }, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "f = 2.3333333333333335\n", 131 | "g = 2\n", 132 | "h = 1\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "f = 7 / 3\n", 138 | "g = 7 // 3\n", 139 | "h = 7 % 3\n", 140 | "\n", 141 | "#觀察計算結果\n", 142 | "print(\"f =\",f)\n", 143 | "print(\"g =\",g)\n", 144 | "print(\"h =\",h)" 145 | ] 146 | }, 147 | { 148 | "cell_type": "markdown", 149 | "metadata": { 150 | "colab_type": "text", 151 | "id": "h6k1giEoK9gv" 152 | }, 153 | "source": [ 154 | "* 邏輯運算,常作為判別結果用途,回傳的值為 True 或 False。" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 12, 160 | "metadata": { 161 | "colab": {}, 162 | "colab_type": "code", 163 | "id": "KVK9XssqLAr2" 164 | }, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "a = 1 , b = 2\n", 171 | "False (a是否等於b)\n", 172 | "False (a是否大於b)\n", 173 | "True (a是否小於b)\n", 174 | "False (a是否大於等於b)\n", 175 | "True (a是否小於等於b)\n", 176 | "True (a是否不等於b)\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "a == b #a是否等於b\n", 182 | "a > b #a是否大於b\n", 183 | "a < b #a是否小於b\n", 184 | "a >= b #a是否大於等於b\n", 185 | "a <= b #a是否小於等於b\n", 186 | "a != b #a是否不等於b\n", 187 | "\n", 188 | "#觀察判斷結果\n", 189 | "print(\"a =\",a,\", b =\", b)\n", 190 | "print(a == b, \"(a是否等於b)\")\n", 191 | "print(a > b, \"(a是否大於b)\")\n", 192 | "print(a < b, \"(a是否小於b)\")\n", 193 | "print(a >= b, \"(a是否大於等於b)\")\n", 194 | "print(a <= b, \"(a是否小於等於b)\")\n", 195 | "print(a != b, \"(a是否不等於b)\")" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "metadata": {}, 201 | "source": [ 202 | "## 課程文件\n", 203 | "\n", 204 | "#### Python Basics 課程清單\n", 205 | "- Lesson00 Preface 課程前言 \n", 206 | "\n", 207 | "- Lesson01 Variable 變數 (Last Chapter! --上一堂課程--)\n", 208 | "\n", 209 | "- Lesson02 Operator 運算子 (We are here now! --本篇課程--)\n", 210 | "\n", 211 | "- Lesson03 Data Structure 資料結構 (Next Chapter! --下一堂課程--)\n", 212 | "\n", 213 | "- Lesson04 String 字串\n", 214 | "\n", 215 | "- Lesson05 List 列表\n", 216 | "\n", 217 | "- Lesson06 Tuple 元組\n", 218 | "\n", 219 | "- Lesson07 Range 範圍\n", 220 | "\n", 221 | "- Lesson08 Set 集合\n", 222 | "\n", 223 | "- Lesson09 Dictionary 字典\n", 224 | "\n", 225 | "- Lesson10 If-else 條件語法\n", 226 | "\n", 227 | "- Lesson11 While 迴圈\n", 228 | "\n", 229 | "- Lesson12 For 迴圈\n", 230 | "\n", 231 | "- Lesson13 Function 函式\n", 232 | "\n", 233 | "- Lesson14 Input/Output 輸入/輸出\n", 234 | "\n", 235 | "- Lesson15 Class 類別" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [] 244 | } 245 | ], 246 | "metadata": { 247 | "colab": { 248 | "collapsed_sections": [], 249 | "name": "course2:Operator.ipynb", 250 | "provenance": [], 251 | "version": "0.3.2" 252 | }, 253 | "kernelspec": { 254 | "display_name": "Python 3", 255 | "language": "python", 256 | "name": "python3" 257 | }, 258 | "language_info": { 259 | "codemirror_mode": { 260 | "name": "ipython", 261 | "version": 3 262 | }, 263 | "file_extension": ".py", 264 | "mimetype": "text/x-python", 265 | "name": "python", 266 | "nbconvert_exporter": "python", 267 | "pygments_lexer": "ipython3", 268 | "version": "3.7.3" 269 | } 270 | }, 271 | "nbformat": 4, 272 | "nbformat_minor": 1 273 | } 274 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Lesson07 Range-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "S4F7QCIWGiLv" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 07 範圍 (1-07)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "F8f-ETEwOhkA" 26 | }, 27 | "source": [ 28 | "# Ranges ( 範圍 )\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "wgnzZIUAOoLD" 36 | }, 37 | "source": [ 38 | "\n", 39 | "\n", 40 | "* Range 內的元素是不可變的,並且可以使用索引,通常用於迴圈(Loop)\n", 41 | "* 宣告方式為 range( start , stop, step )\n", 42 | "* start : 計數從 start 為起點。如果沒有數字就是從0開始。\n", 43 | "* stop : 計數到 stop 為終點。但不包括 stop 的數字。\n", 44 | "* step : 移動步長,沒有數字就視為 1 。\n" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 16, 50 | "metadata": { 51 | "colab": { 52 | "base_uri": "https://localhost:8080/", 53 | "height": 221 54 | }, 55 | "colab_type": "code", 56 | "executionInfo": { 57 | "elapsed": 841, 58 | "status": "ok", 59 | "timestamp": 1566141675125, 60 | "user": { 61 | "displayName": "電機工程系莊翔宇", 62 | "photoUrl": "", 63 | "userId": "06232090834761520364" 64 | }, 65 | "user_tz": -480 66 | }, 67 | "id": "-MZd6NbXNsf5", 68 | "outputId": "843e604e-9121-4e1f-8f12-dfd99b6f2c90" 69 | }, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "0,1,2,3,4,\n", 76 | "0 ~ 4,但不包括5\n", 77 | "\n", 78 | "3,4,5,6,7,8,9,\n", 79 | "從3開始,但不包括10\n", 80 | "\n", 81 | "3,5,7,9,\n", 82 | "從3開始,但不包括10,每次跳2\n", 83 | "\n", 84 | "可以遞增也可以遞減\n", 85 | "10,5,0,-5,-10,-15,-20,-25,\n", 86 | "從10開始,但不包括-30,每次跳-5\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "for i in range(5):\n", 92 | " print(i,end=\",\")\n", 93 | "print()\n", 94 | "print(\"0 ~ 4,但不包括5\")\n", 95 | "\n", 96 | "print()\n", 97 | "for i in range(3,10):\n", 98 | " print(i,end=\",\")\n", 99 | "print() \n", 100 | "print(\"從3開始,但不包括10\")\n", 101 | "\n", 102 | "print()\n", 103 | "for i in range(3,10,2):\n", 104 | " print(i,end=\",\")\n", 105 | "print() \n", 106 | "print(\"從3開始,但不包括10,每次跳2\")\n", 107 | "\n", 108 | "print()\n", 109 | "print(\"可以遞增也可以遞減\")\n", 110 | "for i in range(10,-30,-5):\n", 111 | " print(i,end=\",\")\n", 112 | "print() \n", 113 | "print(\"從10開始,但不包括-30,每次跳-5\")" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": { 119 | "colab_type": "text", 120 | "id": "xToljETJSwbX" 121 | }, 122 | "source": [ 123 | "* 來看看其他用法" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 11, 129 | "metadata": { 130 | "colab": { 131 | "base_uri": "https://localhost:8080/", 132 | "height": 119 133 | }, 134 | "colab_type": "code", 135 | "executionInfo": { 136 | "elapsed": 630, 137 | "status": "ok", 138 | "timestamp": 1566141387574, 139 | "user": { 140 | "displayName": "電機工程系莊翔宇", 141 | "photoUrl": "", 142 | "userId": "06232090834761520364" 143 | }, 144 | "user_tz": -480 145 | }, 146 | "id": "P0L0yXFNQ9Hj", 147 | "outputId": "a6307d57-76f7-49e4-9027-0aea99d564a8" 148 | }, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "[[0, 'jane']]\n", 155 | "[[0, 'jane'], [1, 'john']]\n", 156 | "[[0, 'jane'], [1, 'john'], [2, 'mark']]\n", 157 | "[[0, 'jane'], [1, 'john'], [2, 'mark'], [3, 'harry']]\n", 158 | "[[0, 'jane'], [1, 'john'], [2, 'mark'], [3, 'harry'], [4, 'mike']]\n", 159 | "[[0, 'jane'], [1, 'john'], [2, 'mark'], [3, 'harry'], [4, 'mike'], [5, 'ed']]\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "words = ['jane','john','mark','harry','mike','ed']\n", 165 | "wordlist = []\n", 166 | "for i in range(len(words)):\n", 167 | " wordlist.append([i,words[i]])\n", 168 | " print(wordlist)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": { 174 | "colab_type": "text", 175 | "id": "qc2eXly9TXk7" 176 | }, 177 | "source": [ 178 | "* 也可以把 range 宣告成變數" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 15, 184 | "metadata": { 185 | "colab": { 186 | "base_uri": "https://localhost:8080/", 187 | "height": 136 188 | }, 189 | "colab_type": "code", 190 | "executionInfo": { 191 | "elapsed": 695, 192 | "status": "ok", 193 | "timestamp": 1566141598136, 194 | "user": { 195 | "displayName": "電機工程系莊翔宇", 196 | "photoUrl": "", 197 | "userId": "06232090834761520364" 198 | }, 199 | "user_tz": -480 200 | }, 201 | "id": "ckJcY8e0TRBu", 202 | "outputId": "23af5576-7ea9-4f20-e4b0-fe648249c523" 203 | }, 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "range(0, 10, 2)\n", 210 | "[0, 2, 4, 6, 8]\n", 211 | "True\n", 212 | "1\n", 213 | "6\n", 214 | "range(0, 6, 2)\n", 215 | "8\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "r = range(0,10,2)\n", 221 | "print(r)\n", 222 | "print(list(r))\n", 223 | "print(2 in r)\n", 224 | "print(r.index(2))\n", 225 | "print(r[3])\n", 226 | "print(r[:3])\n", 227 | "print(r[-1])" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": { 233 | "colab": {}, 234 | "colab_type": "code", 235 | "id": "2ZLG-WJVTfOZ" 236 | }, 237 | "source": [ 238 | "## 課程文件\n", 239 | "\n", 240 | "#### Python Basics 課程清單\n", 241 | "- Lesson00 Preface 課程前言 \n", 242 | "\n", 243 | "- Lesson01 Variable 變數 \n", 244 | "\n", 245 | "- Lesson02 Operator 運算子\n", 246 | "\n", 247 | "- Lesson03 Data Structure 資料結構 \n", 248 | "\n", 249 | "- Lesson04 String 字串\n", 250 | "\n", 251 | "- Lesson05 List 列表 \n", 252 | "\n", 253 | "- Lesson06 Tuple 元組 (Last Chapter! --上一堂課程--)\n", 254 | "\n", 255 | "- Lesson07 Range 範圍 (We are here now! --本篇課程--)\n", 256 | "\n", 257 | "- Lesson08 Set 集合 (Next Chapter! --下一堂課程--)\n", 258 | "\n", 259 | "- Lesson09 Dictionary 字典\n", 260 | "\n", 261 | "- Lesson10 If-else 條件語法\n", 262 | "\n", 263 | "- Lesson11 While 迴圈\n", 264 | "\n", 265 | "- Lesson12 For 迴圈\n", 266 | "\n", 267 | "- Lesson13 Function 函式\n", 268 | "\n", 269 | "- Lesson14 Input/Output 輸入/輸出\n", 270 | "\n", 271 | "- Lesson15 Class 類別" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [] 280 | } 281 | ], 282 | "metadata": { 283 | "colab": { 284 | "collapsed_sections": [], 285 | "name": "course7: Ranges.ipynb", 286 | "provenance": [], 287 | "version": "0.3.2" 288 | }, 289 | "kernelspec": { 290 | "display_name": "Python 3", 291 | "language": "python", 292 | "name": "python3" 293 | }, 294 | "language_info": { 295 | "codemirror_mode": { 296 | "name": "ipython", 297 | "version": 3 298 | }, 299 | "file_extension": ".py", 300 | "mimetype": "text/x-python", 301 | "name": "python", 302 | "nbconvert_exporter": "python", 303 | "pygments_lexer": "ipython3", 304 | "version": "3.7.3" 305 | } 306 | }, 307 | "nbformat": 4, 308 | "nbformat_minor": 1 309 | } 310 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Lesson08 Set-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "eIYX18qMb_Ug" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 08 範圍 (1-08)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "g_qwNxi1cBz6" 26 | }, 27 | "source": [ 28 | "# Set (集合)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "zh8NQDatdJ9t" 36 | }, 37 | "source": [ 38 | "* set() 函數創建一個無序不重複元素集,可進行關係測試,刪除重複數據,還可以計算交集、差集、並集等。" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 0, 44 | "metadata": { 45 | "colab": {}, 46 | "colab_type": "code", 47 | "id": "4pNYZD3jbj8p" 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "# class set([iterable])\n", 52 | "# iterable -- 可迭代對象對象;" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": { 58 | "colab_type": "text", 59 | "id": "cPj-rQaVg23D" 60 | }, 61 | "source": [ 62 | "## 實例" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": { 68 | "colab_type": "text", 69 | "id": "unbQorm4eufQ" 70 | }, 71 | "source": [ 72 | "* 以下實例展示了 set 的使用方法:" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 0, 78 | "metadata": { 79 | "colab": { 80 | "base_uri": "https://localhost:8080/", 81 | "height": 102 82 | }, 83 | "colab_type": "code", 84 | "executionInfo": { 85 | "elapsed": 649, 86 | "status": "ok", 87 | "timestamp": 1566145137680, 88 | "user": { 89 | "displayName": "電機工程系李佑謙", 90 | "photoUrl": "", 91 | "userId": "05735385076343474033" 92 | }, 93 | "user_tz": -480 94 | }, 95 | "id": "wtt9P1TehM6_", 96 | "outputId": "afd4b6e9-d0fe-46f0-d6b4-a68b5b84f91c" 97 | }, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 104 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 105 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 106 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 107 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "x = set('runoob')\n", 113 | "y = set('google')\n", 114 | "print(x,y)\n", 115 | "\n", 116 | "x, y\n", 117 | "(set(['b','r','u','o','n']), set(['e','o','g','l'])) # 重複的被刪除\n", 118 | "print(x,y)\n", 119 | "\n", 120 | "x & y # 交集\n", 121 | "set(['o'])\n", 122 | "print(x,y)\n", 123 | "\n", 124 | "x | y # 並集\n", 125 | "set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u'])\n", 126 | "print(x,y)\n", 127 | "\n", 128 | "x - y # 差集\n", 129 | "set(['r','b','u','n'])\n", 130 | "print(x,y)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": { 136 | "colab_type": "text", 137 | "id": "glPs_dsjhLtx" 138 | }, 139 | "source": [ 140 | "## 課程文件\n", 141 | "\n", 142 | "#### Python Basics 課程清單\n", 143 | "- Lesson00 Preface 課程前言 \n", 144 | "\n", 145 | "- Lesson01 Variable 變數 \n", 146 | "\n", 147 | "- Lesson02 Operator 運算子\n", 148 | "\n", 149 | "- Lesson03 Data Structure 資料結構 \n", 150 | "\n", 151 | "- Lesson04 String 字串\n", 152 | "\n", 153 | "- Lesson05 List 列表 \n", 154 | "\n", 155 | "- Lesson06 Tuple 元組\n", 156 | "\n", 157 | "- Lesson07 Range 範圍 (Last Chapter! --上一堂課程--)\n", 158 | "\n", 159 | "- Lesson08 Set 集合 (We are here now! --本篇課程--)\n", 160 | "\n", 161 | "- Lesson09 Dictionary 字典 (Next Chapter! --下一堂課程--)\n", 162 | "\n", 163 | "- Lesson10 If-else 條件語法\n", 164 | "\n", 165 | "- Lesson11 While 迴圈\n", 166 | "\n", 167 | "- Lesson12 For 迴圈\n", 168 | "\n", 169 | "- Lesson13 Function 函式\n", 170 | "\n", 171 | "- Lesson14 Input/Output 輸入/輸出\n", 172 | "\n", 173 | "- Lesson15 Class 類別" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [] 182 | } 183 | ], 184 | "metadata": { 185 | "colab": { 186 | "collapsed_sections": [], 187 | "name": "course8:set.ipynb", 188 | "provenance": [], 189 | "version": "0.3.2" 190 | }, 191 | "kernelspec": { 192 | "display_name": "Python 3", 193 | "language": "python", 194 | "name": "python3" 195 | }, 196 | "language_info": { 197 | "codemirror_mode": { 198 | "name": "ipython", 199 | "version": 3 200 | }, 201 | "file_extension": ".py", 202 | "mimetype": "text/x-python", 203 | "name": "python", 204 | "nbconvert_exporter": "python", 205 | "pygments_lexer": "ipython3", 206 | "version": "3.7.3" 207 | } 208 | }, 209 | "nbformat": 4, 210 | "nbformat_minor": 1 211 | } 212 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Lesson10 If-else-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "N177jyz4StvH" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 10 條件語法 (1-10)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "-F8EhN_TTmxn" 26 | }, 27 | "source": [ 28 | "# If else(條件語法)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "MB7lAcJETyza" 36 | }, 37 | "source": [ 38 | "Python條件語法是通過一條或多條語句的執行結果(True或者False)來決定執行的代碼。\n", 39 | "\n", 40 | "可以通過下圖來簡單瞭解條件語句的執行過程:" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": { 46 | "colab_type": "text", 47 | "id": "YJl_C-y3T7i5" 48 | }, 49 | "source": [ 50 | "![替代文字](https://www.tutorialspoint.com/python/images/if_else_statement.jpg)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": { 56 | "colab_type": "text", 57 | "id": "DFjlhBS1UGPa" 58 | }, 59 | "source": [ 60 | "Python程序語言指定任何非0和非空(null)值爲true,0 或者 null爲false。\n", 61 | "\n", 62 | "Python 編程中 if 語句用於控制程序的執行,基本形式爲:\n", 63 | "\n", 64 | "\n", 65 | "```\n", 66 | "if 判斷條件:\n", 67 | " 執行語句……\n", 68 | "else:\n", 69 | " 執行語句……\n", 70 | "```\n", 71 | "\n" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": { 77 | "colab_type": "text", 78 | "id": "qnJcDIIeVU13" 79 | }, 80 | "source": [ 81 | "其中\"判斷條件\"成立時(非零),則執行後面的語句,而執行內容可以多行,以同樣的縮排來區分表示同一範圍。\n", 82 | "\n", 83 | "else 爲可選語句,當上述條件不成立時執行內容則可以執行相關語句。" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 2, 89 | "metadata": { 90 | "colab": { 91 | "base_uri": "https://localhost:8080/", 92 | "height": 51 93 | }, 94 | "colab_type": "code", 95 | "executionInfo": { 96 | "elapsed": 656, 97 | "status": "ok", 98 | "timestamp": 1567266304265, 99 | "user": { 100 | "displayName": "電機工程系李佑謙", 101 | "photoUrl": "", 102 | "userId": "05735385076343474033" 103 | }, 104 | "user_tz": -480 105 | }, 106 | "id": "BSWF8OPpSZu-", 107 | "outputId": "771c3e01-d57e-42dc-e717-5b11210bce7b" 108 | }, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "hi!\n", 115 | "None\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "x = 1\n", 121 | "if x == 1: # 判斷變量是否爲 1\n", 122 | " print('hi!') # 並輸出信息\n", 123 | " \n", 124 | "else:\n", 125 | " print('None')\n", 126 | "\n", 127 | "x = 2\n", 128 | "if x == 1: \n", 129 | " print('hi!') \n", 130 | "else:\n", 131 | " print('None')" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": { 137 | "colab_type": "text", 138 | "id": "cl0STcCEWBsW" 139 | }, 140 | "source": [ 141 | "if 語句的判斷條件可以用>(大於)、<(小於)、==(等於)、>=(大於等於)、<=(小於等於)來表示其關係。\n", 142 | "\n", 143 | "當判斷條件爲多個值時,可以使用以下形式:\n", 144 | "\n", 145 | "\n", 146 | "```\n", 147 | "if 判斷條件1:\n", 148 | " 執行語句1……\n", 149 | "elif 判斷條件2:\n", 150 | " 執行語句2……\n", 151 | "elif 判斷條件3:\n", 152 | " 執行語句3……\n", 153 | "else:\n", 154 | " 執行語句4……\n", 155 | "```\n", 156 | "\n" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 3, 162 | "metadata": { 163 | "colab": { 164 | "base_uri": "https://localhost:8080/", 165 | "height": 34 166 | }, 167 | "colab_type": "code", 168 | "executionInfo": { 169 | "elapsed": 699, 170 | "status": "ok", 171 | "timestamp": 1567266437408, 172 | "user": { 173 | "displayName": "電機工程系李佑謙", 174 | "photoUrl": "", 175 | "userId": "05735385076343474033" 176 | }, 177 | "user_tz": -480 178 | }, 179 | "id": "4cfe0V-tWIWv", 180 | "outputId": "8a97ee29-db26-48e8-bf99-9963271e5205" 181 | }, 182 | "outputs": [ 183 | { 184 | "name": "stdout", 185 | "output_type": "stream", 186 | "text": [ 187 | "twenty-one\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "number = 21 \n", 193 | "if number == 18: # 判斷number的值\n", 194 | " print('eighteen') \n", 195 | "elif number == 21:\n", 196 | " print('twenty-one') \n", 197 | "elif number == 1:\n", 198 | " print('one') \n", 199 | "else:\n", 200 | " print('not') # 條件均不成立時輸出" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": { 206 | "colab_type": "text", 207 | "id": "ERnTmg27XKUO" 208 | }, 209 | "source": [ 210 | "由於 python 並不支持 switch 語句,所以多個條件判斷,只能用 elif 來實現,如果判斷需要多個條件需同時判斷時,可以使用 or (或),表示兩個條件有一個成立時判斷條件成功;使用 and (與)時,表示只有兩個條件同時成立的情況下,判斷條件才成功。" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 4, 216 | "metadata": { 217 | "colab": { 218 | "base_uri": "https://localhost:8080/", 219 | "height": 68 220 | }, 221 | "colab_type": "code", 222 | "executionInfo": { 223 | "elapsed": 677, 224 | "status": "ok", 225 | "timestamp": 1567266766421, 226 | "user": { 227 | "displayName": "電機工程系李佑謙", 228 | "photoUrl": "", 229 | "userId": "05735385076343474033" 230 | }, 231 | "user_tz": -480 232 | }, 233 | "id": "AFWeJ8-_XJmQ", 234 | "outputId": "86022692-15e7-4da3-b4bc-a584dce34c33" 235 | }, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "between 0 and 10\n", 242 | "undefine\n", 243 | "undefine\n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "num = 9\n", 249 | "if num >= 0 and num <= 10: # 判斷值是否在0~10之間\n", 250 | " print('between 0 and 10')\n", 251 | "# 輸出結果:between 0 and 10\n", 252 | " \n", 253 | "num = 10\n", 254 | "if num < 0 or num > 10: # 判斷值是否在小於0或大於10\n", 255 | " print('less than 0 or more than 10')\n", 256 | "else:\n", 257 | " print('undefine')\n", 258 | "# 輸出結果: undefine\n", 259 | " \n", 260 | "num = 8\n", 261 | "# 判斷值是否在0~5或者10~15之間\n", 262 | "if (num >= 0 and num <= 5) or (num >= 10 and num <= 15): \n", 263 | " print('(more or equal 0 and less or equal than 5) or (more or equal 10 and less or equal than 15)')\n", 264 | "else:\n", 265 | " print('undefine')\n", 266 | "# 輸出結果: undefine" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": { 272 | "colab_type": "text", 273 | "id": "7VtH-IM0YQgP" 274 | }, 275 | "source": [ 276 | "當if有多個條件時可使用括號來區分判斷的先後順序,括號中的判斷優先執行,此外 and 和 or 的優先級低於>(大於)、<(小於)等判斷符號,即大於和小於在沒有括號的情況下會比與或要優先判斷。" 277 | ] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "metadata": { 282 | "colab_type": "text", 283 | "id": "ju1S-GY7YTwD" 284 | }, 285 | "source": [ 286 | "## 簡單的語句組" 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "metadata": { 292 | "colab_type": "text", 293 | "id": "1Yti37DdYXg8" 294 | }, 295 | "source": [ 296 | "你也可以在同一行的位置上使用if條件判斷語句,如下實例:" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 6, 302 | "metadata": { 303 | "colab": { 304 | "base_uri": "https://localhost:8080/", 305 | "height": 51 306 | }, 307 | "colab_type": "code", 308 | "executionInfo": { 309 | "elapsed": 629, 310 | "status": "ok", 311 | "timestamp": 1567266894695, 312 | "user": { 313 | "displayName": "電機工程系李佑謙", 314 | "photoUrl": "", 315 | "userId": "05735385076343474033" 316 | }, 317 | "user_tz": -480 318 | }, 319 | "id": "AaOsVZ9NYkTl", 320 | "outputId": "30f5e1a9-3dc9-42ce-cef2-b5c499520c31" 321 | }, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "變量 var 的值爲50\n", 328 | "Good bye!\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "var = 50 \n", 334 | " \n", 335 | "if ( var == 50 ) : print(\"變量 var 的值爲50\") \n", 336 | " \n", 337 | "print(\"Good bye!\")" 338 | ] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "metadata": {}, 343 | "source": [ 344 | "## 課程文件\n", 345 | "\n", 346 | "#### Python Basics 課程清單\n", 347 | "- Lesson00 Preface 課程前言 \n", 348 | "\n", 349 | "- Lesson01 Variable 變數 \n", 350 | "\n", 351 | "- Lesson02 Operator 運算子\n", 352 | "\n", 353 | "- Lesson03 Data Structure 資料結構 \n", 354 | "\n", 355 | "- Lesson04 String 字串\n", 356 | "\n", 357 | "- Lesson05 List 列表 \n", 358 | "\n", 359 | "- Lesson06 Tuple 元組\n", 360 | "\n", 361 | "- Lesson07 Range 範圍\n", 362 | "\n", 363 | "- Lesson08 Set 集合\n", 364 | "\n", 365 | "- Lesson09 Dictionary 字典 (Last Chapter! --上一堂課程--)\n", 366 | "\n", 367 | "- Lesson10 If-else 條件語法 (We are here now! --本篇課程--)\n", 368 | "\n", 369 | "- Lesson11 While 迴圈 (Next Chapter! --下一堂課程--)\n", 370 | "\n", 371 | "- Lesson12 For 迴圈 \n", 372 | "\n", 373 | "- Lesson13 Function 函式\n", 374 | "\n", 375 | "- Lesson14 Input/Output 輸入/輸出\n", 376 | "\n", 377 | "- Lesson15 Class 類別" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": null, 383 | "metadata": {}, 384 | "outputs": [], 385 | "source": [] 386 | } 387 | ], 388 | "metadata": { 389 | "colab": { 390 | "collapsed_sections": [], 391 | "name": "If_else.ipynb", 392 | "provenance": [], 393 | "version": "0.3.2" 394 | }, 395 | "kernelspec": { 396 | "display_name": "Python 3", 397 | "language": "python", 398 | "name": "python3" 399 | }, 400 | "language_info": { 401 | "codemirror_mode": { 402 | "name": "ipython", 403 | "version": 3 404 | }, 405 | "file_extension": ".py", 406 | "mimetype": "text/x-python", 407 | "name": "python", 408 | "nbconvert_exporter": "python", 409 | "pygments_lexer": "ipython3", 410 | "version": "3.7.3" 411 | } 412 | }, 413 | "nbformat": 4, 414 | "nbformat_minor": 1 415 | } 416 | -------------------------------------------------------------------------------- /documents/.ipynb_checkpoints/Lesson12 For-loop-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "rGCLGcuEPVSa" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 12 For 迴圈 (1-12)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "EmvrmGKYPZz3" 26 | }, 27 | "source": [ 28 | "# For迴圈" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "jKOZ696hFiiu" 36 | }, 37 | "source": [ 38 | "* 就像while迴圈一樣 for循環可以遍歷任何序列的項目,如一個列表或者一個字符串。" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": { 44 | "colab_type": "text", 45 | "id": "FHG_5fQXH1RZ" 46 | }, 47 | "source": [ 48 | "* for循環的語法格式如下:" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 0, 54 | "metadata": { 55 | "colab": {}, 56 | "colab_type": "code", 57 | "id": "QZPQ2d5UPR-F" 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "#for iterating_var in sequence:\n", 62 | "# statements(s)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": { 68 | "colab_type": "text", 69 | "id": "E6dHdcF1IAv7" 70 | }, 71 | "source": [ 72 | "![替代文字](http://www.runoob.com/wp-content/uploads/2013/11/python_for_loop.jpg)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 0, 78 | "metadata": { 79 | "colab": { 80 | "base_uri": "https://localhost:8080/", 81 | "height": 221 82 | }, 83 | "colab_type": "code", 84 | "executionInfo": { 85 | "elapsed": 893, 86 | "status": "ok", 87 | "timestamp": 1566658988248, 88 | "user": { 89 | "displayName": "電機工程系李佑謙", 90 | "photoUrl": "", 91 | "userId": "05735385076343474033" 92 | }, 93 | "user_tz": -480 94 | }, 95 | "id": "C5da9vhyIbB4", 96 | "outputId": "73652b42-db7e-4861-f205-4be6209dbe60" 97 | }, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "當前字母 : N\n", 104 | "當前字母 : e\n", 105 | "當前字母 : w\n", 106 | "當前字母 : \n", 107 | "當前字母 : Y\n", 108 | "當前字母 : o\n", 109 | "當前字母 : r\n", 110 | "當前字母 : k\n", 111 | "當前水果 : Detroit\n", 112 | "當前水果 : Arizona\n", 113 | "當前水果 : Georgia\n", 114 | "finish\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "for ex in 'New York': # 第一個實例\n", 120 | " print('當前字母 :',ex)\n", 121 | " \n", 122 | "fruits = ['Detroit','Arizona','Georgia']\n", 123 | "for fruit in fruits: # 第二個實例\n", 124 | " print('當前地名 :',fruit)\n", 125 | " \n", 126 | "print(\"finish\")" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": { 132 | "colab_type": "text", 133 | "id": "9JigJ0O_J6n_" 134 | }, 135 | "source": [ 136 | "## 通過序列索引迭代" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": { 142 | "colab_type": "text", 143 | "id": "b2Zg4xH_KAQd" 144 | }, 145 | "source": [ 146 | "* 另外一種執行循環的遍歷方式是通過索引,如下實例:" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 0, 152 | "metadata": { 153 | "colab": { 154 | "base_uri": "https://localhost:8080/", 155 | "height": 85 156 | }, 157 | "colab_type": "code", 158 | "executionInfo": { 159 | "elapsed": 678, 160 | "status": "ok", 161 | "timestamp": 1566659431765, 162 | "user": { 163 | "displayName": "電機工程系李佑謙", 164 | "photoUrl": "", 165 | "userId": "05735385076343474033" 166 | }, 167 | "user_tz": -480 168 | }, 169 | "id": "me4EMzYkIgjh", 170 | "outputId": "d061ca19-60cd-4b88-ca93-a17b1e9143e5" 171 | }, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "當前國家 : New Jersey\n", 178 | "當前國家 : Nevada\n", 179 | "當前國家 : Montana\n", 180 | "finish!\n" 181 | ] 182 | } 183 | ], 184 | "source": [ 185 | "state = ['New Jersey', 'Nevada', 'Montana']\n", 186 | "for index in range(len(state)):\n", 187 | " print('當前國家 :', state[index])\n", 188 | " \n", 189 | "print(\"finish!\")" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": { 195 | "colab_type": "text", 196 | "id": "skzu2KQtIFof" 197 | }, 198 | "source": [ 199 | "* 以上實例我們使用了內置函數 len() 和 range(),函數 len() 返回列表的長度,即元素的個數。 range返回一個序列的數。" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": { 205 | "colab_type": "text", 206 | "id": "uiOgQauwLnXY" 207 | }, 208 | "source": [ 209 | "## 循環使用 else 語句" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": { 215 | "colab_type": "text", 216 | "id": "AYXO0UP1Lsdy" 217 | }, 218 | "source": [ 219 | "* 在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是通過 break 跳出而中斷的)的情況下執行,while … else 也是一樣。" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 0, 225 | "metadata": { 226 | "colab": { 227 | "base_uri": "https://localhost:8080/", 228 | "height": 357 229 | }, 230 | "colab_type": "code", 231 | "executionInfo": { 232 | "elapsed": 674, 233 | "status": "ok", 234 | "timestamp": 1566659661038, 235 | "user": { 236 | "displayName": "電機工程系李佑謙", 237 | "photoUrl": "", 238 | "userId": "05735385076343474033" 239 | }, 240 | "user_tz": -480 241 | }, 242 | "id": "hu7ybwNSL130", 243 | "outputId": "53336914-d848-4923-cdac-adf5b3e48fe3" 244 | }, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "0 是一個質數\n", 251 | "1 是一個質數\n", 252 | "2 是一個質數\n", 253 | "3 是一個質數\n", 254 | "4 等於 2 * 2\n", 255 | "5 是一個質數\n", 256 | "6 等於 2 * 3\n", 257 | "7 是一個質數\n", 258 | "8 等於 2 * 4\n", 259 | "9 等於 3 * 3\n", 260 | "10 等於 2 * 5\n", 261 | "11 是一個質數\n", 262 | "12 等於 2 * 6\n", 263 | "13 是一個質數\n", 264 | "14 等於 2 * 7\n", 265 | "15 等於 3 * 5\n", 266 | "16 等於 2 * 8\n", 267 | "17 是一個質數\n", 268 | "18 等於 2 * 9\n", 269 | "19 是一個質數\n" 270 | ] 271 | } 272 | ], 273 | "source": [ 274 | "for num in range(0,20): # 迭代 10 到 20 之間的數字\n", 275 | " for i in range(2,num): # 根據因子迭代\n", 276 | " if num%i == 0: # 確定第一個因子\n", 277 | " j=num/i # 計算第二個因子\n", 278 | " print('%d 等於 %d * %d' % (num,i,j))\n", 279 | " break # 跳出當前循環\n", 280 | " else: # 循環的 else 部分\n", 281 | " print(num, '是一個質數')" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": { 287 | "colab_type": "text", 288 | "id": "nqDzLTZ4MVln" 289 | }, 290 | "source": [ 291 | "## 課程文件\n", 292 | "\n", 293 | "#### Python Basics 課程清單\n", 294 | "- Lesson00 Preface 課程前言 \n", 295 | "\n", 296 | "- Lesson01 Variable 變數 \n", 297 | "\n", 298 | "- Lesson02 Operator 運算子\n", 299 | "\n", 300 | "- Lesson03 Data Structure 資料結構 \n", 301 | "\n", 302 | "- Lesson04 String 字串\n", 303 | "\n", 304 | "- Lesson05 List 列表 \n", 305 | "\n", 306 | "- Lesson06 Tuple 元組 \n", 307 | "\n", 308 | "- Lesson07 Range 範圍 \n", 309 | "\n", 310 | "- Lesson08 Set 集合 \n", 311 | "\n", 312 | "- Lesson09 Dictionary 字典\n", 313 | "\n", 314 | "- Lesson10 If-else 條件語法\n", 315 | "\n", 316 | "- Lesson11 While 迴圈 (Last Chapter! --上一堂課程--)\n", 317 | "\n", 318 | "- Lesson12 For 迴圈 (We are here now! --本篇課程--)\n", 319 | "\n", 320 | "- Lesson13 Function 函式 (Next Chapter! --下一堂課程--)\n", 321 | "\n", 322 | "- Lesson14 Input/Output 輸入/輸出\n", 323 | "\n", 324 | "- Lesson15 Class 類別" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": null, 330 | "metadata": {}, 331 | "outputs": [], 332 | "source": [] 333 | } 334 | ], 335 | "metadata": { 336 | "colab": { 337 | "collapsed_sections": [], 338 | "name": "course12:For_Loop.ipynb", 339 | "provenance": [], 340 | "version": "0.3.2" 341 | }, 342 | "kernelspec": { 343 | "display_name": "Python 3", 344 | "language": "python", 345 | "name": "python3" 346 | }, 347 | "language_info": { 348 | "codemirror_mode": { 349 | "name": "ipython", 350 | "version": 3 351 | }, 352 | "file_extension": ".py", 353 | "mimetype": "text/x-python", 354 | "name": "python", 355 | "nbconvert_exporter": "python", 356 | "pygments_lexer": "ipython3", 357 | "version": "3.7.3" 358 | } 359 | }, 360 | "nbformat": 4, 361 | "nbformat_minor": 1 362 | } 363 | -------------------------------------------------------------------------------- /documents/Lesson00 Preface.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "accelerator": "GPU", 6 | "colab": { 7 | "name": "AI . FREE Team - Python Basics: Lesson 00 Preface 前言.ipynb", 8 | "provenance": [], 9 | "collapsed_sections": [ 10 | "lo-XP-5gAYLW", 11 | "D0XaUjY4JiZ9", 12 | "sZPa1lEfnDcw", 13 | "z8a-OLPrnKUQ" 14 | ], 15 | "toc_visible": true 16 | }, 17 | "kernelspec": { 18 | "display_name": "Python 3", 19 | "language": "python", 20 | "name": "python3" 21 | }, 22 | "language_info": { 23 | "codemirror_mode": { 24 | "name": "ipython", 25 | "version": 3 26 | }, 27 | "file_extension": ".py", 28 | "mimetype": "text/x-python", 29 | "name": "python", 30 | "nbconvert_exporter": "python", 31 | "pygments_lexer": "ipython3", 32 | "version": "3.7.3" 33 | } 34 | }, 35 | "cells": [ 36 | { 37 | "cell_type": "markdown", 38 | "metadata": { 39 | "colab_type": "text", 40 | "id": "Rib_rBYNuDI3" 41 | }, 42 | "source": [ 43 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 44 | "\n", 45 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 46 | "
\n", 47 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 0 前言 (1-0)。
\n", 48 | "
\n", 49 | "\n", 50 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n", 51 | "
\n", 52 | "\n", 53 | "
本課教學影片:https://youtu.be/aki45g6XN2c
\n", 54 | "\n", 55 | "\n", 56 | "\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": { 62 | "colab_type": "text", 63 | "id": "0LEJHkfYJTNU" 64 | }, 65 | "source": [ 66 | "# Preface 前言" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": { 72 | "colab_type": "text", 73 | "id": "lo-XP-5gAYLW" 74 | }, 75 | "source": [ 76 | "## 1. Why Python? 為何選擇 Python?" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": { 82 | "colab_type": "text", 83 | "id": "yf6Hp8n9GXAg" 84 | }, 85 | "source": [ 86 | "在眾多的程式語言當中,我們選擇 Python 做為初學者入門數據分析、人工智慧的語言,而不是 R, C, Matlab or Java 等其他語言,相對這些語言來說,Python 有以下優勢:\n", 87 | "\n", 88 | "\n", 89 | "1. 學習快速 (Python 屬於高階語言,較能使開發者快速讀懂程式碼)\n", 90 | "2. 資源豐富 (Python 的開源開發社群廣大,許多國際企業也都公開自家的開源套件;例如:Google, Facebook)\n", 91 | "3. 用途廣泛 (Python 不僅能作為商業分析、開發 AI 的工具、應用在科研領域,還能做為開發網頁的框架,如Django...)\n", 92 | "\n" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": { 98 | "colab_type": "text", 99 | "id": "D0XaUjY4JiZ9" 100 | }, 101 | "source": [ 102 | "## 2. Why Colab? 為何選擇 Colab 介面進行學習?" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": { 108 | "colab_type": "text", 109 | "id": "Bp2ZDcoyJtnu" 110 | }, 111 | "source": [ 112 | "Colab 為 Google 推出的免費雲端 Python 程式語言開發介面 (基於Jupyter notebook environment 開發),優點如下:\n", 113 | "\n", 114 | "\n", 115 | "1. 不用花費大量時間設定「開發環境」\n", 116 | "2. 提供許多整合性服務 (與Google Drive連結、Github開發社群、相關 Python 開源套件導入...)\n", 117 | "3. 提供免費雲端運算資源 (GPU, TPU 能使開發 AI 運算速度更快)\n", 118 | "4. 高度便利性 (可隨時隨地學習 -- 在有網路 & 電腦的狀況下)\n", 119 | "\n", 120 | "提供給讀者們: Google 官方 Colab 介紹文件 (https://colab.research.google.com/notebooks/welcome.ipynb)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": { 126 | "colab_type": "text", 127 | "id": "QA90qYogNbjg" 128 | }, 129 | "source": [ 130 | "# 第一堂課 Python Basics 基礎教學" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": { 136 | "colab_type": "text", 137 | "id": "sZPa1lEfnDcw" 138 | }, 139 | "source": [ 140 | "## 範例教學 - Print\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": { 146 | "colab_type": "text", 147 | "id": "xl_obAOHOAQz" 148 | }, 149 | "source": [ 150 | "一如程式語言學習的傳統,我們先從 Print 開始;Print 就如字面上意思,將東西列印出來。 (請以 Ctrl + Enter 執行下方程式語言列)\n", 151 | "\n", 152 | "> Print(\"Hello World!\")\n", 153 | "\n", 154 | "\n" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "metadata": { 160 | "colab_type": "code", 161 | "id": "jOBwRHanNk7Q", 162 | "colab": {}, 163 | "outputId": "d1c15e0d-916e-4d02-85c7-66ad935ccd7d" 164 | }, 165 | "source": [ 166 | "print(\"Hello World!\")" 167 | ], 168 | "execution_count": 0, 169 | "outputs": [ 170 | { 171 | "output_type": "stream", 172 | "text": [ 173 | "Hello World!\n" 174 | ], 175 | "name": "stdout" 176 | } 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": { 182 | "colab_type": "text", 183 | "id": "XF4mEjBHOtQs" 184 | }, 185 | "source": [ 186 | "執行成功後,你將看到:\n", 187 | "\n", 188 | "```\n", 189 | "Hello World!\n", 190 | "```\n", 191 | " * \"print()\" 的使用為程式語言開發的基礎工具,還有許多不同的使用方式,請多嘗試看看不同使用方法。\n", 192 | "\n", 193 | "\n" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": { 199 | "id": "kMRkeKlOu1oX", 200 | "colab_type": "text" 201 | }, 202 | "source": [ 203 | "## 課程文件\n", 204 | "\n", 205 | "#### Python Basics 課程清單\n", 206 | "- Lesson00 Preface 課程前言 (We are here now! --本篇課程--)\n", 207 | "\n", 208 | "- Lesson01 Variable 變數 (Next Chapter! --下一堂課程--)\n", 209 | "\n", 210 | "- Lesson02 Operator 運算子\n", 211 | "\n", 212 | "- Lesson03 Data Structure 資料結構\n", 213 | "\n", 214 | "- Lesson04 String 字串\n", 215 | "\n", 216 | "- Lesson05 List 列表\n", 217 | "\n", 218 | "- Lesson06 Tuple 元組\n", 219 | "\n", 220 | "- Lesson07 Range 範圍\n", 221 | "\n", 222 | "- Lesson08 Set 集合\n", 223 | "\n", 224 | "- Lesson09 Dictionary 字典\n", 225 | "\n", 226 | "- Lesson10 If-else 條件語法\n", 227 | "\n", 228 | "- Lesson11 While 迴圈\n", 229 | "\n", 230 | "- Lesson12 For 迴圈\n", 231 | "\n", 232 | "- Lesson13 Function 函式\n", 233 | "\n", 234 | "- Lesson14 Input/Output 輸入/輸出\n", 235 | "\n", 236 | "- Lesson15 Class 類別" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "metadata": { 242 | "id": "3OngnfeTu1oY", 243 | "colab_type": "code", 244 | "colab": {} 245 | }, 246 | "source": [ 247 | "" 248 | ], 249 | "execution_count": 0, 250 | "outputs": [] 251 | } 252 | ] 253 | } -------------------------------------------------------------------------------- /documents/Lesson01 Variable.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "accelerator": "GPU", 6 | "colab": { 7 | "name": "AI . FREE Team - Python Basics: Lesson 01 Variable 變數.ipynb", 8 | "provenance": [], 9 | "collapsed_sections": [], 10 | "toc_visible": true 11 | }, 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.7.3" 28 | } 29 | }, 30 | "cells": [ 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "Rib_rBYNuDI3" 36 | }, 37 | "source": [ 38 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 39 | "\n", 40 | "\n", 41 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 42 | "
\n", 43 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 01 變數 (1-01)。
\n", 44 | "
\n", 45 | "\n", 46 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n", 47 | "
\n", 48 | "\n", 49 | "\n", 50 | "
本課教學影片:https://youtu.be/DNMInSwgWK0
\n", 51 | "\n", 52 | "\n" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": { 58 | "colab_type": "text", 59 | "id": "z8a-OLPrnKUQ" 60 | }, 61 | "source": [ 62 | "## Variables 變數認識" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": { 68 | "colab_type": "text", 69 | "id": "JN8bNrZYoBZU" 70 | }, 71 | "source": [ 72 | "變數是什麼呢?相信大家並不陌生,這邊舉 BMI 計算為例,讓大家初步認識變數在程式語言開發中的使用。" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": { 78 | "colab_type": "text", 79 | "id": "ERLX_MhjoMoj" 80 | }, 81 | "source": [ 82 | "\n", 83 | "\n", 84 | "* **訂定第一個變數 height(高度) 為 1.8 (m)**\n", 85 | "* **訂定第二個變數 weight(體重) 為 80 (kg**) \n", 86 | "* 套入計算 BMI 的公式: **體重(kg) / 身高的平方(m * m)** \n", 87 | "* 查看計算結果 (以print來進行查看)\n", 88 | "\n", 89 | "※ **(請按 Ctrl + enter 執行下列)**\n" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "metadata": { 95 | "colab_type": "code", 96 | "id": "zK8aYkk2Omty", 97 | "colab": {}, 98 | "outputId": "8dc0af5b-49e7-4ee3-a488-2b805d7d1107" 99 | }, 100 | "source": [ 101 | "# 訂定第一個變數 height(高度)\n", 102 | "height = 1.8\n", 103 | "\n", 104 | "# 訂定第二個變數 weight(體重)\n", 105 | "weight = 80\n", 106 | "\n", 107 | "# 套入計算 BMI 的公式: 體重(kg) / 身高的平方(m * m)\n", 108 | "BMI = weight / (height*height)\n", 109 | "\n", 110 | "#將計算結果以 Print 呈現出來\n", 111 | "print(\"您的身高為: \" + str(height) + \"m (公尺)\")\n", 112 | "print(\"您的體重為: \" + str(weight) + \"kg (公斤)\")\n", 113 | "print(\"您的BMI為: \" + str(BMI))" 114 | ], 115 | "execution_count": 0, 116 | "outputs": [ 117 | { 118 | "output_type": "stream", 119 | "text": [ 120 | "您的身高為: 1.8m (公尺)\n", 121 | "您的體重為: 80kg (公斤)\n", 122 | "您的BMI為: 24.691358024691358\n" 123 | ], 124 | "name": "stdout" 125 | } 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": { 131 | "colab_type": "text", 132 | "id": "LKacVqLosLLx" 133 | }, 134 | "source": [ 135 | "* 以上簡單製作出 BMI 的計算工具,請嘗試看看修改身高 or 體重,來體會如何使用變數!" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": { 141 | "colab_type": "text", 142 | "id": "1_praOaBX-i7" 143 | }, 144 | "source": [ 145 | "* 變數可以是任何型態,且大小寫有區分。" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "metadata": { 151 | "colab_type": "code", 152 | "id": "rF-pX6KbIMRL", 153 | "outputId": "3c196431-51d0-474a-8f79-7bca1c3c52ce", 154 | "colab": { 155 | "base_uri": "https://localhost:8080/", 156 | "height": 51 157 | } 158 | }, 159 | "source": [ 160 | "str1='Hello'\n", 161 | "str2='World'\n", 162 | "i=100\n", 163 | "a='a'\n", 164 | "A='A'\n", 165 | "\n", 166 | "print(str1+' '+str2)\n", 167 | "print(a,A)" 168 | ], 169 | "execution_count": 0, 170 | "outputs": [ 171 | { 172 | "output_type": "stream", 173 | "text": [ 174 | "Hello World\n", 175 | "a A\n" 176 | ], 177 | "name": "stdout" 178 | } 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": { 184 | "colab_type": "text", 185 | "id": "ybAqJR3JKXQR" 186 | }, 187 | "source": [ 188 | "* 變數開頭不能為數字,不能使用保留字,ex:if、for、True、in、return..。" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "metadata": { 194 | "colab_type": "code", 195 | "id": "tuAC8WFtKkth", 196 | "outputId": "1cc9b45a-5388-4bb9-b55f-8e53748c41a3", 197 | "colab": { 198 | "base_uri": "https://localhost:8080/", 199 | "height": 131 200 | } 201 | }, 202 | "source": [ 203 | "5=5\n", 204 | "if=2\n", 205 | "for=3" 206 | ], 207 | "execution_count": 0, 208 | "outputs": [ 209 | { 210 | "output_type": "error", 211 | "ename": "SyntaxError", 212 | "evalue": "ignored", 213 | "traceback": [ 214 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m if=2\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 215 | ] 216 | } 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": { 222 | "colab_type": "text", 223 | "id": "TtEknGI82pkG" 224 | }, 225 | "source": [ 226 | "* 以上這些為python內建的變數,所以當你是用這些當做變數時,將不會具有變數的屬性(賦值、運算等等的...)" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": { 232 | "colab_type": "text", 233 | "id": "4n-jQS9-3MDx" 234 | }, 235 | "source": [ 236 | "* 而這些內建的變數,將在後面的章節逐一介紹" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": { 242 | "id": "BXheQaEkxpjk", 243 | "colab_type": "text" 244 | }, 245 | "source": [ 246 | "## 課程文件\n", 247 | "\n", 248 | "#### Python Basics 課程清單\n", 249 | "- Lesson00 Preface 課程前言 (Last Chapter! --上一堂課程--)\n", 250 | "\n", 251 | "- Lesson01 Variable 變數 (We are here now! --本篇課程--)\n", 252 | "\n", 253 | "- Lesson02 Operator 運算子 (Next Chapter! --下一堂課程--)\n", 254 | "\n", 255 | "- Lesson03 Data Structure 資料結構\n", 256 | "\n", 257 | "- Lesson04 String 字串\n", 258 | "\n", 259 | "- Lesson05 List 列表\n", 260 | "\n", 261 | "- Lesson06 Tuple 元組\n", 262 | "\n", 263 | "- Lesson07 Range 範圍\n", 264 | "\n", 265 | "- Lesson08 Set 集合\n", 266 | "\n", 267 | "- Lesson09 Dictionary 字典\n", 268 | "\n", 269 | "- Lesson10 If-else 條件語法\n", 270 | "\n", 271 | "- Lesson11 While 迴圈\n", 272 | "\n", 273 | "- Lesson12 For 迴圈\n", 274 | "\n", 275 | "- Lesson13 Function 函式\n", 276 | "\n", 277 | "- Lesson14 Input/Output 輸入/輸出\n", 278 | "\n", 279 | "- Lesson15 Class 類別" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "metadata": { 285 | "id": "e86vXUUmxpjn", 286 | "colab_type": "code", 287 | "colab": {} 288 | }, 289 | "source": [ 290 | "" 291 | ], 292 | "execution_count": 0, 293 | "outputs": [] 294 | } 295 | ] 296 | } -------------------------------------------------------------------------------- /documents/Lesson02 Operator.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "Bi-PSCAZKjdk" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 02 運算子 (1-02)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "_DeL-QEpKohy" 26 | }, 27 | "source": [ 28 | "# 運算子(Operator)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "RbtpSepcLFaX" 36 | }, 37 | "source": [ 38 | "* 這個章節,我們將教導什麼是運算子,並用一些實例來引導大家了解這個單元" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": { 44 | "colab_type": "text", 45 | "id": "8E9DzMEPKodT" 46 | }, 47 | "source": [ 48 | "* 基本運算" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 1, 54 | "metadata": { 55 | "colab": {}, 56 | "colab_type": "code", 57 | "id": "pPUp6RjVKU_C" 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "a = 1\n", 62 | "b = 2\n", 63 | "c = a + b\n", 64 | "d = a - b\n", 65 | "e = a * b" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": { 71 | "colab_type": "text", 72 | "id": "BTErLy4qLcBC" 73 | }, 74 | "source": [ 75 | "* 並透過print()來觀察以上計算結果" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 4, 81 | "metadata": { 82 | "colab": {}, 83 | "colab_type": "code", 84 | "id": "G68ma_l4LbWV" 85 | }, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "a = 1\n", 92 | "b = 2\n", 93 | "c = 3\n", 94 | "d = -1\n", 95 | "e = 2\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "print(\"a =\",a)\n", 101 | "print(\"b =\",b)\n", 102 | "print(\"c =\",c)\n", 103 | "print(\"d =\",d)\n", 104 | "print(\"e =\",e)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": { 110 | "colab_type": "text", 111 | "id": "5cTk0UziKzjd" 112 | }, 113 | "source": [ 114 | "* 其中常見的運算需要特別注意: / 為直接相除、 // 為取商、 % 為取餘數。" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 8, 120 | "metadata": { 121 | "colab": {}, 122 | "colab_type": "code", 123 | "id": "r28ZmHbjK5bp" 124 | }, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "f = 2.3333333333333335\n", 131 | "g = 2\n", 132 | "h = 1\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "f = 7 / 3\n", 138 | "g = 7 // 3\n", 139 | "h = 7 % 3\n", 140 | "\n", 141 | "#觀察計算結果\n", 142 | "print(\"f =\",f)\n", 143 | "print(\"g =\",g)\n", 144 | "print(\"h =\",h)" 145 | ] 146 | }, 147 | { 148 | "cell_type": "markdown", 149 | "metadata": { 150 | "colab_type": "text", 151 | "id": "h6k1giEoK9gv" 152 | }, 153 | "source": [ 154 | "* 邏輯運算,常作為判別結果用途,回傳的值為 True 或 False。" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 12, 160 | "metadata": { 161 | "colab": {}, 162 | "colab_type": "code", 163 | "id": "KVK9XssqLAr2" 164 | }, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "a = 1 , b = 2\n", 171 | "False (a是否等於b)\n", 172 | "False (a是否大於b)\n", 173 | "True (a是否小於b)\n", 174 | "False (a是否大於等於b)\n", 175 | "True (a是否小於等於b)\n", 176 | "True (a是否不等於b)\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "a == b #a是否等於b\n", 182 | "a > b #a是否大於b\n", 183 | "a < b #a是否小於b\n", 184 | "a >= b #a是否大於等於b\n", 185 | "a <= b #a是否小於等於b\n", 186 | "a != b #a是否不等於b\n", 187 | "\n", 188 | "#觀察判斷結果\n", 189 | "print(\"a =\",a,\", b =\", b)\n", 190 | "print(a == b, \"(a是否等於b)\")\n", 191 | "print(a > b, \"(a是否大於b)\")\n", 192 | "print(a < b, \"(a是否小於b)\")\n", 193 | "print(a >= b, \"(a是否大於等於b)\")\n", 194 | "print(a <= b, \"(a是否小於等於b)\")\n", 195 | "print(a != b, \"(a是否不等於b)\")" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "metadata": {}, 201 | "source": [ 202 | "## 課程文件\n", 203 | "\n", 204 | "#### Python Basics 課程清單\n", 205 | "- Lesson00 Preface 課程前言 \n", 206 | "\n", 207 | "- Lesson01 Variable 變數 (Last Chapter! --上一堂課程--)\n", 208 | "\n", 209 | "- Lesson02 Operator 運算子 (We are here now! --本篇課程--)\n", 210 | "\n", 211 | "- Lesson03 Data Structure 資料結構 (Next Chapter! --下一堂課程--)\n", 212 | "\n", 213 | "- Lesson04 String 字串\n", 214 | "\n", 215 | "- Lesson05 List 列表\n", 216 | "\n", 217 | "- Lesson06 Tuple 元組\n", 218 | "\n", 219 | "- Lesson07 Range 範圍\n", 220 | "\n", 221 | "- Lesson08 Set 集合\n", 222 | "\n", 223 | "- Lesson09 Dictionary 字典\n", 224 | "\n", 225 | "- Lesson10 If-else 條件語法\n", 226 | "\n", 227 | "- Lesson11 While 迴圈\n", 228 | "\n", 229 | "- Lesson12 For 迴圈\n", 230 | "\n", 231 | "- Lesson13 Function 函式\n", 232 | "\n", 233 | "- Lesson14 Input/Output 輸入/輸出\n", 234 | "\n", 235 | "- Lesson15 Class 類別" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [] 244 | } 245 | ], 246 | "metadata": { 247 | "colab": { 248 | "collapsed_sections": [], 249 | "name": "AI . FREE Team - Python Basics: Lesson 02 Operator 運算子.ipynb", 250 | "provenance": [], 251 | "version": "0.3.2" 252 | }, 253 | "kernelspec": { 254 | "display_name": "Python 3", 255 | "language": "python", 256 | "name": "python3" 257 | }, 258 | "language_info": { 259 | "codemirror_mode": { 260 | "name": "ipython", 261 | "version": 3 262 | }, 263 | "file_extension": ".py", 264 | "mimetype": "text/x-python", 265 | "name": "python", 266 | "nbconvert_exporter": "python", 267 | "pygments_lexer": "ipython3", 268 | "version": "3.7.3" 269 | } 270 | }, 271 | "nbformat": 4, 272 | "nbformat_minor": 1 273 | } 274 | -------------------------------------------------------------------------------- /documents/Lesson07 Range.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "S4F7QCIWGiLv" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 07 範圍 (1-07)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "F8f-ETEwOhkA" 26 | }, 27 | "source": [ 28 | "# Ranges ( 範圍 )\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "wgnzZIUAOoLD" 36 | }, 37 | "source": [ 38 | "\n", 39 | "\n", 40 | "* Range 內的元素是不可變的,並且可以使用索引,通常用於迴圈(Loop)\n", 41 | "* 宣告方式為 range( start , stop, step )\n", 42 | "* start : 計數從 start 為起點。如果沒有數字就是從0開始。\n", 43 | "* stop : 計數到 stop 為終點。但不包括 stop 的數字。\n", 44 | "* step : 移動步長,沒有數字就視為 1 。\n" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 16, 50 | "metadata": { 51 | "colab": { 52 | "base_uri": "https://localhost:8080/", 53 | "height": 221 54 | }, 55 | "colab_type": "code", 56 | "executionInfo": { 57 | "elapsed": 841, 58 | "status": "ok", 59 | "timestamp": 1566141675125, 60 | "user": { 61 | "displayName": "電機工程系莊翔宇", 62 | "photoUrl": "", 63 | "userId": "06232090834761520364" 64 | }, 65 | "user_tz": -480 66 | }, 67 | "id": "-MZd6NbXNsf5", 68 | "outputId": "843e604e-9121-4e1f-8f12-dfd99b6f2c90" 69 | }, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "0,1,2,3,4,\n", 76 | "0 ~ 4,但不包括5\n", 77 | "\n", 78 | "3,4,5,6,7,8,9,\n", 79 | "從3開始,但不包括10\n", 80 | "\n", 81 | "3,5,7,9,\n", 82 | "從3開始,但不包括10,每次跳2\n", 83 | "\n", 84 | "可以遞增也可以遞減\n", 85 | "10,5,0,-5,-10,-15,-20,-25,\n", 86 | "從10開始,但不包括-30,每次跳-5\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "for i in range(5):\n", 92 | " print(i,end=\",\")\n", 93 | "print()\n", 94 | "print(\"0 ~ 4,但不包括5\")\n", 95 | "\n", 96 | "print()\n", 97 | "for i in range(3,10):\n", 98 | " print(i,end=\",\")\n", 99 | "print() \n", 100 | "print(\"從3開始,但不包括10\")\n", 101 | "\n", 102 | "print()\n", 103 | "for i in range(3,10,2):\n", 104 | " print(i,end=\",\")\n", 105 | "print() \n", 106 | "print(\"從3開始,但不包括10,每次跳2\")\n", 107 | "\n", 108 | "print()\n", 109 | "print(\"可以遞增也可以遞減\")\n", 110 | "for i in range(10,-30,-5):\n", 111 | " print(i,end=\",\")\n", 112 | "print() \n", 113 | "print(\"從10開始,但不包括-30,每次跳-5\")" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": { 119 | "colab_type": "text", 120 | "id": "xToljETJSwbX" 121 | }, 122 | "source": [ 123 | "* 來看看其他用法" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 11, 129 | "metadata": { 130 | "colab": { 131 | "base_uri": "https://localhost:8080/", 132 | "height": 119 133 | }, 134 | "colab_type": "code", 135 | "executionInfo": { 136 | "elapsed": 630, 137 | "status": "ok", 138 | "timestamp": 1566141387574, 139 | "user": { 140 | "displayName": "電機工程系莊翔宇", 141 | "photoUrl": "", 142 | "userId": "06232090834761520364" 143 | }, 144 | "user_tz": -480 145 | }, 146 | "id": "P0L0yXFNQ9Hj", 147 | "outputId": "a6307d57-76f7-49e4-9027-0aea99d564a8" 148 | }, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "[[0, 'jane']]\n", 155 | "[[0, 'jane'], [1, 'john']]\n", 156 | "[[0, 'jane'], [1, 'john'], [2, 'mark']]\n", 157 | "[[0, 'jane'], [1, 'john'], [2, 'mark'], [3, 'harry']]\n", 158 | "[[0, 'jane'], [1, 'john'], [2, 'mark'], [3, 'harry'], [4, 'mike']]\n", 159 | "[[0, 'jane'], [1, 'john'], [2, 'mark'], [3, 'harry'], [4, 'mike'], [5, 'ed']]\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "words = ['jane','john','mark','harry','mike','ed']\n", 165 | "wordlist = []\n", 166 | "for i in range(len(words)):\n", 167 | " wordlist.append([i,words[i]])\n", 168 | " print(wordlist)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": { 174 | "colab_type": "text", 175 | "id": "qc2eXly9TXk7" 176 | }, 177 | "source": [ 178 | "* 也可以把 range 宣告成變數" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 15, 184 | "metadata": { 185 | "colab": { 186 | "base_uri": "https://localhost:8080/", 187 | "height": 136 188 | }, 189 | "colab_type": "code", 190 | "executionInfo": { 191 | "elapsed": 695, 192 | "status": "ok", 193 | "timestamp": 1566141598136, 194 | "user": { 195 | "displayName": "電機工程系莊翔宇", 196 | "photoUrl": "", 197 | "userId": "06232090834761520364" 198 | }, 199 | "user_tz": -480 200 | }, 201 | "id": "ckJcY8e0TRBu", 202 | "outputId": "23af5576-7ea9-4f20-e4b0-fe648249c523" 203 | }, 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "range(0, 10, 2)\n", 210 | "[0, 2, 4, 6, 8]\n", 211 | "True\n", 212 | "1\n", 213 | "6\n", 214 | "range(0, 6, 2)\n", 215 | "8\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "r = range(0,10,2)\n", 221 | "print(r)\n", 222 | "print(list(r))\n", 223 | "print(2 in r)\n", 224 | "print(r.index(2))\n", 225 | "print(r[3])\n", 226 | "print(r[:3])\n", 227 | "print(r[-1])" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": { 233 | "colab": {}, 234 | "colab_type": "code", 235 | "id": "2ZLG-WJVTfOZ" 236 | }, 237 | "source": [ 238 | "## 課程文件\n", 239 | "\n", 240 | "#### Python Basics 課程清單\n", 241 | "- Lesson00 Preface 課程前言 \n", 242 | "\n", 243 | "- Lesson01 Variable 變數 \n", 244 | "\n", 245 | "- Lesson02 Operator 運算子\n", 246 | "\n", 247 | "- Lesson03 Data Structure 資料結構 \n", 248 | "\n", 249 | "- Lesson04 String 字串\n", 250 | "\n", 251 | "- Lesson05 List 列表 \n", 252 | "\n", 253 | "- Lesson06 Tuple 元組 (Last Chapter! --上一堂課程--)\n", 254 | "\n", 255 | "- Lesson07 Range 範圍 (We are here now! --本篇課程--)\n", 256 | "\n", 257 | "- Lesson08 Set 集合 (Next Chapter! --下一堂課程--)\n", 258 | "\n", 259 | "- Lesson09 Dictionary 字典\n", 260 | "\n", 261 | "- Lesson10 If-else 條件語法\n", 262 | "\n", 263 | "- Lesson11 While 迴圈\n", 264 | "\n", 265 | "- Lesson12 For 迴圈\n", 266 | "\n", 267 | "- Lesson13 Function 函式\n", 268 | "\n", 269 | "- Lesson14 Input/Output 輸入/輸出\n", 270 | "\n", 271 | "- Lesson15 Class 類別" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [] 280 | } 281 | ], 282 | "metadata": { 283 | "colab": { 284 | "collapsed_sections": [], 285 | "name": "AI . FREE Team - Python Basics: Lesson 07 Range 範圍.ipynb", 286 | "provenance": [], 287 | "version": "0.3.2" 288 | }, 289 | "kernelspec": { 290 | "display_name": "Python 3", 291 | "language": "python", 292 | "name": "python3" 293 | }, 294 | "language_info": { 295 | "codemirror_mode": { 296 | "name": "ipython", 297 | "version": 3 298 | }, 299 | "file_extension": ".py", 300 | "mimetype": "text/x-python", 301 | "name": "python", 302 | "nbconvert_exporter": "python", 303 | "pygments_lexer": "ipython3", 304 | "version": "3.7.3" 305 | } 306 | }, 307 | "nbformat": 4, 308 | "nbformat_minor": 1 309 | } 310 | -------------------------------------------------------------------------------- /documents/Lesson08 Set.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "eIYX18qMb_Ug" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 08 範圍 (1-08)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
\n" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "g_qwNxi1cBz6" 26 | }, 27 | "source": [ 28 | "# Set (集合)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "zh8NQDatdJ9t" 36 | }, 37 | "source": [ 38 | "* set() 函數創建一個無序不重複元素集,可進行關係測試,刪除重複數據,還可以計算交集、差集、並集等。" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 0, 44 | "metadata": { 45 | "colab": {}, 46 | "colab_type": "code", 47 | "id": "4pNYZD3jbj8p" 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "# class set([iterable])\n", 52 | "# iterable -- 可迭代對象對象;" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": { 58 | "colab_type": "text", 59 | "id": "cPj-rQaVg23D" 60 | }, 61 | "source": [ 62 | "## 實例" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": { 68 | "colab_type": "text", 69 | "id": "unbQorm4eufQ" 70 | }, 71 | "source": [ 72 | "* 以下實例展示了 set 的使用方法:" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 0, 78 | "metadata": { 79 | "colab": { 80 | "base_uri": "https://localhost:8080/", 81 | "height": 102 82 | }, 83 | "colab_type": "code", 84 | "executionInfo": { 85 | "elapsed": 649, 86 | "status": "ok", 87 | "timestamp": 1566145137680, 88 | "user": { 89 | "displayName": "電機工程系李佑謙", 90 | "photoUrl": "", 91 | "userId": "05735385076343474033" 92 | }, 93 | "user_tz": -480 94 | }, 95 | "id": "wtt9P1TehM6_", 96 | "outputId": "afd4b6e9-d0fe-46f0-d6b4-a68b5b84f91c" 97 | }, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 104 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 105 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 106 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n", 107 | "{'b', 'o', 'u', 'n', 'r'} {'e', 'l', 'g', 'o'}\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "x = set('runoob')\n", 113 | "y = set('google')\n", 114 | "print(x,y)\n", 115 | "\n", 116 | "x, y\n", 117 | "(set(['b','r','u','o','n']), set(['e','o','g','l'])) # 重複的被刪除\n", 118 | "print(x,y)\n", 119 | "\n", 120 | "x & y # 交集\n", 121 | "set(['o'])\n", 122 | "print(x,y)\n", 123 | "\n", 124 | "x | y # 並集\n", 125 | "set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u'])\n", 126 | "print(x,y)\n", 127 | "\n", 128 | "x - y # 差集\n", 129 | "set(['r','b','u','n'])\n", 130 | "print(x,y)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": { 136 | "colab_type": "text", 137 | "id": "glPs_dsjhLtx" 138 | }, 139 | "source": [ 140 | "## 課程文件\n", 141 | "\n", 142 | "#### Python Basics 課程清單\n", 143 | "- Lesson00 Preface 課程前言 \n", 144 | "\n", 145 | "- Lesson01 Variable 變數 \n", 146 | "\n", 147 | "- Lesson02 Operator 運算子\n", 148 | "\n", 149 | "- Lesson03 Data Structure 資料結構 \n", 150 | "\n", 151 | "- Lesson04 String 字串\n", 152 | "\n", 153 | "- Lesson05 List 列表 \n", 154 | "\n", 155 | "- Lesson06 Tuple 元組\n", 156 | "\n", 157 | "- Lesson07 Range 範圍 (Last Chapter! --上一堂課程--)\n", 158 | "\n", 159 | "- Lesson08 Set 集合 (We are here now! --本篇課程--)\n", 160 | "\n", 161 | "- Lesson09 Dictionary 字典 (Next Chapter! --下一堂課程--)\n", 162 | "\n", 163 | "- Lesson10 If-else 條件語法\n", 164 | "\n", 165 | "- Lesson11 While 迴圈\n", 166 | "\n", 167 | "- Lesson12 For 迴圈\n", 168 | "\n", 169 | "- Lesson13 Function 函式\n", 170 | "\n", 171 | "- Lesson14 Input/Output 輸入/輸出\n", 172 | "\n", 173 | "- Lesson15 Class 類別" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [] 182 | } 183 | ], 184 | "metadata": { 185 | "colab": { 186 | "collapsed_sections": [], 187 | "name": "AI . FREE Team - Python Basics: Lesson 08 Set 集合.ipynb", 188 | "provenance": [], 189 | "version": "0.3.2" 190 | }, 191 | "kernelspec": { 192 | "display_name": "Python 3", 193 | "language": "python", 194 | "name": "python3" 195 | }, 196 | "language_info": { 197 | "codemirror_mode": { 198 | "name": "ipython", 199 | "version": 3 200 | }, 201 | "file_extension": ".py", 202 | "mimetype": "text/x-python", 203 | "name": "python", 204 | "nbconvert_exporter": "python", 205 | "pygments_lexer": "ipython3", 206 | "version": "3.7.3" 207 | } 208 | }, 209 | "nbformat": 4, 210 | "nbformat_minor": 1 211 | } 212 | -------------------------------------------------------------------------------- /documents/Lesson10 If-else.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "N177jyz4StvH" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 10 條件語法 (1-10)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "-F8EhN_TTmxn" 26 | }, 27 | "source": [ 28 | "# If else(條件語法)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "MB7lAcJETyza" 36 | }, 37 | "source": [ 38 | "Python條件語法是通過一條或多條語句的執行結果(True或者False)來決定執行的代碼。\n", 39 | "\n", 40 | "可以通過下圖來簡單瞭解條件語句的執行過程:" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": { 46 | "colab_type": "text", 47 | "id": "YJl_C-y3T7i5" 48 | }, 49 | "source": [ 50 | "![替代文字](https://www.tutorialspoint.com/python/images/if_else_statement.jpg)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": { 56 | "colab_type": "text", 57 | "id": "DFjlhBS1UGPa" 58 | }, 59 | "source": [ 60 | "Python程序語言指定任何非0和非空(null)值爲true,0 或者 null爲false。\n", 61 | "\n", 62 | "Python 編程中 if 語句用於控制程序的執行,基本形式爲:\n", 63 | "\n", 64 | "\n", 65 | "```\n", 66 | "if 判斷條件:\n", 67 | " 執行語句……\n", 68 | "else:\n", 69 | " 執行語句……\n", 70 | "```\n", 71 | "\n" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": { 77 | "colab_type": "text", 78 | "id": "qnJcDIIeVU13" 79 | }, 80 | "source": [ 81 | "其中\"判斷條件\"成立時(非零),則執行後面的語句,而執行內容可以多行,以同樣的縮排來區分表示同一範圍。\n", 82 | "\n", 83 | "else 爲可選語句,當上述條件不成立時執行內容則可以執行相關語句。" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 2, 89 | "metadata": { 90 | "colab": { 91 | "base_uri": "https://localhost:8080/", 92 | "height": 51 93 | }, 94 | "colab_type": "code", 95 | "executionInfo": { 96 | "elapsed": 656, 97 | "status": "ok", 98 | "timestamp": 1567266304265, 99 | "user": { 100 | "displayName": "電機工程系李佑謙", 101 | "photoUrl": "", 102 | "userId": "05735385076343474033" 103 | }, 104 | "user_tz": -480 105 | }, 106 | "id": "BSWF8OPpSZu-", 107 | "outputId": "771c3e01-d57e-42dc-e717-5b11210bce7b" 108 | }, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "hi!\n", 115 | "None\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "x = 1\n", 121 | "if x == 1: # 判斷變量是否爲 1\n", 122 | " print('hi!') # 並輸出信息\n", 123 | " \n", 124 | "else:\n", 125 | " print('None')\n", 126 | "\n", 127 | "x = 2\n", 128 | "if x == 1: \n", 129 | " print('hi!') \n", 130 | "else:\n", 131 | " print('None')" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": { 137 | "colab_type": "text", 138 | "id": "cl0STcCEWBsW" 139 | }, 140 | "source": [ 141 | "if 語句的判斷條件可以用>(大於)、<(小於)、==(等於)、>=(大於等於)、<=(小於等於)來表示其關係。\n", 142 | "\n", 143 | "當判斷條件爲多個值時,可以使用以下形式:\n", 144 | "\n", 145 | "\n", 146 | "```\n", 147 | "if 判斷條件1:\n", 148 | " 執行語句1……\n", 149 | "elif 判斷條件2:\n", 150 | " 執行語句2……\n", 151 | "elif 判斷條件3:\n", 152 | " 執行語句3……\n", 153 | "else:\n", 154 | " 執行語句4……\n", 155 | "```\n", 156 | "\n" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 3, 162 | "metadata": { 163 | "colab": { 164 | "base_uri": "https://localhost:8080/", 165 | "height": 34 166 | }, 167 | "colab_type": "code", 168 | "executionInfo": { 169 | "elapsed": 699, 170 | "status": "ok", 171 | "timestamp": 1567266437408, 172 | "user": { 173 | "displayName": "電機工程系李佑謙", 174 | "photoUrl": "", 175 | "userId": "05735385076343474033" 176 | }, 177 | "user_tz": -480 178 | }, 179 | "id": "4cfe0V-tWIWv", 180 | "outputId": "8a97ee29-db26-48e8-bf99-9963271e5205" 181 | }, 182 | "outputs": [ 183 | { 184 | "name": "stdout", 185 | "output_type": "stream", 186 | "text": [ 187 | "twenty-one\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "number = 21 \n", 193 | "if number == 18: # 判斷number的值\n", 194 | " print('eighteen') \n", 195 | "elif number == 21:\n", 196 | " print('twenty-one') \n", 197 | "elif number == 1:\n", 198 | " print('one') \n", 199 | "else:\n", 200 | " print('not') # 條件均不成立時輸出" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": { 206 | "colab_type": "text", 207 | "id": "ERnTmg27XKUO" 208 | }, 209 | "source": [ 210 | "由於 python 並不支持 switch 語句,所以多個條件判斷,只能用 elif 來實現,如果判斷需要多個條件需同時判斷時,可以使用 or (或),表示兩個條件有一個成立時判斷條件成功;使用 and (與)時,表示只有兩個條件同時成立的情況下,判斷條件才成功。" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 4, 216 | "metadata": { 217 | "colab": { 218 | "base_uri": "https://localhost:8080/", 219 | "height": 68 220 | }, 221 | "colab_type": "code", 222 | "executionInfo": { 223 | "elapsed": 677, 224 | "status": "ok", 225 | "timestamp": 1567266766421, 226 | "user": { 227 | "displayName": "電機工程系李佑謙", 228 | "photoUrl": "", 229 | "userId": "05735385076343474033" 230 | }, 231 | "user_tz": -480 232 | }, 233 | "id": "AFWeJ8-_XJmQ", 234 | "outputId": "86022692-15e7-4da3-b4bc-a584dce34c33" 235 | }, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "between 0 and 10\n", 242 | "undefine\n", 243 | "undefine\n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "num = 9\n", 249 | "if num >= 0 and num <= 10: # 判斷值是否在0~10之間\n", 250 | " print('between 0 and 10')\n", 251 | "# 輸出結果:between 0 and 10\n", 252 | " \n", 253 | "num = 10\n", 254 | "if num < 0 or num > 10: # 判斷值是否在小於0或大於10\n", 255 | " print('less than 0 or more than 10')\n", 256 | "else:\n", 257 | " print('undefine')\n", 258 | "# 輸出結果: undefine\n", 259 | " \n", 260 | "num = 8\n", 261 | "# 判斷值是否在0~5或者10~15之間\n", 262 | "if (num >= 0 and num <= 5) or (num >= 10 and num <= 15): \n", 263 | " print('(more or equal 0 and less or equal than 5) or (more or equal 10 and less or equal than 15)')\n", 264 | "else:\n", 265 | " print('undefine')\n", 266 | "# 輸出結果: undefine" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": { 272 | "colab_type": "text", 273 | "id": "7VtH-IM0YQgP" 274 | }, 275 | "source": [ 276 | "當if有多個條件時可使用括號來區分判斷的先後順序,括號中的判斷優先執行,此外 and 和 or 的優先級低於>(大於)、<(小於)等判斷符號,即大於和小於在沒有括號的情況下會比與或要優先判斷。" 277 | ] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "metadata": { 282 | "colab_type": "text", 283 | "id": "ju1S-GY7YTwD" 284 | }, 285 | "source": [ 286 | "## 簡單的語句組" 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "metadata": { 292 | "colab_type": "text", 293 | "id": "1Yti37DdYXg8" 294 | }, 295 | "source": [ 296 | "你也可以在同一行的位置上使用if條件判斷語句,如下實例:" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 6, 302 | "metadata": { 303 | "colab": { 304 | "base_uri": "https://localhost:8080/", 305 | "height": 51 306 | }, 307 | "colab_type": "code", 308 | "executionInfo": { 309 | "elapsed": 629, 310 | "status": "ok", 311 | "timestamp": 1567266894695, 312 | "user": { 313 | "displayName": "電機工程系李佑謙", 314 | "photoUrl": "", 315 | "userId": "05735385076343474033" 316 | }, 317 | "user_tz": -480 318 | }, 319 | "id": "AaOsVZ9NYkTl", 320 | "outputId": "30f5e1a9-3dc9-42ce-cef2-b5c499520c31" 321 | }, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "變量 var 的值爲50\n", 328 | "Good bye!\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "var = 50 \n", 334 | " \n", 335 | "if ( var == 50 ) : print(\"變量 var 的值爲50\") \n", 336 | " \n", 337 | "print(\"Good bye!\")" 338 | ] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "metadata": {}, 343 | "source": [ 344 | "## 課程文件\n", 345 | "\n", 346 | "#### Python Basics 課程清單\n", 347 | "- Lesson00 Preface 課程前言 \n", 348 | "\n", 349 | "- Lesson01 Variable 變數 \n", 350 | "\n", 351 | "- Lesson02 Operator 運算子\n", 352 | "\n", 353 | "- Lesson03 Data Structure 資料結構 \n", 354 | "\n", 355 | "- Lesson04 String 字串\n", 356 | "\n", 357 | "- Lesson05 List 列表 \n", 358 | "\n", 359 | "- Lesson06 Tuple 元組\n", 360 | "\n", 361 | "- Lesson07 Range 範圍\n", 362 | "\n", 363 | "- Lesson08 Set 集合\n", 364 | "\n", 365 | "- Lesson09 Dictionary 字典 (Last Chapter! --上一堂課程--)\n", 366 | "\n", 367 | "- Lesson10 If-else 條件語法 (We are here now! --本篇課程--)\n", 368 | "\n", 369 | "- Lesson11 While 迴圈 (Next Chapter! --下一堂課程--)\n", 370 | "\n", 371 | "- Lesson12 For 迴圈 \n", 372 | "\n", 373 | "- Lesson13 Function 函式\n", 374 | "\n", 375 | "- Lesson14 Input/Output 輸入/輸出\n", 376 | "\n", 377 | "- Lesson15 Class 類別" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": null, 383 | "metadata": {}, 384 | "outputs": [], 385 | "source": [] 386 | } 387 | ], 388 | "metadata": { 389 | "colab": { 390 | "collapsed_sections": [], 391 | "name": "AI . FREE Team - Python Basics: Lesson 10 If-else 條件語法.ipynb", 392 | "provenance": [], 393 | "version": "0.3.2" 394 | }, 395 | "kernelspec": { 396 | "display_name": "Python 3", 397 | "language": "python", 398 | "name": "python3" 399 | }, 400 | "language_info": { 401 | "codemirror_mode": { 402 | "name": "ipython", 403 | "version": 3 404 | }, 405 | "file_extension": ".py", 406 | "mimetype": "text/x-python", 407 | "name": "python", 408 | "nbconvert_exporter": "python", 409 | "pygments_lexer": "ipython3", 410 | "version": "3.7.3" 411 | } 412 | }, 413 | "nbformat": 4, 414 | "nbformat_minor": 1 415 | } 416 | -------------------------------------------------------------------------------- /documents/Lesson12 For-loop.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "rGCLGcuEPVSa" 8 | }, 9 | "source": [ 10 | "![人工智慧 - 自由團隊](https://raw.githubusercontent.com/chenkenanalytic/img/master/af/aifreeteam.png)\n", 11 | "\n", 12 | "\n", 13 | "
Welcome to the course《Python: from business analytics to Artificial Intelligence》by AI . FREE Team.
\n", 14 | "
\n", 15 | "
歡迎大家來到 AI . FREE Team 《Python 從商業分析到人工智慧》的 Python 基礎教學 - Lesson 12 For 迴圈 (1-12)。
\n", 16 | "
\n", 17 | "\n", 18 | "
(Author: Chen Ken, Lee Ovien, Chuang Shawn;Date of published: 2019/9/5;AI . FREE Team Website: https://aifreeblog.herokuapp.com/)
" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text", 25 | "id": "EmvrmGKYPZz3" 26 | }, 27 | "source": [ 28 | "# For迴圈" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": { 34 | "colab_type": "text", 35 | "id": "jKOZ696hFiiu" 36 | }, 37 | "source": [ 38 | "* 就像while迴圈一樣 for循環可以遍歷任何序列的項目,如一個列表或者一個字符串。" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": { 44 | "colab_type": "text", 45 | "id": "FHG_5fQXH1RZ" 46 | }, 47 | "source": [ 48 | "* for循環的語法格式如下:" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 0, 54 | "metadata": { 55 | "colab": {}, 56 | "colab_type": "code", 57 | "id": "QZPQ2d5UPR-F" 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "#for iterating_var in sequence:\n", 62 | "# statements(s)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": { 68 | "colab_type": "text", 69 | "id": "E6dHdcF1IAv7" 70 | }, 71 | "source": [ 72 | "![替代文字](http://www.runoob.com/wp-content/uploads/2013/11/python_for_loop.jpg)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 0, 78 | "metadata": { 79 | "colab": { 80 | "base_uri": "https://localhost:8080/", 81 | "height": 221 82 | }, 83 | "colab_type": "code", 84 | "executionInfo": { 85 | "elapsed": 893, 86 | "status": "ok", 87 | "timestamp": 1566658988248, 88 | "user": { 89 | "displayName": "電機工程系李佑謙", 90 | "photoUrl": "", 91 | "userId": "05735385076343474033" 92 | }, 93 | "user_tz": -480 94 | }, 95 | "id": "C5da9vhyIbB4", 96 | "outputId": "73652b42-db7e-4861-f205-4be6209dbe60" 97 | }, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "當前字母 : N\n", 104 | "當前字母 : e\n", 105 | "當前字母 : w\n", 106 | "當前字母 : \n", 107 | "當前字母 : Y\n", 108 | "當前字母 : o\n", 109 | "當前字母 : r\n", 110 | "當前字母 : k\n", 111 | "當前水果 : Detroit\n", 112 | "當前水果 : Arizona\n", 113 | "當前水果 : Georgia\n", 114 | "finish\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "for ex in 'New York': # 第一個實例\n", 120 | " print('當前字母 :',ex)\n", 121 | " \n", 122 | "fruits = ['Detroit','Arizona','Georgia']\n", 123 | "for fruit in fruits: # 第二個實例\n", 124 | " print('當前地名 :',fruit)\n", 125 | " \n", 126 | "print(\"finish\")" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": { 132 | "colab_type": "text", 133 | "id": "9JigJ0O_J6n_" 134 | }, 135 | "source": [ 136 | "## 通過序列索引迭代" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": { 142 | "colab_type": "text", 143 | "id": "b2Zg4xH_KAQd" 144 | }, 145 | "source": [ 146 | "* 另外一種執行循環的遍歷方式是通過索引,如下實例:" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 0, 152 | "metadata": { 153 | "colab": { 154 | "base_uri": "https://localhost:8080/", 155 | "height": 85 156 | }, 157 | "colab_type": "code", 158 | "executionInfo": { 159 | "elapsed": 678, 160 | "status": "ok", 161 | "timestamp": 1566659431765, 162 | "user": { 163 | "displayName": "電機工程系李佑謙", 164 | "photoUrl": "", 165 | "userId": "05735385076343474033" 166 | }, 167 | "user_tz": -480 168 | }, 169 | "id": "me4EMzYkIgjh", 170 | "outputId": "d061ca19-60cd-4b88-ca93-a17b1e9143e5" 171 | }, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "當前國家 : New Jersey\n", 178 | "當前國家 : Nevada\n", 179 | "當前國家 : Montana\n", 180 | "finish!\n" 181 | ] 182 | } 183 | ], 184 | "source": [ 185 | "state = ['New Jersey', 'Nevada', 'Montana']\n", 186 | "for index in range(len(state)):\n", 187 | " print('當前國家 :', state[index])\n", 188 | " \n", 189 | "print(\"finish!\")" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": { 195 | "colab_type": "text", 196 | "id": "skzu2KQtIFof" 197 | }, 198 | "source": [ 199 | "* 以上實例我們使用了內置函數 len() 和 range(),函數 len() 返回列表的長度,即元素的個數。 range返回一個序列的數。" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": { 205 | "colab_type": "text", 206 | "id": "uiOgQauwLnXY" 207 | }, 208 | "source": [ 209 | "## 循環使用 else 語句" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": { 215 | "colab_type": "text", 216 | "id": "AYXO0UP1Lsdy" 217 | }, 218 | "source": [ 219 | "* 在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是通過 break 跳出而中斷的)的情況下執行,while … else 也是一樣。" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 0, 225 | "metadata": { 226 | "colab": { 227 | "base_uri": "https://localhost:8080/", 228 | "height": 357 229 | }, 230 | "colab_type": "code", 231 | "executionInfo": { 232 | "elapsed": 674, 233 | "status": "ok", 234 | "timestamp": 1566659661038, 235 | "user": { 236 | "displayName": "電機工程系李佑謙", 237 | "photoUrl": "", 238 | "userId": "05735385076343474033" 239 | }, 240 | "user_tz": -480 241 | }, 242 | "id": "hu7ybwNSL130", 243 | "outputId": "53336914-d848-4923-cdac-adf5b3e48fe3" 244 | }, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "0 是一個質數\n", 251 | "1 是一個質數\n", 252 | "2 是一個質數\n", 253 | "3 是一個質數\n", 254 | "4 等於 2 * 2\n", 255 | "5 是一個質數\n", 256 | "6 等於 2 * 3\n", 257 | "7 是一個質數\n", 258 | "8 等於 2 * 4\n", 259 | "9 等於 3 * 3\n", 260 | "10 等於 2 * 5\n", 261 | "11 是一個質數\n", 262 | "12 等於 2 * 6\n", 263 | "13 是一個質數\n", 264 | "14 等於 2 * 7\n", 265 | "15 等於 3 * 5\n", 266 | "16 等於 2 * 8\n", 267 | "17 是一個質數\n", 268 | "18 等於 2 * 9\n", 269 | "19 是一個質數\n" 270 | ] 271 | } 272 | ], 273 | "source": [ 274 | "for num in range(0,20): # 迭代 10 到 20 之間的數字\n", 275 | " for i in range(2,num): # 根據因子迭代\n", 276 | " if num%i == 0: # 確定第一個因子\n", 277 | " j=num/i # 計算第二個因子\n", 278 | " print('%d 等於 %d * %d' % (num,i,j))\n", 279 | " break # 跳出當前循環\n", 280 | " else: # 循環的 else 部分\n", 281 | " print(num, '是一個質數')" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": { 287 | "colab_type": "text", 288 | "id": "nqDzLTZ4MVln" 289 | }, 290 | "source": [ 291 | "## 課程文件\n", 292 | "\n", 293 | "#### Python Basics 課程清單\n", 294 | "- Lesson00 Preface 課程前言 \n", 295 | "\n", 296 | "- Lesson01 Variable 變數 \n", 297 | "\n", 298 | "- Lesson02 Operator 運算子\n", 299 | "\n", 300 | "- Lesson03 Data Structure 資料結構 \n", 301 | "\n", 302 | "- Lesson04 String 字串\n", 303 | "\n", 304 | "- Lesson05 List 列表 \n", 305 | "\n", 306 | "- Lesson06 Tuple 元組 \n", 307 | "\n", 308 | "- Lesson07 Range 範圍 \n", 309 | "\n", 310 | "- Lesson08 Set 集合 \n", 311 | "\n", 312 | "- Lesson09 Dictionary 字典\n", 313 | "\n", 314 | "- Lesson10 If-else 條件語法\n", 315 | "\n", 316 | "- Lesson11 While 迴圈 (Last Chapter! --上一堂課程--)\n", 317 | "\n", 318 | "- Lesson12 For 迴圈 (We are here now! --本篇課程--)\n", 319 | "\n", 320 | "- Lesson13 Function 函式 (Next Chapter! --下一堂課程--)\n", 321 | "\n", 322 | "- Lesson14 Input/Output 輸入/輸出\n", 323 | "\n", 324 | "- Lesson15 Class 類別" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": null, 330 | "metadata": {}, 331 | "outputs": [], 332 | "source": [] 333 | } 334 | ], 335 | "metadata": { 336 | "colab": { 337 | "collapsed_sections": [], 338 | "name": "AI . FREE Team - Python Basics: Lesson 12 For 迴圈.ipynb", 339 | "provenance": [], 340 | "version": "0.3.2" 341 | }, 342 | "kernelspec": { 343 | "display_name": "Python 3", 344 | "language": "python", 345 | "name": "python3" 346 | }, 347 | "language_info": { 348 | "codemirror_mode": { 349 | "name": "ipython", 350 | "version": 3 351 | }, 352 | "file_extension": ".py", 353 | "mimetype": "text/x-python", 354 | "name": "python", 355 | "nbconvert_exporter": "python", 356 | "pygments_lexer": "ipython3", 357 | "version": "3.7.3" 358 | } 359 | }, 360 | "nbformat": 4, 361 | "nbformat_minor": 1 362 | } 363 | --------------------------------------------------------------------------------