Last active
July 7, 2022 12:34
-
-
Save scobin/d015db026ed718c78763d2e8495015af to your computer and use it in GitHub Desktop.
python-basic-02.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "python-basic-02.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyNKhPvJhX0uy+E8Tm/FcpDG", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/scobin/d015db026ed718c78763d2e8495015af/python-basic-02.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#資料型態\n", | |
"\n", | |
"- 數值類型:int, float, complex\n", | |
"- 布林類型:bool (True/False)\n", | |
"- 字串類型(string):str\n", | |
"- 序列類型:list(列表), tuple(元組)、range \n", | |
"- 映射類型:dict(字典) \n", | |
"\n", | |
"type方法:檢查資料型態\n", | |
"\n", | |
"## 參考:\n", | |
"https://docs.python.org/zh-tw/3.11/library/stdtypes.html#" | |
], | |
"metadata": { | |
"id": "JPnSJdYlTlOy" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"print(type(100))\n", | |
"print(type(2.5))\n", | |
"print(type(4j))\n", | |
"print(type(True))\n", | |
"print(type(False))\n", | |
"print(type('abc'))\n", | |
"\n", | |
"print(type([1, 2, 3]))\n", | |
"print(type((1, 2, 3)))\n", | |
"print(type(range(0, 4)))\n", | |
"print(type({'name': 'cyu', 'age': 30}))" | |
], | |
"metadata": { | |
"id": "q0Q8PxGIej8X", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "3922785a-db15-4cef-853d-348323b5474f" | |
}, | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"<class 'int'>\n", | |
"<class 'float'>\n", | |
"<class 'complex'>\n", | |
"<class 'bool'>\n", | |
"<class 'bool'>\n", | |
"<class 'str'>\n", | |
"<class 'list'>\n", | |
"<class 'tuple'>\n", | |
"<class 'range'>\n", | |
"<class 'dict'>\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#布林值 bool\n", | |
"只有兩種結果,`True` 和 `False` 。 \n", | |
"\n", | |
"常用於表示判斷式的結果。例如:`3>1`, `2==2`, `1!=2`。 \n", | |
"## 比較運算子: \n", | |
"`>, <, ==, >=, <=, !=`" | |
], | |
"metadata": { | |
"id": "_tntfx3X9THK" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"print(3!=3)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "ZXlMP_Jm-CUL", | |
"outputId": "e35ca277-57a7-4f61-c223-9713beed00e1" | |
}, | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"False\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# 列表 List\n", | |
"\n", | |
"可用來表示多筆資料。 \n", | |
"例子: \n", | |
"三個字串所組成的列表:`['apple', 'lemon', 'tomato']` \n", | |
"三個數字所組成的列表`[100, 200, 300]` \n", | |
"```\n", | |
"list = ['apple', 'lemon', 'tomato', 1]\n", | |
"```\n", | |
"\n", | |
"由「**[ ]**」, 「**要素**」所構成。 \n", | |
"可以用**索引(index)**來操作指定的要素。 \n", | |
"\n", | |
"## 索引(index)\n", | |
"```\n", | |
"index 0 1 2 3\n", | |
" -4 -3 -2 -1\n", | |
" ['apple', 'lemon', 'tomato', 1]\n", | |
" \n", | |
"```\n", | |
"- 是數字,「**0**」表示第一個。\n", | |
"- **正數** 指定從頭開始,**負數** 指定從尾開始。\n", | |
"- 可以指定 **範圍** 。 \n", | |
"\n", | |
"## 修改,追加,刪除\n", | |
"- 修改: `=`\n", | |
"- 追加: `append()`\n", | |
"- 刪除: `pop()`, `pop(index)`\n" | |
], | |
"metadata": { | |
"id": "YdBiuH5_1ocl" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"list = ['apple', 'lemon', 'tomato']\n", | |
"pop = list.pop(1)\n", | |
"print(pop)\n", | |
"print(list)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "9usAOOtq28Jw", | |
"outputId": "66921e7f-ffb2-4fa6-ab48-bf2fbb8455a6" | |
}, | |
"execution_count": 32, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"lemon\n", | |
"['apple', 'tomato']\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# 型態轉換\n", | |
"- 轉成文字. \n", | |
"```\n", | |
"str(123)\n", | |
"```\n", | |
"- 轉成整數. \n", | |
"```\n", | |
"int('90')\n", | |
"int(90)\n", | |
"```\n", | |
"- 轉成小數. \n", | |
"```\n", | |
"float('90.9')\n", | |
"float(90)\n", | |
"```" | |
], | |
"metadata": { | |
"id": "yTBijfQDuHsF" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"print(str(123), type(str(123)))\n", | |
"print(int('90'), type(int('90')))\n", | |
"print(int(90.9), type(int(90.9)))\n", | |
"print(float('90.9'), type(float('90.9')))\n", | |
"print(float(90), type(float(90)))\n" | |
], | |
"metadata": { | |
"id": "mB_lrEX7kn8P", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "7e773631-e03d-4105-c65f-f8b8c2315b65" | |
}, | |
"execution_count": 36, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"123 <class 'str'>\n", | |
"90 <class 'int'>\n", | |
"90 <class 'int'>\n", | |
"90.9 <class 'float'>\n", | |
"90.0 <class 'float'>\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#條件判斷: if, elif, else \n", | |
"考慮一個情境。要讓電腦做出以下判斷。 \n", | |
"a是一個整數變數, \n", | |
"- 如果 a=1 時輸出`a=1`\n", | |
"- 如果 a<1 時輸出`a<1`\n", | |
"- 如果 a>1 時輸出`a>1` \n", | |
"\n", | |
"```\n", | |
"if 條件1:\n", | |
" 條件1的處理程式\n", | |
"(elif 條件2:\n", | |
" 條件2的處理程式\n", | |
")\n", | |
"(elif 條件3:\n", | |
" 條件3的處理程式\n", | |
")\n", | |
"(else:\n", | |
" 處理程式\n", | |
")\n", | |
"```\n", | |
"\n", | |
"##條件\n", | |
"結果會是 True/False (布林類型)。 \n", | |
"\n", | |
"比較運算子:\n", | |
"`>`, `<`, `==`, `>=`, `<=`, `!=`" | |
], | |
"metadata": { | |
"id": "jvWVP4aisrZH" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 交通號誌\n", | |
"is_green = False\n", | |
"if is_green:\n", | |
" print('go')\n", | |
"print('----')" | |
], | |
"metadata": { | |
"id": "cB_RM-s7xxEH", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "a5d8d8be-8535-4824-877e-2146ba6c00fc" | |
}, | |
"execution_count": 40, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"----\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"a = int(input('a= '))\n", | |
"if a == 1:\n", | |
" print('a = 1')\n", | |
"elif a > 1:\n", | |
" print('a > 1')\n", | |
"else:\n", | |
" print('a < 1')" | |
], | |
"metadata": { | |
"id": "p9yFJVOXzuPr", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "024299e5-b800-435e-b577-16d920b28081" | |
}, | |
"execution_count": 43, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"a= 2\n", | |
"a > 1\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#迴圈處理: while, for\n", | |
"\n", | |
"## while迴圈(while loop)\n", | |
"\n", | |
"常用於重複執行某段程式碼。 \n", | |
"```\n", | |
"while 條件:\n", | |
" 處理程式\n", | |
"```\n", | |
"\n", | |
"注意: \n", | |
"1. 迴圈處理的範圍要「縮排」。 \n", | |
"2. 條件要設定好,避免造成「**無限迴圈**」。\n", | |
"3. break, continue的使用。 \n", | |
"break:立即終止迴圈處理。 \n", | |
"continue:停止本次的迴圈處理,開始下一次的迴圈處理。" | |
], | |
"metadata": { | |
"id": "05dIO2GRs5nO" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"a = 0\n", | |
"while a < 5: \n", | |
" a = a + 1\n", | |
" if a==3:\n", | |
" continue\n", | |
" print('hello')\n", | |
" \n" | |
], | |
"metadata": { | |
"id": "tTiBaj2dr1Up", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "307c606e-542c-4843-ed3c-9095c526b390" | |
}, | |
"execution_count": 46, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"hello\n", | |
"hello\n", | |
"hello\n", | |
"hello\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## for迴圈(for loop)\n", | |
"```\n", | |
"for 變數 in 可迭代的資料型態(iterable_object):\n", | |
" 處理程式\n", | |
"```\n", | |
"\n", | |
"iterable object 例子:\n", | |
"- list\n", | |
"- range\n", | |
"- string " | |
], | |
"metadata": { | |
"id": "giSODxzXr3R9" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"data = [0, 1, 2, 3, 4]\n", | |
"for x in data:\n", | |
" if (x == 2):\n", | |
" continue\n", | |
" print(x*2)\n" | |
], | |
"metadata": { | |
"id": "sozcNVyUuEpv", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "4a0d4f2c-9ff1-4903-e80c-6bf89651882b" | |
}, | |
"execution_count": 50, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"0\n", | |
"2\n", | |
"6\n", | |
"8\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# 猜數字" | |
], | |
"metadata": { | |
"id": "czaaGnExskMt" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import random\n", | |
"\n", | |
"answer = random.randint(0, 10)\n", | |
"play = True\n", | |
"while(play):\n", | |
" guess = int(input('猜一個數字'))\n", | |
" if guess > answer:\n", | |
" print('太高了')\n", | |
" elif guess < answer:\n", | |
" print('太低了')\n", | |
" else:\n", | |
" print('答對了')\n", | |
" play = False" | |
], | |
"metadata": { | |
"id": "IPhAA_-RohuY", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "7b11c3a8-6b81-4be6-8887-652d5543236d" | |
}, | |
"execution_count": 52, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"猜一個數字0\n", | |
"太低了\n", | |
"猜一個數字6\n", | |
"太低了\n", | |
"猜一個數字8\n", | |
"太低了\n", | |
"猜一個數字9\n", | |
"太低了\n", | |
"猜一個數字10\n", | |
"答對了\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment