Last active
July 3, 2022 10:13
-
-
Save scobin/d0e86f99a55c023259e2c6d8e8c6fb52 to your computer and use it in GitHub Desktop.
python-basic-01.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.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyOLyWQWBGw+S8osbX/oVHKq", | |
"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/d0e86f99a55c023259e2c6d8e8c6fb52/python-basic.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!python --version" | |
], | |
"metadata": { | |
"id": "dqqB52NAy-W2", | |
"outputId": "09cad819-10af-4434-a73b-2341ee31933e", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 36, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Python 3.7.13\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#輸出:print\n", | |
"\n", | |
"```\n", | |
"print(object(s), sep=separator, end=end, file=file, flush=flush)\n", | |
"```\n", | |
"\n", | |
"ex:\n", | |
"print(\"Hi 我有一個部落格,名稱是cyublog\")\n", | |
"\n", | |
"## Ref:\n", | |
"- https://www.w3schools.com/python/ref_func_print.asp\n" | |
], | |
"metadata": { | |
"id": "jKXqEvWWZy4P" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": { | |
"id": "2wdeYTE0ZSXR", | |
"outputId": "f5f4c249-8aa9-4cb5-c8ea-c8159e3e982f", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Hi 我有一個部落格,名稱是cyublog,歡迎參觀。" | |
] | |
} | |
], | |
"source": [ | |
"print(\"Hi 我有一個部落格,名稱是cyublog\", \"歡迎參觀\", sep=\",\", end=\"。\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# 變數 variable \n", | |
"命名: \n", | |
"- 變數名稱第一個字母不為數字。\n", | |
"- 變數名稱第一個字母不為大多數的符號,例如*$, #, @*等。但是可以使用***底線_***, 例如:_name, _age。\n", | |
"- 變數名稱中不能有空白。\n", | |
"- 區分大小寫。\n", | |
"\n", | |
"\n" | |
], | |
"metadata": { | |
"id": "PYK_nPRiaLhB" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"name = \"cyu\"\n", | |
"NAME = \"CYU\"\n", | |
"print(name)\n", | |
"print(NAME)" | |
], | |
"metadata": { | |
"id": "iCrf86FQeOJk", | |
"outputId": "287a87db-742a-4f3d-9d79-b3546bcfc74a", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 57, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"cyu\n", | |
"CYU\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"想要讓部落格名稱是非固定的、可變動時,設定變數可以達成效果。 \n", | |
"```\n", | |
"我有部落格,名稱是ooo \n", | |
"```\n", | |
"\n", | |
"把ooo的部分做為一個變數,就可以隨時變更他的值。\n", | |
"\n", | |
"print(變數):列出變數內容。 \n", | |
"\n", | |
"```\n", | |
"print(\"我有部落格,名稱是\", blog)\n", | |
"print(\"我有部落格,名稱是{}\".format(blog))\n", | |
"print(f\"我有部落格,名稱是{blog}\")\n", | |
"print(\"我有部落格,名稱是\" + blog)\n", | |
"```\n", | |
"\n", | |
"format方法\n", | |
"```\n", | |
"string.format(value1, value2...)\n", | |
"```\n", | |
"## Ref\n", | |
"https://www.w3schools.com/python/ref_string_format.asp" | |
], | |
"metadata": { | |
"id": "4B1bwnZhnBpj" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"blog = \"cyublog\"\n", | |
"print(\"我有部落格,名稱是\", blog, sep=\"\")\n", | |
"print(\"我有部落格,名稱是{}\".format(blog))\n", | |
"print(f\"我有部落格,名稱是{blog}\")\n", | |
"print(\"我有部落格,名稱是\" + blog)" | |
], | |
"metadata": { | |
"id": "boVF0mGfbPRE", | |
"outputId": "a6132576-fb4d-4498-f6d7-7af04b9fe7ab", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 59, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"我有部落格,名稱是cyublog\n", | |
"我有部落格,名稱是cyublog\n", | |
"我有部落格,名稱是cyublog\n", | |
"我有部落格,名稱是cyublog\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#四則運算: +, -, *, /\n", | |
"數字的加減乘除" | |
], | |
"metadata": { | |
"id": "6MBixOWAlqSB" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"print(1+1)\n", | |
"print(5-3)\n", | |
"print(12*3)\n", | |
"print(33/11)" | |
], | |
"metadata": { | |
"id": "Zl76SzxMlzBb", | |
"outputId": "a9c5b03e-0ade-44b0-9525-7cf72b005430", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 60, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"2\n", | |
"2\n", | |
"36\n", | |
"3.0\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#輸入: input\n", | |
"\n", | |
"```\n", | |
"input(prompt)\n", | |
"```\n", | |
"\n", | |
"讓使用者輸入內容,返回輸入的文字。 \n", | |
"如果想將內容轉換為數字可以使用int()方法,將文字轉成數字。 " | |
], | |
"metadata": { | |
"id": "0s4pSGtDrVt5" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"a = input('輸入一個數字')\n", | |
"print(type(a))\n", | |
"print(a)" | |
], | |
"metadata": { | |
"id": "TeXQOsaiwZNS", | |
"outputId": "e61a52dd-afa0-45fd-bd93-0f3fb483e0ce", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 62, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"輸入一個數字12\n", | |
"<class 'str'>\n", | |
"12\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#BMI計算\n", | |
"\n", | |
"BMI = 體重(kg)/( 身高(m)*身高(m) )" | |
], | |
"metadata": { | |
"id": "SZA5rUngrCBf" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"weight = int(input('輸入體重(kg):'))\n", | |
"height = float(input('輸入身高(m):'))\n", | |
"bmi = weight/(height*height)\n", | |
"print(bmi)" | |
], | |
"metadata": { | |
"id": "bMRckpI6rH52", | |
"outputId": "d41e8b84-c979-45d2-a7df-c549a2063068", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 244 | |
} | |
}, | |
"execution_count": 64, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"輸入體重(kg):ee\n" | |
] | |
}, | |
{ | |
"output_type": "error", | |
"ename": "ValueError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-64-65b1c7f56efd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mweight\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'輸入體重(kg):'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mheight\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'輸入身高(m):'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mbmi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mweight\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mheight\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mheight\u001b[0m\u001b[0;34m)\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[0mbmi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'ee'" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"try:\n", | |
" weight = int(input('輸入體重(kg):'))\n", | |
" height = float(input('輸入身高(m):'))\n", | |
" bmi = weight/(height*height)\n", | |
" print(bmi)\n", | |
"except ValueError:\n", | |
" print('請輸入正確數字。')" | |
], | |
"metadata": { | |
"id": "1aNdbAc20Z9D", | |
"outputId": "81d269ab-49f7-4246-9486-5e4e686b68a7", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 68, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"輸入體重(kg):abc\n", | |
"請輸入正確數字。\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment