{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# [CodingX] Lesson 02 -Python_Basic(I)\n",
    "## by Kun-Ta Chuang\n",
    " Advanced Network Database Laboratory <br>\n",
    " Dept. of Computer Science and Information Engineering <br>\n",
    " National Cheng Kung University "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Why Python ?\n",
    "![](https://imgur.com/RMNaBrW.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 適合初學者\n",
    "### 親民\n",
    "* 可以快速的做出成品\n",
    "* 注重了解語言特性和非底層細節\n",
    "* 養成良好的編碼風格\n",
    "* 上手簡單,又不失嚴謹\n",
    "* 社群資源\n",
    "\n",
    "`print(\"Hello, world!\")`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 做什麼?\n",
    "### * 資料挖掘、分析\n",
    "### * 科學運算\n",
    "### * 機器學習\n",
    "### * Web 網頁\n",
    "### * GUI\n",
    "### * 遊戲\n",
    "### ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 爬蟲實例\n",
    "![](https://imgur.com/wPD8irn.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "##  優點\n",
    "* 簡單、易學\n",
    "* 語法簡潔\n",
    "* 生態成熟、豐富強大的擴充庫\n",
    "* 開發效率高\n",
    "* 應用領域廣泛\n",
    "* 適用於快速修正回饋式的服務開發(Growth Hacking)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "##  缺點\n",
    "* 開發過程過度使用高階封裝式套件,常知其然不知其所以然\n",
    "* AI工程師 vs. API工程師\n",
    "* 直譯式語言在執行速度優化較困難\n",
    "* Python 2&3 不相容"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 氣宗 VS. 劍宗\n",
    "![](https://i.imgur.com/c28fwtN.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 寫python時遇到問題怎麼辦?\n",
    "1. python doc\n",
    "2. stackoverflow\n",
    "3. google\n",
    "4. 同儕、老師、助教等等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# [python doc](https://docs.python.org/3/)\n",
    "* 安裝問題\n",
    "* 教學\n",
    "* 標準函式庫\n",
    "* 更新模組\n",
    "* 等等....."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# [python doc](https://docs.python.org/3/) (cont'd)\n",
    "![](https://i.imgur.com/ITGIfI7.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# [stackoverflow](https://stackoverflow.com/)\n",
    "![](https://i.imgur.com/oDRzt5I.jpg)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## [stackoverflow](https://stackoverflow.com/) (cont'd)\n",
    "\n",
    "### 遇到 errors、bugs\n",
    "![](https://i.imgur.com/WtXaX0b.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# [stackoverflow](https://stackoverflow.com/) (cont'd)\n",
    "\n",
    "* 找工作\n",
    "![](https://i.imgur.com/6ctFqeF.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# [Google](https://www.google.com)\n",
    "\n",
    "![](https://i.imgur.com/iKoqpPh.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# 同儕\n",
    "## [提問的藝術](https://ryanhanwu.gitbooks.io/how-to-ask-questions-the-smart-way/content/)\n",
    "![](https://i.imgur.com/r5yIVEC.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 今天會上什麼呢?\n",
    "1. object (物件)\n",
    "2. Primitive Datatype (基本型別)\n",
    "3. operator and operand (運算子與運算元)\n",
    "4. variable (變數)\n",
    "5. Common Operation\n",
    "6. IO (input、print)\n",
    "7. Condition 條件句(if ... elif ... else)\n",
    "8. comment 註解(#...)\n",
    "\n",
    "附錄\n",
    "a. 命名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 1.物件(object)\n",
    "* python中 每一個東西都是一個物件\n",
    "* 物件可以被理解為一種容器\n",
    "* 每個物件有屬於自己的型別(type)與值(value)\n",
    "* 在記憶體中佔有空間\n",
    "![](https://i.imgur.com/pdgvc4L.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "123"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "123"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(123)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello World!'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Hello World!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"Hello World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 2. 基本型別 (Primitive Data Types)\n",
    "\n",
    "\n",
    "![](https://i.imgur.com/jgV4HUa.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 3. 運算元與運算子\n",
    "## 運算元(operand):即物件(int、float...)\n",
    "## 運算子(operator)\n",
    "* 數學運算子\n",
    "* 布林運算子(又稱邏輯運算子)\n",
    "* 比較運算子\n",
    "* 位元運算子\n",
    "* 賦值運算子"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 數學運算子\n",
    "![](https://i.imgur.com/kD3JGSz.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 比較運算子\n",
    "![](https://i.imgur.com/CDunWsU.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "## == : 判斷左右兩邊的值是不是一樣\n",
    "## != :  !為not的意思  != 判斷左右兩邊的值是不是不一樣"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n",
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(10 > -1.5)\n",
    "print(10 < -1.5)\n",
    "print(10 == -1.5)\n",
    "print(10 >= -1.5)\n",
    "print(10 <= -1.5)\n",
    "print(10 != -1.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 布林運算子(又稱邏輯運算子)\n",
    "![](https://i.imgur.com/5DnD9hs.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1+1 > 2) or (2 > 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not(False) and (3 > 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 賦值運算子\n",
    "![](https://i.imgur.com/pNC3lCN.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# = Assign\n",
    "## 其意義不是我們數學符號中的等於"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "a = 2\n",
    "a **= 2  # a = a**2\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# 運算子優先順序\n",
    "![](https://i.imgur.com/DhysPNH.png)\n",
    "## reference : [unary operator](https://zh.wikipedia.org/wiki/%E4%B8%80%E5%85%83%E9%81%8B%E7%AE%97)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15.0\n"
     ]
    }
   ],
   "source": [
    "a = 3 + 10/5*2*(1+2)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 4.變數(variable)\n",
    "* 不用宣告型別(int、float)\n",
    "* 為物件命名(不能為保留字)\n",
    "* 記憶體位址為物件的記憶體位址"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "* 不用宣告型別\n",
    "![](https://i.imgur.com/W1xYG59.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "<class 'int'>\n",
      "Hello\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "print(a)\n",
    "print(type(a))\n",
    "\n",
    "b = \"Hello\"\n",
    "print(b)\n",
    "print(type(b))\n",
    "\n",
    "a = 1\n",
    "a = 2\n",
    "#1 = 2 # can notassign value 2 to value 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "8\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "a = 8\n",
    "print(a)\n",
    "\n",
    "b = a\n",
    "print(b)\n",
    "\n",
    "a = 7\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![](https://i.imgur.com/fnrgZ87.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 5. Common Operation\n",
    "* 5.1 數值\n",
    "* 5.2 布林\n",
    "* 5.3 字串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 5.1. 數值\n",
    "* 整數(integer)\n",
    "* 浮點數(float)\n",
    "![](https://i.imgur.com/kD3JGSz.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "0\n",
      "-10\n",
      "3.3333333333333335\n",
      "1\n",
      "3\n",
      "0.01\n"
     ]
    }
   ],
   "source": [
    "print(10 + (-5))\n",
    "print(1 + 2 + 3 + 4 - 4 - 3 - 2 - 1)\n",
    "print(10 * (-1))\n",
    "print(10 / 3) # 會變成浮點數\n",
    "print(10 % 3)\n",
    "print(10 // 3)\n",
    "print(10 ** (-2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Float\n",
    "\n",
    "![](https://i.imgur.com/wSioQL5.png)\n",
    "\n",
    "參考:[IEEE754](https://zh.wikipedia.org/wiki/IEEE_754)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.3333333333333335"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 / 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## round():\n",
    "* 四捨六入五成雙(Banker's Rounding)\n",
    "    * 四 表示 < 5\n",
    "    * 六 表示 > 5\n",
    "    * 五 表示 當尾數為5,而尾數後面的數字均為0時,應看尾數\"5\"的前一位:若前一位數字此時為奇數,就應向前進一位;若前一位數字此時為偶數,則應將尾數捨去。數字\"0\"在此時應被視為偶數。\n",
    "    * 當尾數為5,而尾數\"5\"的後面還有任何不是0的數字時,無論前一位在此時為奇數還是偶數,也無論\"5\"後面不為0的數字在哪一位上,都應向前進一位。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(10/3) # default = 0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.33"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(10 / 3, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(1.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 浮點數與整數運算會變為浮點數"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n",
      "<class 'float'>\n",
      "3.0\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "print(1.5 + 2)\n",
    "print(type(1.5 + 2))\n",
    "print(1.5 // 0.5) # 浮點數運算完 還是浮點數\n",
    "print(type(1.5 // 0.2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### python2 與 python3 的除法\n",
    "![](https://i.imgur.com/WUql41d.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 5.2 布林(Boolean) : True、False(字首一定要大寫)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 字串(String)\n",
    "![](https://i.imgur.com/IxD0lJZ.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p\n",
      "e\n",
      "Apple Banana\n"
     ]
    }
   ],
   "source": [
    "a = \"Apple\"\n",
    "print(a[1])\n",
    "print(a[-1])\n",
    "\n",
    "b = 'Banana'\n",
    "c = a + \" \" + b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 6.輸入與輸出\n",
    "![](https://i.imgur.com/mNbvViQ.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## 輸入:input()  - string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a = input(\"Enter a sentence: \")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "a = input(\"Enter a number: \")\n",
    "print(\"The number you type is :\", a)\n",
    "print(type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a = input(\"Enter a number: \")\n",
    "# print(2 + a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![](https://i.imgur.com/vFcaBda.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 轉換型別 - int() 、 float() 、 str()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a = \"123\"\n",
    "print(type(a))\n",
    "\n",
    "b = int(a)\n",
    "print(type(b))\n",
    "\n",
    "c = float(b)\n",
    "print(type(c))\n",
    "print(c)\n",
    "\n",
    "d = str(c)\n",
    "print(type(d))\n",
    "print(d)\n",
    "print(d[3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 輸出:print() \n",
    "* sep = ' ' 指定每個輸出值之間的分隔字元,預設為空白。\n",
    "* end = ' ' 指定在句尾輸出的最後加上字元,預設為換行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(1, 2.0, '3', 4, end='')\n",
    "print(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(1, 2.0, '3', 4,  sep='' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(1, 2.0, \"3\", 4, end='!!!!!!!' )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## python2 與 python3 的print\n",
    "![](https://i.imgur.com/G5SpNSa.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 7.條件句\n",
    "![](https://i.imgur.com/Kok5Wbb.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "weather = input(\"Enter the weather: \")\n",
    "if weather == 'rainy':\n",
    "    print(\"Bring umbrella\")\n",
    "print(\"Go out.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "if False:\n",
    "    print(\"In the if.\" )\n",
    "print(\"Out of if.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 縮排(indentation)\n",
    "* 不同的程式區塊(block)\n",
    "* 一個程式的縮排要統一使用相同的空白數或統一使用tab\n",
    "* 建議使用四個空白鍵\n",
    "![](https://i.imgur.com/rceTWyv.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 試試看把註解(#)拿掉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "if 2 > 0:\n",
    "    print(\"True\")\n",
    "#  print(\"True\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![](https://i.imgur.com/HYjWTxL.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "![](https://i.imgur.com/jZZXFHt.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "score = 57\n",
    "if score >= 60:\n",
    "    print(\"Yeah! I pass.\")\n",
    "else:\n",
    "    print(\"Oh no! I fail.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "![](https://i.imgur.com/TnIyPpa.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# else if  = elif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "score = 60\n",
    "if score >= 90:\n",
    "    print('A')\n",
    "elif score >= 80:\n",
    "    print('B')\n",
    "elif score >= 70:\n",
    "    print('C')\n",
    "else:\n",
    "    print('D')\n",
    "    \n",
    "print('End')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 巢狀 if \n",
    "![](https://i.imgur.com/AlFHgf1.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "score = 70\n",
    "if score >= 80:\n",
    "    if score >= 90:\n",
    "        print('A')\n",
    "    else:\n",
    "        print('B')\n",
    "else: \n",
    "    if score >= 70:\n",
    "        print('C')\n",
    "    else:\n",
    "        print('D')\n",
    "print('End')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "if 0.1*3 == 0.3:\n",
    "    print('True')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(0.1*3 == 0.3)\n",
    "\n",
    "# WHY\n",
    "print(0.1*3)\n",
    "print(0.3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "# !!! 切記,判斷浮點數(float)的四則運算的結果時務必移項\n",
    "## 例如: a\\*b == c  代換成   (a*b - c) < 誤差值 (10**(-6))\n",
    "[IEEE754 - 2精度](https://zh.wikipedia.org/wiki/IEEE_754)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "if 0.1*3 - 0.3 < 10**(-6):\n",
    "    print('True')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 複習一下 if"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a = 1\n",
    "if a < 10:\n",
    "    print(a)\n",
    "    a += 2\n",
    "print(\"end\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 8.註解\n",
    "* 增加程式可讀性 - 對他人,甚至是之後的自己。\n",
    "* 方便除錯\n",
    "* 紀錄用法 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# print(\"In single line comment\")\n",
    "print(\"Out of comment\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "''' \n",
    "print(\"In multiple lines comment\")\n",
    "print(\"In multiple lines comment\")\n",
    "'''\n",
    "print(\"Out of comment\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# 不好/不必要的使用註解方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "x = 1\n",
    "x = x + 1     # Increment x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "# 正確的使用註解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "bike_time = 5     # hr\n",
    "bike_speed = 10   # km/hr\n",
    "bike_distance = bike_time * bike_speed\n",
    "print(bike_distance)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Appendix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 為物件命名 - 命名方式\n",
    "* 開頭必為大小寫英文字母或底線\n",
    "* 非開頭可以為大小寫英文字母、數字、底線\n",
    "* 區分大小寫(case sensitivity)\n",
    "* 不可為保留字(reserved words)及運算子(operator)\n",
    "    \n",
    "    \n",
    "![](https://i.imgur.com/MKfCASH.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 合法的變數命名方式\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "name0   = \"Mary\"\n",
    "_nAmE_2 = \"Joe\"\n",
    "Name0   = \"John\" # case sensitivity\n",
    "print(name0 , _nAmE_2 , Name0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 錯誤的變數命名方式,大家可以依序把註解(#)拿掉看看結果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#1name  = \"Mike\"\n",
    "#+name  = \"James\" # + 為運算子\n",
    "#name.1 = \"Kate\"\n",
    "#for    = \"Cindy\" # for 為保留字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 正確的命名方式 - 有意義的命名\n",
    "* 小寫、兩個單字以底線隔開、有意義的名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n"
     ]
    }
   ],
   "source": [
    "a1 = 5\n",
    "a2 = 10\n",
    "a3 = a1 * a2\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n",
      "90\n"
     ]
    }
   ],
   "source": [
    "bike_time = 5\n",
    "bike_speed = 10\n",
    "bike_distance = bike_time * bike_speed\n",
    "print(bike_distance)\n",
    "\n",
    "car_time = 3\n",
    "car_speed = 30\n",
    "car_distance = car_time * car_speed\n",
    "print(car_distance)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}