{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# [CodingX] Lesson 03 - Python_Basic(II)\n", "## by Kun-Ta Chuang\n", "\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": [ "# 複習一下我們昨天學到什麼\n", "1. python中任何東西都是個物件,每個物件都有它自己的型別(type)\n", "2. 常見的型別有int、float、string等等\n", "4. 除了int與float之間可以直接做運算,其他不同型別的物件彼此做運算時需要轉換型別。\n", "3. if ... elif ... else" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "score = int(input(\"Enter your grade: \"))\n", "if score < 70:\n", " print('D')\n", "elif score >= 70:\n", " print('C')\n", "elif score >= 80:\n", " print('B')\n", "elif score >= 90:\n", " print('A')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Index\n", "\n", "1. Composite data types (複合資料型態)\n", "2. Variables, Objects, and References\n", "3. Loop (迴圈): while、for" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 1. Composite data types (複合資料型態)\n", "\n", "* list\n", "* tuple\n", "* dict" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 1.1 List:有順序的物件集合,具有索引特性,長度可變動。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[] [100, 200, 300, 400, 500] ['apple', 'banana'] [1, 0.9, 'cat', [1, 2], ['apple', 'banana']]\n" ] } ], "source": [ "a = []\n", "numbers = [100, 200, 300, 400, 500]\n", "fruit = ['apple', 'banana']\n", "mixed = [1, 0.9, 'cat', [1, 2], fruit]\n", "print(a,numbers, fruit, mixed)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n", "200\n", "500\n" ] } ], "source": [ "numbers = [100, 200, 300, 400, 500]\n", "print(numbers[0])\n", "print(numbers[1])\n", "print(numbers[-1])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 常見的運算方式\n", "" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 3, 4]\n" ] } ], "source": [ "# example update\n", "numbers = [1,2,3,4]\n", "numbers[0] = 2\n", "numbers[1] = 3\n", "print(numbers)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## [i : j] 從 i 開始,遇到 j 結束\n", "" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100, 200, 300, 400, 500]\n", "[200, 300]\n" ] } ], "source": [ "numbers = [100, 200, 300, 400, 500]\n", "print(numbers)\n", "print(numbers[1:3])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[200, 400, 600, 800]\n" ] } ], "source": [ "numbers = [100, 200, 300, 400, 500, 600, 700, 800]\n", "print(numbers[1:8:2])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100, 300, 500, 700]\n", "True\n", "True\n" ] } ], "source": [ "numbers = [100, 200, 300, 400, 500, 600, 700, 800]\n", "\n", "del[numbers[1:10:2]]\n", "print(numbers)\n", "\n", "print(300 in numbers)\n", "print(400 not in numbers)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Math\n", "English\n" ] } ], "source": [ "# example condition\n", "courses = ['Math', 'Chinese']\n", "\n", "if 'Math' in courses:\n", " print('Math')\n", " \n", "if 'English' not in courses:\n", " print('English')" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4, 6, 1, 3, 5, 7, 9]\n", "[2, 4, 6, 2, 4, 6, 2, 4, 6, 1, 3, 5, 7, 9, 1, 3, 5, 7, 9]\n", "[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", "10\n", "[1, 3, 5, 7, 9]\n" ] } ], "source": [ "a = [2, 4, 6]\n", "b = [1, 3, 5, 7, 9]\n", "print(a + b)\n", "print(a * 3 + b * 2)\n", "\n", "c = [1]*10\n", "print(c)\n", "print(len(c))\n", "\n", "print(b)\n", "# print(max(b))\n", "# print(min(b))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "# exmaple delete\n", "\n", "numbers = [1, 2, 3, 4, 5]\n", "del numbers[0]\n", "print(numbers)\n", "del numbers[2]\n", "print(numbers)\n", "del numbers[1:2]\n", "print(numbers)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 常用的函式\n", "\n", "### [reference](https://docs.python.org/3/tutorial/datastructures.html)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5]\n", "[1, 2, 3, 4]\n", "[4, 3, 2, 1]\n", "[1, 2, 3, 4]\n" ] } ], "source": [ "# example\n", "\n", "num = [1,2,3,4]\n", "num.append(5)\n", "print(num)\n", "num.pop()\n", "print(num)\n", "num.reverse()\n", "print(num)\n", "num.sort()\n", "print(num)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 1.2 tuple:就像是list,不過list賦值之後可以更改,但是tuple不行。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, -1.2, 'hi')\n", "<class 'tuple'>\n" ] } ], "source": [ "a = (1, 2, -1.2, 'hi')\n", "print(a)\n", "print(type(a))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('apple', 'banana', 'grape', 'watermelon', 'guava', '123')\n" ] } ], "source": [ "fruit = ('apple', 'banana', 'grape', 'watermelon')\n", "fruit = fruit + ('guava', '123')\n", "print(fruit)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### 為什麼要加逗號?\n", "" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<class 'int'>\n", "<class 'tuple'>\n", "<class 'tuple'>\n" ] } ], "source": [ "a = (1)\n", "print(type(a))\n", "\n", "b = (1,)\n", "print(type(b))\n", "\n", "c = (1, 2)\n", "print(type(c))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "## 切記!有更動到值的運算都不能使用" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "number = (1, 2, 3)\n", "# number[0] = 5" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4, 5, 6)\n" ] } ], "source": [ "number = (1, 2, 3)\n", "number = (4, 5, 6)\n", "print(number)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "number = [1, 2, 3]\n", "number[0] = 4\n", "number[1] = 5\n", "number[2] = 6" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "\n", "## 1.3 字典(dictionary)\n", "" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Physics score = 50\n" ] } ], "source": [ "subject = ['Chinese', 'English', 'Math', 'History', 'Physics', 'Chemistry']\n", "score = [98, 65, 87, 77, 50, 88]\n", "\n", "ind = subject.index('Physics')\n", "print(\"Physics score = \", score[ind])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "> 字典的出現,使我們更方便取得對應的值。" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Physics score = 50\n" ] } ], "source": [ "grade = {\n", " 'Chinese' : 98,\n", " 'English' : 65,\n", " 'Math' : 87,\n", " 'History' : 77,\n", " 'Physics' : 50,\n", " 'Chemistry' : 88\n", "}\n", "\n", "print(\"Physics score = \", grade['Physics'])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "grade = {\n", " 'Chinese' : 98,\n", " 'English' : 65,\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "## Add\n", "print(grade)\n", "\n", "grade['Math'] = 100\n", "print(grade)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "## Update" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(grade)\n", "\n", "grade['English'] = 95\n", "print(grade)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0: 'Hi!', 'Apple': 12.3}\n", "12.3\n" ] } ], "source": [ "## key 與 value 可以為任意型態\n", "\n", "a = {\n", " 0 : 'Hi!',\n", " 'Apple' : 12.3,\n", "}\n", "print(a)\n", "print(a['Apple'])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 常用的函式\n", "\n", "## [reference](https://www.programiz.com/python-programming/methods/dictionary)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 2. Variables, Objects, and References" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# Object\n", "---\n", "**In Python, we do things with stuff.**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Recap: What is an object ?\n", "---" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Everything in Python is an object !!!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Built-in Types Object\n", "---\n", "Numbers, Strings, Lists, Dictionarys, Tuple, Files, Sets, etc." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Built-in Types Object example\n", "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Number\n", "12 # integer\n", "12.5 # float-point\n", "-12.5 # negative\n", "\n", "# String\n", "'Hello Word'\n", "\n", "# list\n", "[1, 2, 3, 4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# variable a, object\n", "a = 3\n", "a = 'Hello World'" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## The Dynamic Typing Interlude\n", "---\n", "In Python, types are determined automatically at runtime." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Variables (i.e. name)\n", "---\n", "\n", "**Variables are created when assigned, can reference any type of object, and must be assigned before they are referenced.**\n", "* Variable creation\n", "* Variable types\n", "* Variable use" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "a = 3 # Assign a name to an object\n", "\n", "# 1. Create an object to represent the value 3.\n", "# 2. Create the variable a, if it does not yet exist. \n", "# 3. Link the variable a to the new object 3." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Types Live with Objects, Not Variables" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "a = 3 # It's an integer\n", "a = 'spam' # Now it's a string\n", "a = 1.23 # Now it's a floating point" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Shared References (Shared Object)\n", "---\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Shared References and In-Place Changes\n", "---\n", "* immutable objects\n", " * int, float, bool, str, etc.\n", "* mutable objects\n", " * list, set, dict, etc." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "<img src=\"https://i.imgur.com/HWvxQCS.png\" width=450 height=180 />\n", "<img src=\"https://i.imgur.com/xGOu8Qs.png\" width=450 height=180 />" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Objects are Garbage-Collected\n", "---\n", "Whenever a name is assigned to a new object, the space held by the prior object is reclaimed if it is not referenced by any other name or object." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "a = 3 # It's an integer\n", "a = 'spam' # Now it's a string" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "x = 42\n", "x = 'shrubbery' # Reclaim 42 now (unless referenced elsewhere)\n", "x = 3.1415 # Reclaim 'shrubbery' now\n", "x = [1, 2, 3] # Reclaim 3.1415 now" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Example 2.1\n", "經過這三個statements, print(a)的結果為何?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# example1\n", "a = \"Hello\"\n", "b = a\n", "b = \"World\"\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n" ] } ], "source": [ "# answer\n", "a = \"Hello\"\n", "b = a\n", "b = \"World\"\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Example 2.2\n", "經過這三個statements, print(a)的結果為何?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# example\n", "a = [\"Hello\"]\n", "b = a\n", "b[0] = \"World\"\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['World']\n" ] } ], "source": [ "# answer\n", "a = [\"Hello\"]\n", "b = a\n", "b[0] = \"World\"\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 3. Loop (迴圈)\n", "\n", "\n", "- 3.1 While\n", "- 3.2 Nested loop - While\n", "- 3.3 For\n", "- 3.4 Nested loop - For" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 3.1 While" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\n", "" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 end\n" ] } ], "source": [ "a = 1\n", "while a < 100:\n", " print(a, end = ' ')\n", " a += 2\n", "print(\"end\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## 把小於10的奇數印出來\n", "\n", "-引用自http://www.mathwarehouse.com" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 要怎麼計算從1+2+3 + ... + 100 ? + 1000?\n", "## total = 1+ 2 + 3 + ... +100 + ... +1000 " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "total = 1 + 2 + 3 + 4 + 5 + 6" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## 太累了!!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 使用迴圈!\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "n = 100\n", "iterate = 0\n", "while iterate != n:\n", " iterate += 1\n", " print(iterate, end=' ')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5050\n" ] } ], "source": [ "n = 100\n", "iterate = 0\n", "total = 0\n", "while iterate != n:\n", " iterate += 1\n", " total += iterate\n", "print(total)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.1.1: while + if\n", "### 判斷1~6的數字是奇數還是偶數\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "a = 5\n", "while a < 7:\n", " if (a % 2 == 0):\n", " print(a, \"is even\")\n", " else:\n", " print(a, \"is odd\")\n", " a += 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.1.2: 欲求 1~1000 中可以整除11的最大數" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "n = 0\n", "ans = 0\n", "while n <= 1000:\n", " if (n % 11 == 0):\n", " ans = n\n", " n += 1\n", "print(ans)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## 思考一下\n", "### 找最大的數,從1000往1找比較快找到,還是我從1開始往1000找?" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1000\n", "999\n", "998\n", "997\n", "996\n", "995\n", "994\n", "993\n", "992\n", "991\n", "correct 990\n" ] } ], "source": [ "n = 1000\n", "flag = True\n", "while n > 0:\n", " if (n % 11 == 0) and flag:\n", " print(\"correct\", n)\n", " flag = False\n", " break\n", " print(n)\n", " n -= 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 明明找10次最大值 990 就找到了,可是程式還是找了1000次!\n", "\n", "## \"break\" :中斷其所在的最接近迴圈" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "n = 1000\n", "while n > 0:\n", " if (n % 11 == 0):\n", " print(\"correct\", n)\n", " break\n", " n -= 1\n", " print(n)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## \"continue\" : 省略下方迴圈區塊內的程式碼,進行迴圈的下一回合。\n", "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 is even.\n", "9 is odd.\n", "8 is even.\n", "7 is odd.\n", "6 is even.\n", "5 is odd.\n", "4 is even.\n", "3 is odd.\n", "2 is even.\n", "1 is odd.\n", "End\n" ] } ], "source": [ "n = 10\n", "while n > 0:\n", " if n % 2 == 0:\n", " print(n, \"is even.\")\n", " n -= 1\n", " continue\n", " print(n, \"is odd.\")\n", " n -= 1\n", "print(\"End\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 我想印 99乘法表" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1*1=1\t1*2=2\t1*3=3\t1*4=4\t1*5=5\t1*6=6\t1*7=7\t1*8=8\t1*9=9\n", "2*1=2\t2*2=4\t2*3=6\t2*4=8\t2*5=10\t2*6=12\t2*7=14\t2*8=16\t2*9=18\n", "3*1=3\t3*2=6\t3*3=9\t3*4=12\t3*5=15\t3*6=18\t3*7=21\t3*8=24\t3*9=27\n", "4*1=4\t4*2=8\t4*3=12\t4*4=16\t4*5=20\t4*6=24\t4*7=28\t4*8=32\t4*9=36\n", "5*1=5\t5*2=10\t5*3=15\t5*4=20\t5*5=25\t5*6=30\t5*7=35\t5*8=40\t5*9=45\n", "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t6*6=36\t6*7=42\t6*8=48\t6*9=54\n", "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t7*7=49\t7*8=56\t7*9=63\n", "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t8*7=56\t8*8=64\t8*9=72\n", "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t9*6=54\t9*7=63\t9*8=72\t9*9=81\n" ] } ], "source": [ "i = 1\n", "while i < 10:\n", " print(i, '*', 1, '=', i * 1, sep='', end='\\t')\n", " print(i, '*', 2, '=', i * 2, sep='', end='\\t')\n", " print(i, '*', 3, '=', i * 3, sep='', end='\\t')\n", " print(i, '*', 4, '=', i * 4, sep='', end='\\t')\n", " print(i, '*', 5, '=', i * 5, sep='', end='\\t')\n", " print(i, '*', 6, '=', i * 6, sep='', end='\\t')\n", " print(i, '*', 7, '=', i * 7, sep='', end='\\t')\n", " print(i, '*', 8, '=', i * 8, sep='', end='\\t')\n", " print(i, '*', 9, '=', i * 9, sep='', end='\\n')\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 如果今天是99*99乘法表呢?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 3.2 巢狀迴圈(nested loop) - while\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "i = 1 # 被乘數\n", "j = 1 # 乘數\n", "while i <= 9:\n", " while j <= 9:\n", " print(i, '*', j, '=', i* j, sep='', end='\\t')\n", " j += 1\n", " print(\"\")\n", " j = 1\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 課堂練習 - 思考一下 改變n的值會發生什麼事?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1*1=1\t1*2=2\t1*3=3\t1*4=4\t1*5=5\t\n", "2*1=2\t2*2=4\t2*3=6\t2*4=8\t2*5=10\t\n", "3*1=3\t3*2=6\t3*3=9\t3*4=12\t3*5=15\t\n", "4*1=4\t4*2=8\t4*3=12\t4*4=16\t4*5=20\t\n", "5*1=5\t5*2=10\t5*3=15\t5*4=20\t5*5=25\t\n", "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t\n", "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t\n", "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t\n", "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t\n" ] } ], "source": [ "n = 9\n", "i = 1\n", "j = 1\n", "while i <= n:\n", " while j <= 5:\n", " print(i, '*', j, '=', i* j, sep='', end='\\t')\n", " j += 1\n", " print(\"\")\n", " j = 1\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1*1=1\t1*2=2\t1*3=3\t1*4=4\t1*5=5\t1*6=6\t1*7=7\t1*8=8\t1*9=9\t\n", "2*1=2\t2*2=4\t2*3=6\t2*4=8\t2*5=10\t2*6=12\t2*7=14\t2*8=16\t2*9=18\t\n", "3*1=3\t3*2=6\t3*3=9\t3*4=12\t3*5=15\t3*6=18\t3*7=21\t3*8=24\t3*9=27\t\n", "4*1=4\t4*2=8\t4*3=12\t4*4=16\t4*5=20\t4*6=24\t4*7=28\t4*8=32\t4*9=36\t\n", "5*1=5\t5*2=10\t5*3=15\t5*4=20\t5*5=25\t5*6=30\t5*7=35\t5*8=40\t5*9=45\t\n", "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t6*6=36\t6*7=42\t6*8=48\t6*9=54\t\n", "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t7*7=49\t7*8=56\t7*9=63\t\n", "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t8*7=56\t8*8=64\t8*9=72\t\n", "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t9*6=54\t9*7=63\t9*8=72\t9*9=81\t\n" ] } ], "source": [ "n = 9\n", "i = 1\n", "j = 1\n", "while i <= n:\n", " while j <= n:\n", " print(i, '*', j, '=', i* j, sep='', end='\\t')\n", " j += 1\n", " print(\"\")\n", " j = 1\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.2.1: 只印出99乘法表中,小於50的式子\n", "### 多加一行 if i*j < 50: 搞定" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1*1=1\t1*2=2\t1*3=3\t1*4=4\t1*5=5\t1*6=6\t1*7=7\t1*8=8\t1*9=9\t\n", "2*1=2\t2*2=4\t2*3=6\t2*4=8\t2*5=10\t2*6=12\t2*7=14\t2*8=16\t2*9=18\t\n", "3*1=3\t3*2=6\t3*3=9\t3*4=12\t3*5=15\t3*6=18\t3*7=21\t3*8=24\t3*9=27\t\n", "4*1=4\t4*2=8\t4*3=12\t4*4=16\t4*5=20\t4*6=24\t4*7=28\t4*8=32\t4*9=36\t\n", "5*1=5\t5*2=10\t5*3=15\t5*4=20\t5*5=25\t5*6=30\t5*7=35\t5*8=40\t5*9=45\t\n", "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t6*6=36\t6*7=42\t6*8=48\t\n", "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t7*7=49\t\n", "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t\n", "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t\n" ] } ], "source": [ "i = 1\n", "j = 1\n", "while i <= 9:\n", " while j <= 9:\n", " if i*j < 50:\n", " print(i, '*', j, '=', i* j, sep='', end='\\t')\n", " j += 1\n", " print(\"\")\n", " j = 1\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1*1=1\t1*2=2\t1*3=3\t1*4=4\t1*5=5\t1*6=6\t1*7=7\t1*8=8\t1*9=9\t\n", "2*1=2\t2*2=4\t2*3=6\t2*4=8\t2*5=10\t2*6=12\t2*7=14\t2*8=16\t2*9=18\t\n", "3*1=3\t3*2=6\t3*3=9\t3*4=12\t3*5=15\t3*6=18\t3*7=21\t3*8=24\t3*9=27\t\n", "4*1=4\t4*2=8\t4*3=12\t4*4=16\t4*5=20\t4*6=24\t4*7=28\t4*8=32\t4*9=36\t\n", "5*1=5\t5*2=10\t5*3=15\t5*4=20\t5*5=25\t5*6=30\t5*7=35\t5*8=40\t5*9=45\t\n", "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t6*6=36\t6*7=42\t6*8=48\t\n", "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t7*7=49\t\n", "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t\n", "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t\n" ] } ], "source": [ "i = 1\n", "j = 1\n", "while i <= 9:\n", " while j <= 9:\n", " if i*j > 50:\n", " break\n", " print(i, '*', j, '=', i* j, sep='', end='\\t')\n", " j += 1\n", " print('')\n", " j = 1\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 無窮迴圈 (infinite loop)\n", "### 聽起來好像很厲害!! 試試下面的程式碼\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "\n", "\n", "## 若是使用 Terminal 請按Ctrl+c 強制停止" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 程式沒辦法結束\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 如何避免\n", "1. 使用迴圈時,確認迴圈會有結束的時候(變數起始值、累加、判斷..等)\n", "2. 使用 while True: 時,一定要有break,且確保會執行到break\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 3.3 for loop\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 語法\n", "\n", "### iterable object : list、range、tuple、dictionary" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## range()\n", "* range(起始值(預設為0), 結束值(不包含), 間隔(預設為1))\n", "* 序列型別的一種\n", "* 大多被用在for迴圈\n", "\n", "### 提示:回想一下list的[ i : j : k]\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "range(1, 10)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(1, 10)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "range" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(range(1, 10))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 1 2 3 4 5 6 7 8 9 " ] } ], "source": [ "for i in range(10):\n", " print(i, end=' ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "for i in range(2, 10, 2):\n", " print(i, end=' ')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.3.1: 從1加到100" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5050\n" ] } ], "source": [ "total = 0\n", "for i in range(101):\n", " total += i\n", "print(total)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5050\n" ] } ], "source": [ "n = 1\n", "total = 0\n", "while n <= 100:\n", " total += n\n", " n += 1\n", "print(total)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.3.2: for (with list)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple banana grape watermelon " ] } ], "source": [ "fruit = ['apple', 'banana', 'grape', 'watermelon']\n", "for i in fruit:\n", " print(i, end=' ')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 3 5 7 11 " ] } ], "source": [ "prime = [2, 3, 5, 7, 11]\n", "for i in prime:\n", " print(i, end=' ')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.3.3: for with string" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n", "p\n", "p\n", "l\n", "e\n" ] } ], "source": [ "fruit = 'Apple'\n", "for i in fruit:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.3.4: 使用 break" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "990\n" ] } ], "source": [ "for i in range(1000,0,-1):\n", " if (i % 11 == 0):\n", " print(i)\n", " break" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.3.5: 使用 continue " ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 is even.\n", "1 is odd.\n", "2 is even.\n", "3 is odd.\n", "4 is even.\n", "5 is odd.\n", "6 is even.\n", "7 is odd.\n", "8 is even.\n", "9 is odd.\n", "10 is even.\n" ] } ], "source": [ "for i in range(0,11):\n", " if(i % 2 == 0):\n", " print(i, \"is even.\")\n", " continue\n", " print(i, \"is odd.\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.3.6: 找出list中的最大值" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "21\n" ] } ], "source": [ "a = [10, 2, 8, 15, 21, 1]\n", "maximum = 0\n", "for i in a:\n", " if maximum < i:\n", " maximum = i\n", "print(maximum)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.3.7 用for loop完成 象限判斷" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A is in III\n", "B is in IV\n", "C is in I\n", "D is in II\n" ] } ], "source": [ "A = [-1, -10]\n", "B = [5, -5]\n", "C = [1, 1]\n", "D = [-2, 3]\n", "dots = [\n", " ['A', A], \n", " ['B', B], \n", " ['C', C], \n", " ['D', D]\n", "]\n", "\n", "for i in dots:\n", " print(i[0], \"is in\", end=' ')\n", " if i[1][0] > 0 and i[1][1] > 0:\n", " print(\"I\")\n", " elif i[1][0] < 0 and i[1][1] > 0:\n", " print(\"II\")\n", " elif i[1][0] < 0 and i[1][1] < 0:\n", " print(\"III\")\n", " elif i[1][0] > 0 and i[1][1] < 0:\n", " print(\"IV\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 3.4 巢狀迴圈(nested loop) - for\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.4.1 印出99乘法表" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1*1=1\t1*2=2\t1*3=3\t1*4=4\t1*5=5\t1*6=6\t1*7=7\t1*8=8\t1*9=9\t\n", "2*1=2\t2*2=4\t2*3=6\t2*4=8\t2*5=10\t2*6=12\t2*7=14\t2*8=16\t2*9=18\t\n", "3*1=3\t3*2=6\t3*3=9\t3*4=12\t3*5=15\t3*6=18\t3*7=21\t3*8=24\t3*9=27\t\n", "4*1=4\t4*2=8\t4*3=12\t4*4=16\t4*5=20\t4*6=24\t4*7=28\t4*8=32\t4*9=36\t\n", "5*1=5\t5*2=10\t5*3=15\t5*4=20\t5*5=25\t5*6=30\t5*7=35\t5*8=40\t5*9=45\t\n", "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t6*6=36\t6*7=42\t6*8=48\t6*9=54\t\n", "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t7*7=49\t7*8=56\t7*9=63\t\n", "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t8*7=56\t8*8=64\t8*9=72\t\n", "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t9*6=54\t9*7=63\t9*8=72\t9*9=81\t\n" ] } ], "source": [ "n = 10\n", "for i in range(1, n):\n", " for j in range(1, n):\n", " print(i, '*', j, '=', i* j, sep='', end='\\t')\n", " print(\"\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.4.2: 建立二維矩陣" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 0, 0], [0, 0, 0], [0, 0, 0]]\n" ] } ], "source": [ "n = 3\n", "m = 3\n", "table = []\n", "for i in range(n):\n", " table.append([0] * 3)\n", "print(table)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1]\n", "[1, [0, 0, 0]]\n" ] } ], "source": [ "empty = []\n", "empty.append(1)\n", "print(empty)\n", "\n", "empty.append([0] * 3)\n", "print(empty)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initialize : [[0, 0, 0], [0, 0, 0], [0, 0, 0]]\n", "[[1, 0, 0], [1, 0, 0], [1, 0, 0]]\n", "[[1, 2, 0], [1, 4, 0], [1, 8, 0]]\n", "[[1, 2, 3], [1, 4, 9], [1, 8, 27]]\n", "[[1, 2, 3], [1, 4, 9], [1, 8, 27]]\n" ] } ], "source": [ "n = 3\n", "m = 3\n", "table = []\n", "for i in range(n):\n", " table.append([0] * 3)\n", "print(\"Initialize : \",table)\n", "\n", "for i in range(n):\n", " table[0][i] = i+1\n", " table[1][i] = (i+1)**2\n", " table[2][i] = (i+1)**3\n", " print(table)\n", "print(table)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## example 3.4.3 把99乘法表的值存起來" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "scrolled": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]\n", "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "[2, 4, 6, 8, 10, 12, 14, 16, 18]\n", "[3, 6, 9, 12, 15, 18, 21, 24, 27]\n", "[4, 8, 12, 16, 20, 24, 28, 32, 36]\n", "[5, 10, 15, 20, 25, 30, 35, 40, 45]\n", "[6, 12, 18, 24, 30, 36, 42, 48, 54]\n", "[7, 14, 21, 28, 35, 42, 49, 56, 63]\n", "[8, 16, 24, 32, 40, 48, 56, 64, 72]\n", "[9, 18, 27, 36, 45, 54, 63, 72, 81]\n" ] } ], "source": [ "n = 9\n", "m = 9\n", "mul_table = []\n", "for i in range(n):\n", " mul_table.append([0] * m)\n", "\n", "print(mul_table) \n", "\n", "for i in range(1, 10):\n", " for j in range(1, 10):\n", " mul_table[i-1][j-1] = i*j\n", " print(mul_table[i-1])" ] } ], "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 }