{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "waZDna5nPCfT" }, "source": [ "\n", "(ch1)=\n", "# เขียนโค้ดแรกในภาษาไพธอน (Python) \n", "\n", "ในบทนี้เราจะศึกษา\n", "* การเขียนโค้ดอย่างง่ายในภาษาไพธอน\n", "* การใช้ข้อมูลหลากหลายชนิดในภาษาไพธอน และการเปลี่ยนชนิดของข้อมูล (Typecasting) เบื่องต้น\n", "* การใช้ตัวแปร (Variables) และเขียนนิพจน์ (Expressions) โดยใช้ตัวดำเนินการทางคณิตศาสตร์ (Arithmetic operators)\n", "* การใช้ฟังก์ชัน print() แสดงค่าตัวแปร (ค่านิพจน์) ออกหน้าแสดงผล\n", "\n", "\n", "Refs:\n", "* https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming\n", "* https://docs.python.org/3/tutorial/errors.html\n", "* https://docs.python.org/3/tutorial/controlflow.html#defining-functions\n", "* https://docs.python.org/3/library/functions.html#print\n", "* https://www.edx.org/course/python-basics-for-data-science\n", "* https://realpython.com/python-operators-expressions/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": { "id": "53d6jDxhPCfX" }, "source": [ "## โปรแกรม \"Hello World\" ในภาษาไพธอน" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ เราจะลองเขียนโปรแกรม **\"Hello World\"** ซึ่งเป็นโปรแกรมคอมพิวเตอร์พื้นฐานที่ทำการแสดงผลคำว่า \"Hello, World\" บนอุปกรณ์แสดงผล " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "โค้ดต่อไปนี้เป็น \"Hello World\" ในภาษาไพธอน " ] }, { "cell_type": "markdown", "metadata": { "id": "D0MHckbYPCfY" }, "source": [ "```{tip}\n", "รันโค้ดเซลล์ (```code cell```) ต่อไปนี้ โดยการกดคีย์ Shift + Enter\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1628484559273, "user": { "displayName": "Suti Chai", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgarRF-CZdRj7Ua_bTKi26VaUhYO8IIsLS2x2oP=s64", "userId": "07754154546807752175" }, "user_tz": -420 }, "id": "5Rto0uS8PCfY", "outputId": "3164ec11-84ba-4140-ca43-40037cc1e38b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World\n" ] } ], "source": [ "print('Hello, World')" ] }, { "cell_type": "markdown", "metadata": { "id": "naxRG7GOPCfa" }, "source": [ "\n", "$\\qquad$ โค้ดตัวอย่างข้างต้น ภายในวงเล็บ `\"Hello, World\"` เป็นข้อมูลชนิดสตริง (string) เป็นอาร์กิวเมนต์ (argument) ส่งไปยังฟังก์ชัน `print()` เพื่อให้แสดงข้อความดังกล่าวออกหน้าจอมอนิเตอร์\n", "\n", "\n", "```{note}\n", "$\\qquad$ ฟังก์ชัน `print()` เป็นฟังก์ชันฝังตัวในภาษาไพธอน (Built-in function: ฟังก์ชันที่ติดตั้งหรือมีอยู่แล้วในภาษา Python) เราสามารถใช้ได้เลยโดยไม่ต้องดำเนินการใดๆ เพิ่มเติม\n", "\n", "ฟังก์ชันฝังตัวในภาษาไพธอน นอกจากฟังก์ชัน `print()` แล้ว ยังมีฟังก์ชันฝังตัวอื่นๆ อีกมากมาย เช่น `input()`, `int()`, `float()`, `list()`, `tuple()`, `set()`, `pow()`, `abs()`, `id()` ฯลฯ ศึกษาเพิ่มเติมเกี่ยวกับฟังก์ชันฝังตัวได้ที่ [Python.org: Built-in Functions](https://docs.python.org/3/library/functions.html)\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ \"Hello World\" หรือ \"สวัสดีชาวโลก\" เป็นคำที่มนุษย์คอมพิวเตอร์ผู้มีอารยธรรม กล่าวทักทายต่อมนุษย์โลก เมื่อได้พบเจอกับมนุษย์โลกเป็นครั้งแรก \n", "\n", "$\\qquad$ โปรแกรม \"Hello World\" โปรแกรมแรกเกิดขึ้น จากหนังสือ [\"The C Programming Language\"](http://cslabcms.nju.edu.cn/problem_solving/images/c/cc/The_C_Programming_Language_(2nd_Edition_Ritchie_Kernighan).pdf) ที่แต่งโดย Brian W. Kernighan (ไบรอัน เคอร์นิงแฮน) และ Dennis M. Ritchie (เดนนิส ริตชี) ตีพิมพ์ในปี พ.ศ. 2521 (ค.ศ. 1978) \n", "\n", "โปรแกรม \"Hello World\" เป็นหนึ่งในโปรแกรมที่ง่ายที่สุดในการเขียนภาษาโปรแกรมต่างๆ มักถูกใช้เป็นตัวอย่างในการแสดงรูปแบบการเขียนหรือไวยากรณ์ของภาษาโปรแกรมนั้นๆ (Syntax) โดยมักจะเป็นโปรแกรมแรกที่ผู้เริ่มต้นเขียนโปรแกรมเขียนออกมา นอกเหนือจากนี้ ยังถูกใช้ในการทดสอบว่า ตัว Compiler หรือ Interpreter ที่ติดตั้งทำงานได้ปกติหรือไม่\n", "\n", "\n", "**ตัวอย่างโปรแกรม \"Hello World\" ในภาษา C, C++, Java และ JavaScript**\n", "\n", "```{code}\n", " /* < C > */\n", " #include \n", " int main(void) { \n", " printf(\"hello, world\\n\"); \n", " }\n", "\n", " // < C++ >\n", " #include \n", " int main() { \n", " std::cout << \"Hello, world!\\n\"; \n", " return 0; \n", " }\n", "\n", " // < Java >\n", " class HelloWorldApp {\n", " public static void main(String[] args) {\n", " System.out.println(\"Hello World!\");\n", " }\n", " }\n", "\n", " // < JavaScript >\n", " document.write('Hello, world!');\n", "\n", "```\n", "\n", "การเขียนโปรแกรม \"Hello World\" ในแต่ละภาษามีอยู่หลายรูปแบบ \n", "รูปแบบข้างต้นเป็นตัวอย่างของรูปแบบหนึ่งเท่านั้น \n", "\n", "อ่านเพิ่มเติมเกี่ยวกับการเขียนโปรแกรม \"Hello, World\" ด้วยภาษาอื่นๆ ได้ที่ [วิกิพีเดีย:โปรแกรมเฮลโลเวิลด์ในวิกิพีเดีย](https://th.wikipedia.org/wiki/โปรแกรมเฮลโลเวิลด์) " ] }, { "cell_type": "markdown", "metadata": { "id": "HksQorV-PCfa" }, "source": [ "(ch1-checkPythonVersion)=\n", "## การตรวจสอบเวอร์ชั่นของไพธอนที่ใช้งานอยู่\n" ] }, { "cell_type": "markdown", "metadata": { "id": "5_4U2Vh1PCfb" }, "source": [ "$\\qquad$ ไพธอนที่ใช้อยู่ในปัจจุบันหลักๆ มีอยู่ 2 เวอร์ชั่น คือ Python 2 และ Python 3\n", "\n", "* **Python 2** ถูกเพยแพร่ครั้งแรกในปี 2000 แต่ปัจจุบันถูกระงับการสนับสนุนอย่างเป็นทางการในปี 2020 โดยมีเวอร์ชั่น 2.7.18 เป็นเวอร์ชั่นสุดท้าย\n", "* **Python 3** ถูกเพยแพร่ครั้งแรกในปี 2008 เป็นไพธอนที่ใช้งานกันอยู่ในปัจจุบัน โดยมีมูลนิธิซอฟต์แวร์ไพธอน ([Python Software Foundation, PSF)(https://www.python.org/psf/)] ซึ่งเป็นองค์กรไม่แสวงผลกำไร ทำหน้าที่ดูแลและจัดการทรัพยากรสำหรับการพัฒนาไพธอน สามารถดาวโหลดเวอร์ชั่นล่าสุดได้ที่ [Download the latest version](https://www.python.org/downloads/)\n", "\n", "\n", "```{attention}\n", "\n", "1. **Python เวอร์ชัน 3 ไม่รองรับเวอร์ชัน 2** นั่นหมายความว่า โค้ดที่เขียนด้วยไพธอนเวอร์ชั่น 2 ไม่สามารถทำงานได้อย่างถูกต้องในไพธอนเวอร์ชั่น 3\n", "\n", " **เวอร์ชัน 2 และ เวอร์ชัน 3 แตกต่างกันหลายอย่าง** เช่น \n", " * คำสั่ง `print` \n", " ถ้าเป็นเวอร์ชั่น 2 จะเขียน `print \"Hello World!\\n\"` แต่ถ้าเป็นเวอร์ชั่น 3 จะถือว่าเป็นฟังก์ชัน จะเขียน `print(\"Hello World!\")` \n", "\n", " * การหารจำนวนเต็ม ```355/113``` ถ้าเป็นเวอร์ชั่น 2 จะได้ผลลัพธ์เป็นจำนวนเต็ม ```3 ``` แต่ถ้าเป็นเวอร์ชั่น 3 จะได้ผลลัพธ์เป็นทศนิยม ``` 3.1415929203539825``` เป็นต้น\n", "\n", " * นอกจากนี้ Python เวอร์ชั่น 3 ยังรองรับ Unicode (ยูนิโค๊ด) \n", " UTF-8 (ในขณะที่เวอร์ชั่น 2 รองรับ ASCII (7 bit)) มีฟังก์ชันที่มีความสามารถพิเศษมากกว่า\n", " ทำให้เวอร์ชั่น 3 มีโครงสร้างภาษาหรือไวยกรณ์ที่ง่ายกว่า เป็นต้น\n", "\n", " ความแตกต่างระหว่างวอร์ชัน 2 และ 3 สามารอ่านเพิ่มเติมได้ที่ [Interviewbit: Difference Between Python 2 and 3](https://www.interviewbit.com/blog/difference-between-python-2-and-3/) \n", "\n", "\n", "2. Python ถูกติดตั้งเป็นค่าเริ่มต้นบน Mac และ Linux แต่เป็นเวอร์ชัน 2 ในกรณีที่จะใช้เวอร์ชัน 3 ก็สามารถติดตั้งเวอร์ชัน 3 เพิ่มได้ (สามารถดาวโหลดเวอร์ชั่น 3 เวอร์ชั่นล่าสุดได้ที่ [Python.org: Download the latest version](https://www.python.org/downloads/))\n", "```\n", "\n", "การตรวจสอบเวอร์ชั่นของไพธอนที่ใช้งานอยู่สามารถทำได้ 2 วิธีหลัก คือ \n", "1. Command line (--version, -V, -VV) และ \n", "2. Script (sys, platform)\n", "\n", "\n", "เราลองใช้วิธี Script กันก่อนโดยเขียนโค้ดต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 339, "status": "ok", "timestamp": 1628484576668, "user": { "displayName": "Suti Chai", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgarRF-CZdRj7Ua_bTKi26VaUhYO8IIsLS2x2oP=s64", "userId": "07754154546807752175" }, "user_tz": -420 }, "id": "PBgtofH8PCfc", "outputId": "584ce675-7fc4-41e5-ce48-20c34e7fa8ee" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.8.13 (default, Jul 20 2022, 13:16:07) \n", "[Clang 13.1.6 (clang-1316.0.21.2.5)]\n" ] } ], "source": [ "import sys\n", "print(sys.version)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หรือ จะเขียนโดยใช้ ```sys.version_info```" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sys.version_info(major=3, minor=8, micro=13, releaselevel='final', serial=0)\n" ] } ], "source": [ "print(sys.version_info)" ] }, { "cell_type": "markdown", "metadata": { "id": "QugI4975PCfc" }, "source": [ "```sys``` เป็นโมดูลที่ฝังตัวอยู่ในภาษาไพธอน ([Built-in module](https://docs.python.org/3/py-modindex.html)) แต่ก่อนใช้งานต้อง `import` เข้ามาก่อน (รายละเอียดของการ Import จะอธิบายภายหลัง) ```sys.version``` จะคืนค่าเป็นข้อความ (String) ที่เป็นรุ่นของไพธอนที่กำลังใช้งานอยู่ ในขณะที่ `sys.version_info` จะคืนค่าเป็น tuple ที่มีค่าเป็นตัวเลข โดยเวอร์ชั่นจะประกอบไปด้วยตัวเลข 3 ส่วนหลักๆ คือ Major Minor และ Patch (Micro) เช่น 3.8.13 หรือ 3.10.0 ตามลำดับ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "ถ้ารันไพธอนผ่านทาง Terminal\t(ในระบบปฏิบัติการ Mac/Linux) หรือผ่านทาง Command Prompt (Cmd ในระบบปฏิบัติการ Windows)\n", "ให้ใช้คำสั่งใด python หรือ python3 โดยใส่ Option `--version`, `-V`, `-VV` คำสั่งหนึ่งต่อไปนี้\n", "\n", "* python --version\n", "* python -V\n", "* python -VV\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถรันคำสั่ง Command line ใน Notebook ได้ รายละเอียดจะอธิบายภายหลัง" ] }, { "cell_type": "markdown", "metadata": { "id": "53m59j4VPCfc" }, "source": [ "(ch1-commentsDocstrings)=\n", "## การเขียนคอมเมนต์ในภาษาไพธอน (Comments & Docstrings)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "TlAJjnKHPCfd" }, "source": [ "$\\qquad$ โปรแกรมเมอร์ที่ดีควรจะเขียน Comments หรือคำอธิบายโค้ดในโปรแกรม เพื่อให้ **ผู้อื่น** อ่านแล้วเข้าใจ และเพื่อช่วยให้**ผู้เขียนเอง**เขียนโปรแกรมได้ง่ายขึ้น (โดยเฉพาะโปรแกรมที่มีความซับซ้อน) อีกทั่้ง ยังจะช่วยให้ ผู้เขียนจำได้ว่าคืออะไร ตอนที่กลับมาอ่านในภายหลัง\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "การเขียนคอมเมนต์ในภาษาไพธอนมีด้วยกัน 2 วิธีคือ \n", "1. ใช้เครื่องหมาย hash (`\"#\"`) นำหน้าบรรทัดที่ต้องการคอมเมนต์ \n", "2. ใช้ DocString คล่อมข้อความหรือโค้ดที่ต้องการคอมเมนต์\n", "\n", "วิธีแรกใช้คอมเมนต์บรรทัดเดียว ส่วนวิธีที่สองใช้คอมเมนต์หลายบรรทัด" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **การเขียนคอมเมนต์บรรทัดเดียวโดยใช้เครื่องหมาย hash**\n", "\n", "\n", "$\\qquad$ โดยทั้วไป การคอมเมนต์ในภาษาไพธอนจะเริ่มต้นด้วยเครื่องหมาย hash (`\"#\"`) ข้อความที่อยู่หลัง `\"#\"` ไปจนสิ้นสุดบรรทัดนั้นๆ จะไม่มีผลต่อการทำงานของโปรแกรม (ความยาวไม่จำกัด เพียงแค่อยู่ในบรรทัดเดียวกัน)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "256" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# บรรทัดนี้ทั้งบรรทัดคือ Comment เพียงแค่เริ่มต้นด้วยเครื่องหมาย hash (#)\n", "\n", "# นอกจากนี้ยังสามารถเขียน Comment หลังโค้ดเพื่ออธิบายโค้ดก็ได้\n", "2**8 # คำนวณ 2 ยกกำลัง 8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "บ่อยครั้ง เราจะใช้เครื่องหมาย hash ('#') นำหน้าโค้ดที่ไม่ต้องการให้ถูกรัน (เรียกว่า ถูก \"Comment out\")\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "65536" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "# 2**8 # คำนวณ 2 ยกกำลัง <-- บรรทัดนี้ถูก Comment out\n", "2**16 # คำนวณจริงๆ คือ 2 ยกกำลัง 16" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{code}\n", "# *** ข้อควรระวัง ***\n", "เครื่องหมาย hash (#) ใช้\n", "คอมเมนต์ได้แค่บรรทัดเดียว \n", "หลายบรรทัดแบบนี้ไม่ได้ (จะเกิด Error)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ในกรณีหลายบรรทัดต้องใส่เครื่องหมาย `#` หน้าบรรทัดทุกบรรทัด" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# นี่เป็นตัวอย่างที่ดี\n", "# ที่จะแบ่งคอมเมนต์ออกเป็นหลายบรรทัด\n", "# ทำให้อ่านง่าย" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{tip}\n", "**คีย์ลัด (Shortcut keys)**\n", "\n", "1. คีย์ลัดในการเลือกหลายบรรทัด: \n", " กดคีย์ Shift ค้างไว้ แล้วเลือกบรรทัดโดยกดคีย์ $\\uparrow$/$\\downarrow$ (Shift + Arrow) หรือ คลิกซ้ายที่เมาส์ (Shift + Click) (จะเห็นเงาในบรรทัดที่เลือก)\n", "\n", "2. คีย์ลัดในการคอมเมนต์หลายบรรทัด:\n", "กดคีย์ Ctrl/Cmd + \"/\" (เอาคอมเมนต์ออกก็ใช้คีย์ลัดเดียวกัน)\n", "```" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\nแต่ถ้าไม่ชอบพิมพ์เครื่องหมาย \"#\"\\n(เพราะต้องกดกดปุ่ม \"Shift\")\\nแถมต้องกดคีย์ \\'Enter\\' จริงๆ\\nก็สามารถคอมเมนต์หลายบรรทัด\\nแบบนี้ โดยใช้ DocString แทนได้\\n(รายละเอียดอยู่ในหัวข้อถัดไป)\\n'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "แต่ถ้าไม่ชอบพิมพ์เครื่องหมาย \"#\"\n", "(เพราะต้องกดกดปุ่ม \"Shift\")\n", "แถมต้องกดคีย์ 'Enter' จริงๆ\n", "ก็สามารถคอมเมนต์หลายบรรทัด\n", "แบบนี้ โดยใช้ DocString แทนได้\n", "(รายละเอียดอยู่ในหัวข้อถัดไป)\n", "\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **คอมเมนต์หลายบรรทัดด้วย [DocString](https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings)**\n", "\n", "$\\qquad$ DocString (Triple Quote String) ในภาษาไพธอนคือ ข้อความหรือสายตัวอักษร (String/Literal string) ที่อยู่**ภายใน Triple Quote $'''...'''$** หรือ **Triple double quotes $\"\"\"...\"\"\"$**\n", "\n", "โดยทั่วไปจะเขียน DocString เป็นบรรทัดแรกภายในโมดูล (Module), ฟังก์ชัน (Function), คลาส (Class), เมธอด (Method) เพื่อใช้เป็นคอมเมนต์บอกว่า โมดูล ฟังก์ชัน คลาสและเมธอด นั้นๆ ใช้ทำอะไร เช่น" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\nProgram: rectangle.py\\nAuthor: Taitai Smally\\nLast update modified: 1/1/2022\\n\\nThe purpose of this program is to caculate the area of a rectangle.\\nThe input is an int or float number representing width and length of the rectangle. \\nThe output is a float number labeled the area of the rectangle.\\n'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''\n", "Program: rectangle.py\n", "Author: Taitai Smally\n", "Last update modified: 1/1/2022\n", "\n", "The purpose of this program is to caculate the area of a rectangle.\n", "The input is an int or float number representing width and length of the rectangle. \n", "The output is a float number labeled the area of the rectangle.\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "แต่อย่างไรก็ตาม เราสามารถใช้ DocString คอมเมนต์ข้อความหรือโค้ดตรงส่วนใดๆ ในโปรแกรมก็ได้ \n", "\n", "นอกจากนี้แล้ว ยังเรายังใช้ Docstring กำหนดข้อมูลชนิดสตริง (หลายบรรทัด) ได้อีกด้วย (รายละเอียดของสตริงอยู่ใน (การจัดการกับสตริง (String Operations))[ch2])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note} **PEP8**\n", "\n", "\n", "หลักเกณฑ์หรือสไตล์การเขียนโปรแกรม PEP8 แนะนำการเขียน DocString ที่เหมาะสมด้วยเครื่องหมาย ***Triple double quotes $\"\"\"...\"\"\"$**\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "Gs5Lxp6-PCfg" }, "source": [ "## [Exercise] ลองเขียนโด้ดไพธอนโค้ดแรกกัน" ] }, { "cell_type": "markdown", "metadata": { "id": "jyAnzknaPCfg" }, "source": [ "```{epigraph}\n", "\"The only way to learn a new programming language is by writing programs in it.\"\n", "\n", "-- Dennis M. Ritchie (1901-1975) \n", "\n", "```\n", "$\\\\$\n", "\n", "$\\qquad$ โปรแกรมเมอร์รุ่นพี่ (ไม่ว่าจะรุ่นเก่า รุ่นใหม่) เริ่มต้นการเขียนโค้ดโดยแสดงวลีในตำนาน \"hello, world\" ออกหน้าจอมอนิเตอร์ \n", "\n", "$\\\\$\n", "_ถึงตานักศึกษาแล้ว..._\n", "$\\\\$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. ใช้ฟังก์ชัน `print()` แสดงวลีในตำนาน ```\"Hello, World!\"``` ออกหน้าจอมอนิเตอร์\n", "2. ใช้ฟังก์ชัน `print()` แสดงวลีในตำนานเป็นฉบับภาษาไทย ```\"สวัสดี ชาวโลก!\"``` และใส่คอมเมนต์ต่อท้ายฟังก์ชัน print() ``` \"แสดงข้อความ Hello World ฉบับภาษาไทยโดยใช้ฟังก์ชัน print()\"``` \n", "3. แสดงข้อความหลายบรรทัดต่อไปนี้เพิ่มเติม\n", " \n", "```\n", "\"สวัสดี \n", "\n", "ยินดีต้อนรับเข้าสู่ Python\"\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "id": "uyf1hp7EPCfg" }, "source": [ "## ไพธอนจะแจ้ง Error ก่อนทำการรันหรือไม่?" ] }, { "cell_type": "markdown", "metadata": { "id": "1AzYRbw8PCfg" }, "source": [ "$\\qquad$ ไพธอน (Python) เป็นภาษาอินเตอร์พรีเตอร์ (Interpreted language) ทำงานโดยการแปลคำสั่งทีละคำสั่งด้วยตัวแปลภาษาไพธอน (Python Interpreter) ซึ่งแตกต่างจากภาษาอื่นเช่น C/C++, Java ซึ่งเป็นภาษาคอมไพล์ (Compiled language)\n", "\n", "ภาษาคอมไพล์ (Compiled language) เช่น ภาษา C/C++ และ Java จะตรวจสอบโปรแกรมทั้งหมดในเวลาคอมไพล์โดยคอมไพล์เรอร์ (Compiler) และจะแจ้งเกี่ยวกับข้อผิดพลาดทั้งหมดก่อนที่จะแปลงเป็นภาษาเครื่องเพื่อทำการรัน ในขณะที่ภาษาอินเตอร์พรีเตอร์ (Interpreted language) เช่น ภาษา Python, JavaScript และ Ruby ทำงานโดยการแปลคำสั่งทีละคำสั่งด้วยตัวแปลภาษา(Interpreter) เมื่อพบข้อผิดพลาด จะหยุดรันโปรแกรม (เว้นแต่จะมีการจัดการข้อผิดพลาดโดยโปรแกรมเมอร์ ชนิดของข้อผิดพลาดและการจัดการข้อผิดพลาดจะอธิบายรายละเอียดในภายหลัง)" ] }, { "cell_type": "markdown", "metadata": { "id": "X7uuVQn2PCfg" }, "source": [ "ลองรันโค้ดต่อไปนี้ ดูว่าเกิดจะมีข้อความอะไรอะไรแสดงบ้าง\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "OLEeGVMYPCfg" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This will be printed out\n" ] }, { "ename": "NameError", "evalue": "name 'printf' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [9], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThis will be printed out\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mprintf\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThis will cause an error (NameError)\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThis will NOT be printed (This is because Python interpreter has stopped working!)\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'printf' is not defined" ] } ], "source": [ "print(\"This will be printed out\")\n", "printf(\"This will cause an error (NameError)\")\n", "print(\"This will NOT be printed (This is because Python interpreter has stopped working!)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เมื่อรันเซลล์ ไพธอนจะประมวลผลทีละคำสั่ง โดยรันคำสั่งแรก `print(\"This will be printed out\")` ไม่มีข้อผิดผลาด จึงแสดงข้อความที่หน้าจอ ```This will be printed out``` จากนั้นรันคำสั่งที่ 2 `printf(\"This will cause an error (NameError)\")` เกิดข้อผิดพลาด ไพธอนจะแจ้งข้อผิดพลาดที่หน้าจอ ```NameError: name 'printf' is not defined``` และหยุดรันโปรแกรมทันที **โดยไม่ประมวลผลคำสั่งถัดไป**" ] }, { "cell_type": "markdown", "metadata": { "id": "tBMJuqqUPCfe" }, "source": [ "(ch1-errors)=\n", "## Error (ข้อผิดพลาด) ในภาษาไพธอน" ] }, { "cell_type": "markdown", "metadata": { "id": "UjOmnPK1PCff" }, "source": [ "```{epigraph}\n", "\n", "\n", "\"Testing leads to failure, and failure leads to understanding.\"\n", "\n", "-- Burt Rutan (1943-) \n", "\n", "\n", "```\n", "\n", "$\\\\$ \n", "\n", "$\\qquad$ เมื่อเกิด Error ขึ้น เราจะจำเป็นจะต้องอ่านข้อความเออเรอร์ เพราะจะบอกถึงสาเหตุ และแสดงจุดที่ผิดพลาดตำแหน่งนั้นๆ เช่นในตัวอย่างข้างต้นที่สะกดชื่อฟังก์ชัน `print` ผิด กลายเป็น `printf` ไพธอนจะแสดง Error ดังต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 198 }, "executionInfo": { "elapsed": 352, "status": "error", "timestamp": 1628484652360, "user": { "displayName": "Suti Chai", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgarRF-CZdRj7Ua_bTKi26VaUhYO8IIsLS2x2oP=s64", "userId": "07754154546807752175" }, "user_tz": -420 }, "id": "YZbE9ISLPCff", "outputId": "31ebfdcc-ec98-4a2d-9b13-03ce96b63826" }, "outputs": [ { "ename": "NameError", "evalue": "name 'printf' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [10], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# ลองรันโค้ดต่อไปนี้\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;66;03m# แสดงข้อความ (ข้อผิดผลาด (Error)) ออกหน้าจอ\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m \u001b[43mprintf\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHello, Python!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'printf' is not defined" ] } ], "source": [ "# ลองรันโค้ดต่อไปนี้\n", "# แสดงข้อความ (ข้อผิดผลาด (Error)) ออกหน้าจอ\n", "\n", "printf(\"Hello, Python!\")" ] }, { "cell_type": "markdown", "metadata": { "id": "6giebeL5PCff" }, "source": [ "ไพธอนพยายามประมวลผลคำสั่ง `printf` แต่ไม่รู้ว่า `printf` คืออะไร จึงเกิดข้อผิดพลาดขึ้น\n", "\n", "\n", "**ข้อความเออเรอร์จะบอก...**\n", "\n", "* ตำแหน่งที่เกิดข้อผิดพลาด (โดยเฉพาะกับโค้ดยาวๆ) และ\n", "* ชนิดของข้อผิดพลาด เช่นกรณีข้างต้น เป็นข้อผิดพลาดชนิด NameError (ข้อผิดพลาดที่เกิดเมื่อไม่พบ Identifier) โดยมีข้อความ ```name 'printf' is not defined``` (printf ไม่ได้ถูกกำหนดไว้) เป็นส่วนขยายความ แสดงใน**บรรทัดสุดท้ายของข้อความเออเรอร์**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "++\n", "\n", "**ชนิดของข้อผิดพลาด (Type of Errors)**\n", "\n", "ข้อผิดพลาดที่เกิดขึ้นจากการเขียนโปรแกรมแบ่งออกเป็น 3 ชนิด คือ\n", "1. ข้อผิดพลาดที่เกิดจากไวยากรณ์ (Syntax Errors)\n", "2. ข้อผิดพลาดในขณะที่รันโค้ด (Runtime Errors)\n", "3. ข้อผิดพลาดที่เกิดจากตรรกะโปรแกรม (Logic Errors)" ] }, { "cell_type": "markdown", "metadata": { "id": "bUkMJks7PCff" }, "source": [ "### ข้อผิดพลาดที่เกิดจากไวยากรณ์ (Syntax Errors)\n", "\n", "$\\qquad$ ข้อผิดพลาดที่เกิดจากไวยากรณ์ หรือ Syntax Errors น่าจะเป็นข้อผิดพลาดที่พบได้บ่อยที่สุดในช่วงเร่ิมเขียนภาษาไพธอน ตัวอย่าง Syntax Errors ที่มักพบมากที่สุดคือ\n", "- ลืมปิดวงเล็บ\n", "- ลืมใส่โคลอน `,`\n", "- สับสนระหว่าง `=` และ `==`\n", "- เยื้อง (Indentation) ผิด \n", "\n", "เป็นต้น\n", "\n", "\n", "ตัวอย่างของ Syntax Errors \n", "\n", "ถ้าสตริง (string) ไม่ได้ปิดด้วยเครื่องหมาย ```'Single quotes'``` (หรือ ```\"Double quotes\")``` จะแสดง ```SyntaxError``` (โครงสร้าง/รูปแบบไวยากรณ์ของภาษา (Syntax))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "4J2KAWKpPCff" }, "outputs": [ { "ename": "SyntaxError", "evalue": "EOL while scanning string literal (2090099991.py, line 3)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn [11], line 3\u001b[0;36m\u001b[0m\n\u001b[0;31m print(\"Hello, Python!)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n" ] } ], "source": [ "# ลองรันโค้ดต่อไปนี้ ดูว่าจะแสดงข้อผิดผลาดอะไร\n", "\n", "print(\"Hello, Python!)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ข้อผิดพลาดในขณะที่รันโปรแกรม (Runtime Errors)\n", "\n", "$\\qquad$ Runtime Errors เป็นข้อผิดพลาดที่เกิดในขณะที่ทำการรันโปรแกรม คำสั่งหรือโค้ดอาจจะเขียนถูก (ไม่มี Syntax errors) แต่ไม่สามารถรันได้\n", "\n", "ตัวอย่างของข้อผิดพลาดขณะที่ทำการรันโปรแกรม (Runtime Erors) ที่มักจะพบบ่อย เช่น\n", "- องค์ประกอบของข้อมูล String List ฯลฯ ไม่ถูกต้อง (IndexError, ValueError, ...)\n", "- พิมพ์ชื่อตัวแปร/ชื่อฟังก์ชันผิด (NameError)\n", "- โมดูล/ไลบรารี่ที่ import เข้ามา ไม่มีหรือไม่ถูกต้อง (ModuleNotFoundError)\n", "- การประมวลผลและประเภทของข้อมูลที่ไม่สอดคล้องกัน (TypeError, ValueError)\n", "- การหารด้วยศูนย์ (ZeroDivisionError)\n", "\n", "เป็นต้น\n", "\n", "\n", "ตัวอย่างต่อไปนี้เป็นการหารด้วยศูนย์" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [12], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# ZeroDivisionError เป็นข้อผิดพลาดที่เกิดเมื่อหาร \"/\", \"//\" หรือ \"%\" (modulo) ด้วยศูนย์\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m)\n", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "# ZeroDivisionError เป็นข้อผิดพลาดที่เกิดเมื่อหาร \"/\", \"//\" หรือ \"%\" (modulo) ด้วยศูนย์\n", "print(1/0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ข้อผิดพลาดที่เกิดจากตรรกะโปรแกรม (Logic Errors)\n", "\n", "$\\qquad$ ข้อผิดพลาดที่เกิดจากตรรกะโปรแกรม (Logic Errors) คือ \"Bugs\" ของโปรแกรม เป็นข้อผิดพลาดที่เกิดจากซอร์สโค้ดซึ่งส่งผลให้ผลลัพธ์ที่ได้ไม่ถูกต้องหรือโปรแกรมทำงานไม่เป็นไปตามที่คาดคิดไว้ เช่น ตัวอย่างต่อไปนี้\n", "\n", "```{important}\n", "ข้อผิดพลาดที่เกิดจากตรรกะโปรแกรม (Logic Errors) เป็นข้อผิดพลาดแบบนี้ไพธอนไม่สามารถตรวจพบได้ ดังนั้นผู้เขียนโปรแกรมจึงต้องมีการตรวจสอบเงื่อนไขหรือสูตรให้ถูกต้อง และขณะเขียนโค้ด ให้ตรวจสอบว่ามีอะไรแตกต่างจากที่เราคาดหวัง หากจำเป็น ให้แทรกคำสั่ง \"print()\" ลงในโค้ดเพื่อตรวจสอบผลการอินพุต/เอาต์พุต)\n", "```\n", "\n", "ตัวอย่างต่อไปนี้ เป็นการหาค่าเฉลี่ยของตัวเลขสองจำนวน (ที่ผิด)\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "60.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num1 = 20\n", "num2 = 80\n", "\n", "average = num1 + num2 / 2\n", "average" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "โค้ดข้างต้นประมวลผลได้ แต่ให้ผลลัพธ์ที่ไม่ถูกต้อง (เนื่องจากค่าเฉลี่ยของ 20 และ 80 ไม่ได้ 50 ตามที่ควรจะเป็น)\n", "\n", "ลองแก้โค้ดข้างต้นให้ถูกต้อง" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": { "id": "qCuBaeCwPCfi" }, "source": [ "## ชนิดของออบเจกต์ (Object) พื้นฐานในภาษาไพธอน" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ ไพธอนเป็นภาษาเชิงวัตถุ (object-oriented programming; OOP) [(Wiki: ไพธอน)](https://th.wikipedia.org/wiki/ไพธอน_(ภาษาโปรแกรม)) โดยมองสิ่งต่างๆ ในระบบเป็นวัตถุหรือออบเจกต์ (Object)[^object]\n", " ชิ้นหนึ่ง\n", "\n", "$\\qquad$ ออบเจกต์ในภาษาไพธอน มีให้เลือกใช้งานหลากหลายชนิดเช่นเดียวกับภาษาอื่นๆ และแต่ละชนิดก็มีเป้าหมายในการใช้งานที่แตกต่างกัน\n", "\n", "ออบเจกต์พื้นฐานในไพธอนสรุปเป็นตารางได้ดังนี้\n", "\n", "| **กลุ่มชนิดข้อมูล** | **ชนิดข้อมูล** |\n", "|-------------------------------------|------------------------------|\n", "| ข้อมูลประเภทตัวอักษร | str |\n", "| ข้อมูลประเภทตัวเลข | int, float, complex |\n", "| ข้อมูลประเภท Sequence | list, tuple, range |\n", "| ข้อมูลประเภท Mapping | dict |\n", "| ข้อมูลประเภทเซ็ท Set | set, frozenset |\n", "| ข้อมูลประเภทค่าความจริง | bool |\n", "| ข้อมูลประเภทไบนารี่ | bytes, bytearray, memoryview |\n", "\n", "\n", "\n", "[^object]: ออบเจกต์ (Object) ในที่นี้ไม่ได้หมายถึง Instance ของ class ในภาษา OOP เท่านั้น แต่ยังหมายถึงตัวแปรทุกชนิด(ซึ่งโดยปกติในภาษา OOP จะมองว่าเป็น Object ทั้งหมด) " ] }, { "cell_type": "markdown", "metadata": { "id": "I6AtJS57PCfi" }, "source": [ "**ออบเจกต์ (Object) พื้นฐานที่ใช้มากที่สุด คือ ```str``` (strings), ```int``` (integers) และ ```float``` (floats)**" ] }, { "cell_type": "markdown", "metadata": { "id": "wNBK9sKuPCfi" }, "source": [ "```{image} images/TypesObjects.png\n", ":alt: types-of-objects\n", ":width: 200px\n", ":align: center\n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "-Ypa_fLKPCfi" }, "source": [ "ตัวอย่างต่อไปนี้ เป็นตัวอย่างของจำนวนเต็ม (Integer) จำนวนจริงหรือจำนวนทศนิยม (Float) และแถวตัวอักษร (String) ตามลำดับ" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "id": "Do6NPhaNPCfi" }, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12 # จำนวนเต็ม (Integer) 1 โหล" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "id": "c4i3YpntPCfj" }, "outputs": [ { "data": { "text/plain": [ "1.618033988749" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1.618033988749 # จำนวนจริง หรือจำนวนทศนิยม (Float) ค่าของ Golden ratio" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.6e-19" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1.6e-19 # เลขทศนิยมในรูปของเลขยกกำลังฐานสิบ ค่าประจุดของอิเล็กตรอน " ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "id": "4sxYY_ZGPCfj" }, "outputs": [ { "data": { "text/plain": [ "'Hello, Python!'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello, Python!\" # String แถวอักษรที่อยู่ภายใน Single quote หรือ Double quotes" ] }, { "cell_type": "markdown", "metadata": { "id": "nV1G095XPCfj" }, "source": [ "ไพธอนมีฟังก์ชัน ```type()``` (built-in function) บอกชนิดของออบเจกต์\n", "\n", "* integers $\\rightarrow$ ```int``` (), \n", "\n", "* floats $\\rightarrow$ ```float``` (),\n", "\n", "* character strings $\\rightarrow$ ```str``` ()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ลองใช้ฟังก์ชัน ```type()``` แสดงชนิดของออบเจกต์" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "id": "rHxmJb4hPCfj" }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ชนิดข้อมูลของ 12\n", "\n", "type(12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เป็น ```int``` ซึ่งย่อมาจาก _integer number_ (จำนวนเต็ม)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "id": "84zXfMtQPCfj" }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ชนิดข้อมูลของ 1.618033988749\n", "\n", "type(1.618033988749)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เป็น ```float``` ซึ่งย่อมาจาก _floating point number_ (จำนวนเต็ม)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "id": "E53ZPk_sPCfj" }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ชนิดข้อมูลของ \"Hello, Python!\"\n", "\n", "type(\"Hello, Python!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เป็น ```str``` ซึ่งย่อมาจาก _string_ (สตริงหรือสายอักขระ)" ] }, { "cell_type": "markdown", "metadata": { "id": "nfE42dqbPCfj" }, "source": [ "ลองเขียนโค้ด ตรวจสอบดูว่า ```7.0``` เป็นออบเจกต์ชนิด int หรือ float\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "id": "NAy0eyZ4PCfj" }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": { "id": "zWSjHVSxPCfk" }, "source": [ "### จำนวนเต็ม (Integers)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "TcOneIhMPCfk" }, "source": [ "$\\qquad$ จำนวนเต็ม (integers) เป็นได้ทั้งตัวเลขที่เป็นบวกและลบ เช่น" ] }, { "cell_type": "markdown", "metadata": { "id": "Zq5WdaWfPCfk" }, "source": [ "```{image} images/TypesInt.png\n", ":width: 200px\n", ":align: center\n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "li3k0uT3PCfk" }, "source": [ "เราสามารถตรวจสอบชนิดได้โดยใช้ฟังก์ชัน ```type()```" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "id": "C37adVJ2PCfk" }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(-1) # ชนิดข้อมูลของ -1" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "id": "hAg0jP8oPCfk" }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(4) # ชนิดข้อมูลของ 4" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "id": "dNE7pw1vPCfk" }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(0) # ชนิดข้อมูลของ 0\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python ไม่ให้เขียน 0 หน้าจำนวนเต็ม (ห้าม!) มิฉะนั้นจะเกิด SyntaxError\n" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers (1587878768.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn [26], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 007 # SyntaxError\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers\n" ] } ], "source": [ "007 # SyntaxError" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note} **เลขจำนวนเต็มในไพธอน ...**\n", "* เลขจำนวนเต็มใน Python เวอร์ชัน 2 มี 2 ชนิด คือ `int` และ `long` แต่ใน Python 3 มีเพียงชนิดเดียวคือ `int` และมีสิ่งที่ต่างกับภาษาอื่นๆ คือ **มีขนาดไม่จำกัด!** ดังนั้น เราสามารถให้จำนวนเต็มมีกี่หลักก็ได้ (แต่แน่นอน ไม่เกินหน่วยความจำ (Memory) ของคอมฯ ที่ใช้เก็บข้อมูล)[Mortada Mehyar: Can Integer Operations Overflow in Python?](https://mortada.net/can-integer-operations-overflow-in-python.html) \n", "* นอกจากเลขจำนวนเต็มฐาน 10 แล้ว ไพธอนยังมีเลขจำนวนเต็มฐาน 2 (binary) ฐาน 8 (octal) และ ฐาน 16 (hexadecimal) อีกด้วย เช่น \n", "\n", ">> `0b10` เป็นเลขจำนวนเต็มฐาน 2 มีค่าเท่ากับ `2` \n", "\n", ">> `0o10` เป็นเลขจำนวนเต็มฐาน 8 มีค่าเท่ากับ `8` \n", "\n", ">> `0x10` เป็นเลขจำนวนเต็มฐาน 16 มีค่าเท่ากับ `16` เป็นต้น \n", "\n", ">> (ไม่อธิบายรายละเอียดแต่สามารถศึกษาเพิ่มเติมได้ที่เว็บ [python.org: PEP 3127 – Integer Literal Support and Syntax](https://peps.python.org/pep-3127/)) \n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "TUanOzKEPCfk" }, "source": [ "### จำนวนจริง (Floats)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "WNP72axYPCfl" }, "source": [ "$\\qquad$ Floats เป็นเลขจำนวนจริง หรือ \"ตัวเลขที่มีทศนิยม\" (จำนวนจริงเป็น superset ของตัวเลขจำนวนเต็ม) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ทั้ง ```1``` และ ```1.0``` มีค่าเท่ากัน แต่ชนิดของข้อมูลต่างกัน" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "id": "0oKOzO5dPCfl" }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(1.0) # ข้อสังเกต: 1 เป็นชนิดชนิด int ในขณะที่ 1.0 เป็นชนิด float" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เช่นกัน ทั้ง ```0``` และ ```0.0``` มีค่าเท่ากัน แต่ชนิดของข้อมูลต่างกัน" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "id": "oc5k4ZnTPCfl" }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(0.0) # ชนิดข้อมูลของ 0.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "จำนวนจริงเขียนได้ทั้งในรูปทศนิยม และในรูปของเลขยกกำลังฐานสิบ\n", "\n", "ยกตัวอย่าง เช่น ค่าคงตัวของแก๊ส (Gas constant) $R = 8.314472 \\times 10^{-15} J \\cdot K^{-1} \\cdot mol^{-1}$" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "id": "pljuO1vYPCfl", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(8.314472E-15) # ชนิดข้อมูลของ 8.314472E-15 \n" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (1285332365.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn [30], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 1e0.5 # SyntaxError: เลขชี้กำลังต้องเป็นจำนวนเต็มเท่านั้น\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "1e0.5 # SyntaxError: เลขชี้กำลังต้องเป็นจำนวนเต็มเท่านั้น" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ในภาษาไพธอน จำนวนเต็มมีขนาดไม่สิ้นสุด! แล้วจำนวนจริงหละ??\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ ตัวเลขที่มากที่สุด (Max) และน้อยที่สุด (Min) ของข้อมูลตัวเลขชนิด Floats สามาถตรวจสอบได้โดยใช้โมดูล ```sys``` ใช้ ```sys.float_info``` (sys.float_info.max, sys.float_info.min) (รายละเอียดของโมดูล [sys](https://docs.python.org/3/library/sys.html) )" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "id": "fsfEXkKqPCfl" }, "outputs": [ { "data": { "text/plain": [ "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# System settings about float type\n", "sys.float_info" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เนื่องจากคอมพิวเตอร์ผู้เชียนเป็น 64-bit ตัวเลขจำนวนจริงจะอยู่ระหว่าง 2.2250738585072014e-308 (min) และ 1.7976931348623157e+308 (max) (เทียบเท่าข้อมูลชนิด double (64-bit) ในภาษาอื่น)\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.2250738585072014e-308" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.float_info.min" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.7976931348623157e+308" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.float_info.max" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราจะลองทำให้เกิดการ Overflow ในไพธอน ด้วยการบวก 1 เข้าไปยังค่า max" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.7976931348623157e+308" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Overflow in Python\n", "sys.float_info.max + 1 # On my computer (64-bit): 1.7976931348623157e+308" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัวเลขไม่เปลี่ยน มีค่าเท่าเดิม? \n", "\n", "เราลองตรวจสอบยืนยันด้วยโอเปอร์เรเตอร์ ```==``` เพื่อดูว่าค่าเท่ากันหรือไม่" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print (sys.float_info.max+1 == sys.float_info.max) # the output will be TRUE (Boolean)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ผลลัพธ์ที่ได้คือ ```True``` นั่นหมายความว่า ซ้าย ```sys.float_info.max``` มีค่าเท่ากับขวา ```sys.float_info.max + 1``` แสดงว่า Python ไม่มีความแม่นยำเพียงพอที่จะเก็บข้อมูลเพิ่ม ทำให้ผลลัพธ์ไม่ถูกต้อง\n", "\n", "แต่ถ้าเราบวกเพิ่มเยอะๆ เช่น" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "inf" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.float_info.max + sys.float_info.max" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "จะทำให้เกิดโอเวอร์โฟลว์ชัดเจน และในกรณีนี้ Python จะส่งค่ากลับเป็น ```inf``` (infinity) ซึ่งเป็นข้อมูลชนิด ```float```\n", "\n", "รายละเอียดของข้อมูลชนิด float(64-bit)\n", "และการ Overflow สามารถอ่านเพิ่มเติมได้ที่ [Double-precision floating-point format](https://www.wikiwand.com/en/Double-precision_floating-point_format) และ [Floating Point Numbers](https://pythonnumericalmethods.berkeley.edu/notebooks/chapter09.02-Floating-Point-Numbers.html) \n" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(sys.float_info.max + sys.float_info.max)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "ใน Python 3 จำนวนจริง (Floating) เป็นอ็อบเจ็กต์ที่มีขีดจำกัด ในขณะที่ จำนวนเต็มเป็นอ็อบเจ็กต์ที่มีขนาดไม่จำกัด) เมื่อขนาดของตัวเลขใหญ่ขึ้น Python จะปรับหน่วยความจำให้เพิ่มขึ้นโดยอัตโนมัติตามต้องการ ซึ่งหมายความว่าเราสามารถคำนวณตัวเลขจำนวนมากๆ ได้ โดยไม่ต้องทำอะไรเพิ่ม และโดยปกติแล้ว การคำนวณจำนวนเต็มในภาษา Python จะไม่เกิด Overflow (การคำนวณจำนวนที่เกินขีดจำกัด) เหมือนภาษาโปรแกรมอื่นๆ หลายภาษา เช่น C, FORTRAN (ที่จำนวนเต็มมีขนาดคงที่ ซึ่งส่วนใหญ่มีขนาด 4 ไบต์ โดยเก็บค่าได้ $2^{32}$ ข้อมูลที่มีค่าต่างกัน) \n", "\n", "**อย่างไรก็ตาม** เราต้องตระหนักถึงการเกิด Overflow เมื่อเราใช้ไลบรารีบางตัว เช่น NumPy Pandas ในการคำนวณ เนื่องจากจำนวนเต็มในไลบรารีเหล่านั้นมีขนาด (จำนวนบิตที่ใช้ในการเก็บข้อมูล) คงที่ (เพื่อให้การคำนวณมีประสิทธิภาพมากขึ้นและเนื่องจากมีรากฐานมาจากภาษา C/C++) [NumPy: ประเภทข้อมูล (Data types) Numpy](https://numpy.org/doc/stable/user/basics.types.html) เช่น `int16` เป็นจำนวนเต็ม 16 บิต โดยเก็บค่าได้ $2^{16}$ ข้อมูลที่มีค่าต่างกัน โดยจำนวนเต็มสามารถกำหนดให้เก็บค่าได้ทั้งจำนวนที่เป็นบวกและลบ (Signed) และเก็บค่าเฉพาะจำนนวนที่เป็นบวก (Unsigned) ยกตัวอย่างเช่น\n", ">> * uint16 (unsigned) มีค่าตั้งแต่ 0 ถึง $2^{16}-1$\n", ">> * int16 (signed) มีค่าตั้งแต่ $-2^{15}$ ถึง $2^{15}-1$ \n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "mNzho_N2PCfo" }, "source": [ "### บูลีน (Boolean)" ] }, { "cell_type": "markdown", "metadata": { "id": "lwEaxzbFPCfo" }, "source": [ "$\\qquad$ บูลีน (Boolean) เป็นชนิดของออบเจกต์ที่สำคัญชนิดหนึ่ง ออบเจกต์ Boolean สามารถมีค่าเป็น ```\"True\"``` หรือ ```\"False\"``` อย่างใดอย่างหนึ่ง" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "id": "pDZ1P03NPCfo" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "True # Value true\n" ] }, { "cell_type": "markdown", "metadata": { "id": "yPPSnuHkPCfo" }, "source": [ "```{attention}\n", "```True``` อักษรตัวแรกใช้ตัวพิมพ์ใหญ่ \"T\" และเช่นกัน ```False``` อักษรตัวแรกใช้ตัวพิมพ์ใหญ่ \"F\"\n", "```" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "id": "G9oCzsgjPCfo" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "False # Value false" ] }, { "cell_type": "markdown", "metadata": { "id": "nJ5Fq0DCPCfo" }, "source": [ "ถ้าลองใช้ฟังก์ชัน ```type()``` แสดงชนิดของออบเจกต์บูลีน จะแสดงเป็น ```bool``` ซึ่งย่อมาจาก _boolean_" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "id": "0FTXRlz_PCfo" }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "type(True) # Type of True\n" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "id": "A-ogk4atPCfo" }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "type(False) # Type of False\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "โดยส่วนใหญ่ จะใช้บูลีนสำหรับการตัดสินแบบมีเงื่อนไข (Conditions) \n", "\n", "เช่น ตัดสินว่า ```-1``` มากกว่า ```3``` หรือไม่" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-1 > 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ได้ผลลลัพธ์เป็นเท็จ (False) เนื่องจาก ```-1``` ไม่มากกว่า ```3``` " ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3**3**3 != (3**3)**3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ได้ผลลลัพธ์เป็นจริง (True) เนื่องจาก `3**3**3` ไม่เท่ากับ `(3**3)**3` จริง (ซ้ายมือมีค่าเท่ากับ `3**(3**3)` $= 3^9$ เนื่องจากการยกกำลังประมวลผลจากขาวไปซ้าย ในขณะที่ขวามือมีค่าเท่ากับ `(3**3)**3` $= 9^3$) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{attention}\n", "การยกกำลังประมวลผลจากขาวไปซ้าย\n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "-UDyvQjvPCfo" }, "source": [ "เราสามารถแปลงออบเจกต์บูลีนไปเป็นออบเจกต์ชนิดอื่นได้ \n", "\n", "หากเราแปลงบูลีนที่มีค่า ```True``` เป็นจำนวนเต็มหรือทศนิยม เราก็จะได้ค่า 1 (หนึ่ง) \n", "หากเราแปลงบูลีนที่มีค่า ```False``` เป็นจำนวนเต็มหรือทศนิยม เราจะได้ 0 (ศูนย์) \n", "\n", "ในทำนองเดียวกัน หากเราแปลง 1 ไปเป็นชนิดบูลีน ค่าที่ได้จะเป็น ```True``` และถ้าเราแปลง 0 ไปที่เป็นชนิดบูลีน ค่าที่ได้จะเป็น ```False```" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "id": "XKEA8BV2PCfp" }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# แปลง True เป็น int\n", "\n", "int(True)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "id": "nRHnsn1IPCfp" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# แปลง 1 เป็น boolean\n", "\n", "bool(1)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "id": "3KDHddt-PCfp" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# แปลง 0 เป็น boolean\n", "\n", "bool(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "แต่ถ้าเป็นเลขค่าอื่นที่ไม่ใช่ ```0``` และ ```1``` จะเป็นอย่างไร?\n", "\n", "เช่น ถ้าเป็นเลข 0.1, -1" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0.1) " ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(-1) " ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "id": "AzozB2qGPCfp" }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# แปลง True เป็น float\n", "\n", "float(True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "ไพธอนจะประเมินผลบูลีนว่าเป็น True หรือ False โดยการเปรียบเทียบกับ 0.0 หากเปรียบเทียบแล้วไม่เท่า Python จะให้ค่าบูลีนเป็น True (หรือ 1) แต่หากเท่า Python จะให้ค่าบูลีนเป็น False (0)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**ตารางสรุปโอเปอร์เรเตอร์เปรียบเทียบ (Comparison operators) ในภาษาไพธอน**\n", "\n", " \n", "| **โอเปอร์เรเตอร์** $\\quad \\quad$ | **ตัวอย่าง** $\\quad$ | **ความหมาย** $\\quad \\quad$ | **ผลลัพธ์** |\n", "|----------------| -------------- | ---------------- | ---------- |\n", "| `\\==` | `a==b` | เท่ากับ | เป็นจริง (True) ก็ต่อเมื่อค่าของ a เท่ากับค่าของ b มิฉะนั้นผลลัพธ์เป็นเท็จ (False)|\n", "| `!=` | `a!=b` | ไม่เท่ากับ | เป็นจริง (True) ก็ต่อเมื่อค่าของ a ไม่เท่ากับค่าของ b มิฉะนั้นผลลัพธ์เป็นเท็จ (False)|\n", "| `<` | `a` | `a>b` | มากกว่า | เป็นจริง (True) ก็ต่อเมื่อค่าของ a มากกว่าค่าของ b มิฉะนั้นผลลัพธ์เป็นเท็จ (False)|\n", "| `\\>=` | `a>=b` | มากกว่าหรือเท่ากับ | เป็นจริง (True) ก็ต่อเมื่อค่าของ a มากกว่าหรือเท่ากับค่าของ b มิฉะนั้นผลลัพธ์เป็นเท็จ (False)|\n", "\n", "src: https://realpython.com/python-operators-expressions/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### สตริง (Strings)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ สตริง (String) หรือ **สายอักขระ** คือ ชนิดของข้อมูลที่เป็น**ข้อความ**หรือ**ตัวอักษร/อักขระ (Character) หลายๆ ตัวเรียงต่อๆ กัน**\n", "\n", "ในภาษาไพธอน จะกำหนดสตริงโดยให้ข้่อความหรือตัวอักษรอยู่ในเครื่องหมายคำพูด single quotes '...' หรือเครื่องหมายคำพูด double quotes \"...\" ซึ่งทั้งสองรูปแบบให้ผลลัพธ์เหมือนกัน\n", "\n", "ดังตัวอย่างต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('spam eggs')" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'spam eggs' == \"spam eggs\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สายอักขระว่าง (Empty string, null string, empty word) เป็นสตริงที่มีเอกลักษณ์เฉพาะ มีขนาดของสตริงเท่ากับศูนย์\n" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('') # Empty string, null string, empty word." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ในกรณีที่ต้องการสร้างสตริงที่มีเครื่องหมาย ```'``` ให้ใช้เครื่องหมาย ```\"\\\"``` (Back slash/Backslash) นำหน้า เช่น" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"I doesn't care\"" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'I doesn\\'t care' # ใช้ '\\' แสดงเครื่องหมายคำพูด single quote..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หรือจะใช้ Double quotes \"...\" คล่อมทั้งข้อความแบบนี้ก็ได้" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"I doesn't care\"" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"I doesn't care\" " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สามารถเชื่อมสตริงด้วยโอเปอเรเตอร์ (Operator) ```+``` เช่น" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"I doesn't care, You don't Care, who cares?\"" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"I doesn't care\" + ', ' + 'You don\\'t Care' + ', ' + 'who cares?'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**สรุป** ชนิดของออบเจกต์ (ข้อมูล) พื้นฐานที่ใช้ได้ในภาษาไพธอน\n", "\n", "\n", "| **ชนิดข้อมูล** **ฟังก์ชันแปลง** | **คำอธิบาย** | **ตัวอย่าง** | **หมายเหตุ** | \n", "| ------------------------ | ---------------- | ---------------- | ---------------- |\n", "| จำนวนเต็ม **int()**      | (จำนวนที่ไม่มีทศนิยม) | `-5`, `0`, `5` | ไม่จำกัดจำนวนหลัก |\n", "| จำนวนจริง **float()** | จำนวนที่มีทศนิยม | `1.0`, $\\qquad \\qquad $ `1.6E-19` | จำกัดจำนวนหลักของเลขนัยสำคัญและเลขชี้กำลัง ตัวเลขที่มีค่าเยอะมากๆ หรือน้อยมากๆ จะแสดงในรูปเลขทศนิยมยกกำลังฐานสิบ เช่น `1.6E-19` (หรือ `1.6e-19`) หมายถึง $1.6 \\times 10^{-19}$ (โดยเลขหลัง E(e) ต้องเป็น int เสมอ) |\n", "| สติง **str()** | ข้อความหรือสายอักขระ | `'aaa101'`, $\\quad$ `\"ภาษาไทย\"` | สตริงถูกคร่อมภายในเครื่องหมายคำพูด single/double quote |\n", "| บูลีน **bool()**| ใช้สำหรับการตัดสินแบบมีเงื่อนไข | `True`, `False` | เป็นคำสงวน (ตัวแรกเป็นตัวพิมพ์ใหญ่) โดยมีค่าคงที่ `True(1)`, `Falase(0)` |" ] }, { "cell_type": "markdown", "metadata": { "id": "Y3cTJXOIPCfl" }, "source": [ "(ch1-typecasting)=\n", "## การแปลงชนิดของออบเจกต์ (Typecasting)" ] }, { "cell_type": "markdown", "metadata": { "id": "TPkkRlyjPCfl" }, "source": [ "$\\qquad$ เราสามารถเปลี่ยนชนิดของออบเจกต์จากชนิดหนึ่งไปเป็นอีกชนิดหนึ่งได้ เราเรียกว่ากระบวนการนี้ว่า **การแปลงชนิดข้อมูล (Type Casting)**\n", "\n", "เราจะลองเปลี่ยนชนิดข้อมูลจากจำนวนเต็ม (int) เป็นจำนวนจริง (float) เช่น จาก 2 เป็น 2.0\n", "\n", "โดยเริ่มจาก ตรวจสอบชนิดของข้อมูลของ ```2``` กันก่อน" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "id": "bB88nmKRPCfm" }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2) # ตรวจสอบชนิดของข้อมูลว่าเป็น int" ] }, { "cell_type": "markdown", "metadata": { "id": "h9l-C6J9PCfm" }, "source": [ "### แปลง Integers เป็น Floats\n", "\n", "$\\qquad$ แปลงจำนวนเต็ม ```2``` ให้เป็นจำนวนจริงหรือเลขทศนิยม โดยใช้ฟังก์ชั่น ```float( )```" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "id": "Ar8RFqBCPCfm" }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(2) # แปลง 2 เป็น float" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ได้ผลลัพธ์เป็น ```2.0``` ซึ่งเป็นข้อมูลชนิด float\n", "\n", "หรือจะทำการแปลงและทำการตรวจสอบพร้อมๆกัน โดยเขียนเป็นบรรทัดเดียว" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "id": "nmbASUcSPCfm" }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(float(2)) # แปลงจำนวนเต็ม 2 เป็น float และตรวจสอบชนิดของข้อมูล" ] }, { "cell_type": "markdown", "metadata": { "id": "BEzsm2eAPCfm" }, "source": [ "การแปลงจำนวนเต็มเป็นจำนวนจริง ผลลัพธ์ที่ได้จะไม่เปลี่ยนค่า ตรวจสอบได้โดยใช้โอเปอร์เรเตอร์ ```==```\n" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 == float(2) # ตรวจสอบว่า 2 มีค่าเท่ากับจำนวนจริง 2.0 หรือไม่" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ผลลัพธ์ที่ได้เป็น ```True``` ยืนยันว่าการเปลี่ยนจาก ```2``` เป็น ```2.0``` มีค่าเท่าเดิม (ชนิดของข้อมูลเปลี่ยน แต่ค่าของของมูลไม่เปลี่ยน) \n", "\n", "**แต่**ถ้าเราแปลงจำนวนจริงเป็นจำนวนเต็ม ข้อมูลบางส่วนอาจสูญหาย (เพราะ int เป็น subset ของ float) ตัวอย่างเช่น หากเราแปลงเลขทศนิยม ```1.1``` หรือ ```1.9``` เป็นจำนวนเต็ม เราจะได้ ```1``` ค่าไม่เท่าเดิม ข้อมูลทศนิยมหายไป!" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "id": "wHRK7aVaPCfm" }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(1.1) # การแปลง 1.1 เป็นจำนวนเต็ม จะทำให้ข้อมูลทศนิยม 0.1 สูญหาย" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(1.9) # การแปลง 1.9 เป็นจำนวนเต็ม จะทำให้ข้อมูลทศนิยม 0.9 สูญหาย" ] }, { "cell_type": "markdown", "metadata": { "id": "BEzsm2eAPCfm" }, "source": [ "```{attention}\n", "\n", "ฟังก์ชั่น ```int()``` จะแปลงจำนวนทศนิยมเป็นจำนวนเต็มโดยการตัดตัวเลขหลังทศนิยมทิ้ง เช่น int(1.1), int(1.9) จะมีค่าเป็น ```1``` ฉะนั้นในกรณีที่ต้องการปัดจำนวนทศนิยมขึ้นหรือปัดลงให้เป็นจำนวนเต็ม ให้ใช้ฟังก์ชั่น ```round()``` เช่น round(1.1) จะถูกปัดลง มีค่าเป็น ```1``` แต่ถ้า round(1.9) จะถูกปัดขึ้น มีค่าเป็น ```2```\n", "``` " ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(1.1)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(1.9)" ] }, { "cell_type": "markdown", "metadata": { "id": "4itY7CuOPCfm" }, "source": [ "### แปลงจากสตริง (strings) เป็นจำนวนเต็ม (integers) หรือจำนวนจริง (floats)" ] }, { "cell_type": "markdown", "metadata": { "id": "L2ua0fQMPCfm" }, "source": [ "$\\qquad$ ถ้าสตริงเป็นตัวเลข เราสามารถแปลงให้เป็น integer ได้โดยใช้ฟังก์ชัน ```int()```\n" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "id": "Ccg_tGKcPCfm" }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "int('1')" ] }, { "cell_type": "markdown", "metadata": { "id": "aBtkQwNqPCfn" }, "source": [ "แต่ถ้าในสตริงไม่ได้เป็นตัวเลขทั้งหมด จะเกิด Error ดังตัวอย่างต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "id": "PhOXfJkKPCfn" }, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: '1 or 2 people'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [65], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m1 or 2 people\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '1 or 2 people'" ] } ], "source": [ "\n", "int('1 or 2 people')" ] }, { "cell_type": "markdown", "metadata": { "id": "zvqkgxN4PCfn" }, "source": [ "แน่นอน ถ้าสตริงเป็นเลขทศนิยม เราก็สามารถแปลงให้เป็นจำนวนจริง (floating number) ได้โดยใช้ฟังก์ชัน ```float()``` \n", "\n", "เช่น แปลงสตริง ```1.2```" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "id": "57rBda_oPCfn" }, "outputs": [ { "data": { "text/plain": [ "1.2" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "float('1.2') # แปลงสตริง \"1.2\" เป็นชนิืด float\n" ] }, { "cell_type": "markdown", "metadata": { "id": "gpFL7kySPCfn" }, "source": [ "```{tip}\n", "สตริง (Strings) จะถูกคร่อมด้วย Single quotes เช่น ```'1.2'``` หรือ Double quotes เช่น ```\"1.2\"``` ก็ได้ แต่ใช้ผสมกันแบบไม่ได้ เช่น ```\"1.2'``` (เกิด Error)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "NRvrAjX4PCfn" }, "source": [ "### แปลงตัวเลขเป็นสตริง\n" ] }, { "cell_type": "markdown", "metadata": { "id": "RWtoT6fzPCfn" }, "source": [ "$\\qquad$ ถ้าเราแปลงสตริงเป็นตัวเลขได้ การแปลงตัวเลขเป็นสตริงก็เป็นเรื่องธรรมดาที่สามารถทำได้เช่นกัน" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "id": "yzCWQ3HFPCfn" }, "outputs": [ { "data": { "text/plain": [ "'1'" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(1) # แปลงจำนวนเต็มเป็น string" ] }, { "cell_type": "markdown", "metadata": { "id": "rcoq_ia1PCfn" }, "source": [ "จะแปลงตัวเลขทศนิยมเป็นสตริงก็ทำได้" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "id": "c6_U_OkAPCfn" }, "outputs": [ { "data": { "text/plain": [ "'1.2'" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(1.2) # แปลงจำนวนจริงเป็น string" ] }, { "cell_type": "markdown", "metadata": { "id": "SxWUWK5cPCfp" }, "source": [ "### [Exercise] : Types\n" ] }, { "cell_type": "markdown", "metadata": { "id": "vNxg-Jm_PCfp" }, "source": [ "```1.``` ผลลัพธ์ของ ```6 / 2``` เป็นข้อมูลชนิดใด?\n", "(เรียกเครื่องหมาย ```/``` ว่า Single slash)\n" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "id": "o0YNMNxQPCfp" }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": { "id": "dya9cWAvPCfq" }, "source": [ "```2.``` ผลลัพธ์ของ ```6 // 2``` เป็นข้อมูลชนิดใด? (เรียกเครื่องหมาย ```//``` ว่า Double slash)\n" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "id": "vZaD9tUrPCfq" }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Gk_o-9PcPCfq" }, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": { "id": "2f54zIaJPCfq" }, "source": [ "## ตัวแปร (Variable) และนิพจน์ (Expression)" ] }, { "cell_type": "markdown", "metadata": { "id": "7U2b_ssNPCfq" }, "source": [ "### นิพจน์ (Expression)" ] }, { "cell_type": "markdown", "metadata": { "id": "apRGcai5PCfq" }, "source": [ "$\\qquad$ **นิพจน์ (Expression)** คือ กลุ่มของข้อมูลที่เขียนในรูปสัญลักษณ์ซึ่งอาจจะอยู่ในรูปของตัวแปร,ค่าคงที่หรือฟังก์ชันเชื่อมต่อกับตัวดำเนินการโดยใช้เครื่องหมายต่างๆ ไม่ว่าจะเป็นเครื่องหมายทางคณิตศาสตร์ (เช่น ```+, -, *, /``` ฯลฯ) เครื่องหมายการเปรียบเทียบ (เช่น ```==, !=, >, <``` ฯลฯ) หรือเครื่องหมายทางตรรกศาสตร์ (เช่น ```and, or, ``` ฯลฯ) **โดยนิพจน์จะมีมีค่าคืนกลับเสมอ** ยกตัวอย่างเช่น การคำนวณทางคณิตศาสตร์พื้นฐาน 43+60+16+41 (มีค่าคืนกลับคือ 160)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "id": "h7rXx70APCfq" }, "outputs": [ { "data": { "text/plain": [ "160" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "43 + 60 + 16 + 41 # นิพจน์ทางคณิตศาสตร์ทำการบวก" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "id": "kOwZEYCiPCfq" }, "outputs": [ { "data": { "text/plain": [ "-10" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "50 - 60 # นิพจน์ทางคณิตศาสตร์ทำการลบ" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "id": "bJD7LRRCPCfr" }, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "5 * 5 # นิพจน์ทางคณิตศาสตร์ทำการคูณ (ใช้เครื่องหมาย * (asterisk/star)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "id": "X926wrjIPCfr" }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "25 / 5 # นิพจน์ทางคณิตศาสตร์ทำการหาร (ใช้เครื่องหมาย / (Single slash))" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "id": "cTgbkeNtPCfr" }, "outputs": [ { "data": { "text/plain": [ "4.166666666666667" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "25 / 6 # นิพจน์ทางคณิตศาสตร์ทำการหาร (ใช้เครื่องหมาย / (Single slash))" ] }, { "cell_type": "markdown", "metadata": { "id": "HGIRVz-GPCfr" }, "source": [ "การหารจำนวนเต็มด้วย Double slash ```//``` จะได้ผลหารที่ถูก***ปัดเศษลง*** เป็นจำนวนเต็มที่ใกล้เคียงที่สุด\n" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "id": "m_JOr7_tPCfr" }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "25 // 5 # นิพจน์ทางคณิตศาสตร์ทำการหารปัดเศษทิ้ง (ใช้เครื่องหมาย // (Double slash))" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "id": "aM72p8GtPCfr" }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "25 // 6 # นิพจน์ทางคณิตศาสตร์ทำการหารปัดเศษทิ้ง" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(-1)//2 # นิพจน์ทางคณิตศาสตร์ทำการหารจำนวนติดลบแบบปัดเศษทิ้ง" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1//(-2) # นิพจน์ทางคณิตศาสตร์ทำการหารจำนวนติดลบแบบปัดเศษทิ้ง" ] }, { "cell_type": "markdown", "metadata": { "id": "mS3gmI07PCfs" }, "source": [ "นิพจน์ทางคณิตศาสตร์ในภาษา Python เป็นไปตามหลักคณิตศาสตร์พื้นฐาน\n" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "id": "-e381K2OPCfs" }, "outputs": [ { "data": { "text/plain": [ "150" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "30 + 2 * 60 # นิพจน์ทางคณิตศาสตร์" ] }, { "cell_type": "markdown", "metadata": { "id": "YmLv907oPCfs" }, "source": [ "เช่นเดียวกับคณิตศาสตร์ นิพจน์ที่อยู่ในวงเล็บมีลำดับความสำคัญมากกว่าการคูณการหาร\n" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "id": "a2u7YGYHPCfs" }, "outputs": [ { "data": { "text/plain": [ "1920" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "(30 + 2) * 60 # นิพจน์ทางคณิตศาสตร์" ] }, { "cell_type": "markdown", "metadata": { "id": "uuh0Hja0PCfr" }, "source": [ "### [Exercise] นิพจน์ (Expression)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```1.``` จงเขียนนิพจน์เพื่อคำนวณว่า ในเวลา 24 ชั่วโมง มีกี่วินาที?\n" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "id": "nCuNPuT0PCfs" }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter" ] }, { "cell_type": "markdown", "metadata": { "id": "EwrF2Hr-PCfs" }, "source": [ "```2.``` จงเขียนนิพจน์เพื่อคำนวณว่า ในเวลา 160 นาที มีกี่ชั่วโมง?\n" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Ev1impJqPCfs" }, "source": [ "### ตัวแปร (Variables)" ] }, { "cell_type": "markdown", "metadata": { "id": "kzK4ClCiPCfs" }, "source": [ "\n", "$\\qquad$ เช่นเดียวกับภาษาคอมพิวเตอร์อื่นๆ เราสามารถเก็บค่าใน _ตัวแปร (Variables)_ เพื่อนำไปใช้ในภายหลังได้ (**ในโปรแกรมคอมพิวเตอร์ เราใช้ตัวแปรเก็บข้อมูล**)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### การกำหนดค่าตัวแปร\n", "\n", "$\\qquad$ คำสั่งในการกำหนดค่า (Assignment Statements) ในภาษาไพธอนจะใช้เครื่องหมาย (ตัวดำเนินการ) ```\"=\"``` เพื่อกำหนดค่าให้กับตัวแปร (Assignment)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "คำสั่ง (Statement) หมายถึง โค้ดที่เขียนขึ้นเพื่อให้โปรแกรมทำงาน ประกอบด้วยตัวแปร หรือนิพจน์ต่างๆ **โดยคำสั่งจะไม่ค่าคืนกลับหลังจากที่คำสั่งทำงานเสร็จสิ้น** (ในขณะที่นิพจน์ (Expression) จะมีค่าคืนกลับ)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "การกำหนดค่าตัวแปรในภาษาไพธอนสามารถกำหนดค่าได้เลย โดยที่ไม่ต้องระบุชนิดของข้อมูล (Data Type) ที่จะเก็บ เนื่องจากไพธอนเป็นภาษา Dynamically typed (ต่างจากภาษา C/C++, Swift ฯลฯ ซึ่งเป็นภาษา Statically typed) ที่ต้องกำหนดชนิดข้อมูล ก่อนเรียกใช้งาน)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "กำหนดค่าให้กับตัวแปร ```x``` ได้ง่ายๆ โดยใช้ตัวดำเนินการ ```=``` ดังนี้" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "id": "Qv3t9YM3PCft" }, "outputs": [], "source": [ "\n", "x = 43 + 60 + 16 + 41" ] }, { "cell_type": "markdown", "metadata": { "id": "IhNt9IDvPCft" }, "source": [ "ถ้าต้องการแสดงค่าตัวแปร ```x``` ใน Notebook ทำได้ง่ายๆ เพรียงแค่พิมพ์ตัวแปรลงในเซลล์ แล้วกดปุ่ม ```Shift+Enter``` จะแสดงค่าตัวแปรที่กำหนดไว้\n", "\n" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "id": "x8kBzyHDPCft" }, "outputs": [ { "data": { "text/plain": [ "160" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัวแปร ```x``` มีค่าเป็น ```int``` คือตัวเลขจำนวนเต็ม" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(x))" ] }, { "cell_type": "markdown", "metadata": { "id": "WD_HiqpyPCft" }, "source": [ "เราสามารถดำเนินการกับตัวแปรและเก็บผลลัพธ์ที่ได้ลงในตัวแปรใหม่ได้อีกด้วย" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "id": "8_n1Fva7PCft" }, "outputs": [ { "data": { "text/plain": [ "2.6666666666666665" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = x / 60\n", "y" ] }, { "cell_type": "markdown", "metadata": { "id": "q_TeLRgOPCft" }, "source": [ "ถ้าเราเซฟค่าลงในตัวแปรเดิมที่มีอยู่ ค่าเดิมจะหายไป จะถูกเซฟทับด้วยค่าใหม่แทน\n" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "id": "mEGBH-fhPCft" }, "outputs": [ { "data": { "text/plain": [ "2.6666666666666665" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = x / 60\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ณ ตอนนี้ ตัวแปร ```x``` มีค่าเป็น 2.6666666666666665 เป็น ```float```" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "**ข้อมูลทุกอย่างในภาษาไพธอนเป็น \"ออบเจ็กต์ (Objects)\"**\n", "\n", "$\\qquad$ ใน __โปรแกรมเชิงวัตถุ (Object-oriented programming; OOP)__ __คลาส (Class)__ เป็นต้นแบบ (Blueprint) หรือแม่แบบ (Template) เพื่อสร้างวัตถุหรือออบเจ็กต์ (วัตถุเสมือน; Objects) ขึ้นมา **ออบเจ็กต์ที่สร้างมาจากคลาสบางครั้งจะเรียกว่าอินสแตนท์ (Instance) ของคลาสนั้นๆ** โดยคลาสหนึ่งสามารถใช้สร้างออบเจ็กต์ได้หลายๆ ออบเจ็กต์ โดยที่แต่ละออบเจ็กต์ต้องมีชื่อที่แตกต่างกันไป (รายละเอียดของคลาและออบเจ็กต์มีอธิบายใน Part 3)\n", "\n", ">> จากตัวอย่างข้างต้น ตัวแปร ```x``` เป็นอินสแตนท์ (Instance) ของคลาส ```float```\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถกำหนดค่าให้กับตัวแปรหลายตัวพร้อมๆ กันได้\n" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "160" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b, c, d = 43, 60, 16, 41\n", "a+b+c+d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "คำสั่ง ```a, b, c, d = 43, 60, 16, 41``` เป็นการกำหนดค่าให้ตัวแปร ตามลำดับของตัวแปร เทียบเท่ากับการรันคำสั่ง ```a = 43; b = 60; c = 16; d = 41```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตรวจสอบดูว่าตัวแปร ```c``` มีค่าเป็น 16 หรือไม่โดยพิมพ์คำสั่ง ```print(c)``` หรือง่ายๆ แค่พิมพ์ ```c```" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "นอกจากนี้ เรายังสามารถกำนดค่าตัวแปรแบบลูกโซ่ (Chained assignment) ได้อีกด้วย \n" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "160" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = y = z = 160\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัวแปร ```x```, ```y``` และ ```z``` มีค่าเป็น 160 " ] }, { "cell_type": "markdown", "metadata": { "id": "d9nrd08mPCft" }, "source": [ "(naming-conventions)=\n", "### หลักในการตั้งชื่อตัวแปร \n", "\n", "$\\qquad$ เราควรตั้งชื่อตัวแปรที่มีความหมาย เพื่อให้ตัวเราเองและคนอื่นๆ สามารถอ่านโค้ดและทำความเข้าใจได้ง่ายขึ้น \n", "\n", "สมมุติว่า เราต้องการรวมเวลาของอัลบัม 3 อัลบัม (หน่วยเป็นนาที) แล้วเก็บค่าไว้ในในตัวแปรตัวหนึ่ง จากนี้นเอาตัวแปรดังกล่าวหารด้วย 60 เพื่อเปลี่ยนเป็นหน่วยชั่วโมง แล้วเก็บค่าไว้ในตัวแปรอีกตัวแปรหนึ่ง เราจะเขียนคำสั่งตามดังนี้\n" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "id": "HO-ol3pZPCft" }, "outputs": [ { "data": { "text/plain": [ "142" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total_min = 43 + 42 + 57 # Total length of albums in minutes\n", "total_min" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "id": "dF5K65ePPCft" }, "outputs": [ { "data": { "text/plain": [ "2.3666666666666667" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total_hours = total_min / 60 # Total length of albums in hours \n", "total_hours" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ชื่อของตัวแปรข้างต้น ```total_min``` และ ```total_hours``` อ่านแล้วเข้าใจได้เลยว่า เป็นตัวแปรที่เก็บค่าของจำนวนนาทีทั้งหมด และจำนวนชั่วโมงทั้งหมด\n", "\n", "\n", "เราลองตั้งชื่อตัวแปรที่มีความหมายอีกตัวหนึ่ง โดยใช้ชื่อ ```and```" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (77897713.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn [95], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m and = 2\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "and = 2\n", "and\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เกิด Error (SyntaxError: invalid syntax) !! เพราะเหตุใด?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**กฎเกณฑ์ในการตั้งชื่อตัวแปร** \n", "\n", "1. ตัวอักษรตัวแรกต้องเป็นภาษาอังกฤษ (A-Z, a-z) ตามด้วยตัวอักษร หรือตัวเลขใดๆ ก็ได้\n", "\n", "2. ชื่อตัวแปรห้ามมีช่องว่าง จุดทศนิยม และสัญลักษณ์พิเศษ ( !, @, #, $, %) ยกเว้น underscore \"_\" เท่านั้น\n", "\n", "3. การใช้อักษรตัวพิมพ์ใหญ่และอักษรพิมพ์เล็กมีความแตกต่างกัน (Case-sensitive)\n", "\n", "4. **ห้าม**ใช้คำสวงน (Reserved word, Keyword) เป็นชื่อตัวแปร เช่น if, for, and, global, yield ... และ **ไม่ควร**ใช้ตั้งชื่อตัวแปรซ้ำกับชื่อฟังก์ชันในไพธอน เช่น int, str, max, sum ...\n", "\n", "5. ชื่อควรสื่อความหมายกับข้อมูลที่จะเก็บ เป็นชื่อที่สามารถอ่านและทำความเข้าใจได้ง่าย\n", "\n", " (อ่านเพิ่มเติมได้ใน [Dmitri Pavlutin: Coding like Shakespeare: Practical Function Naming Conventions](https://dmitripavlutin.com/coding-like-shakespeare-practical-function-naming-conventions/))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note} **PEP8**\n", "\n", "หลักเกณฑ์หรือสไตล์การเขียนโปรแกรม PEP8 แนะนำไม่ให้ใช้ `l` (ตัวแอลพิมพ์เล็ก) ,`O` (ตัวโอพิมพ์ใหญ่), หรือ `I` (ตัวไอพิพม์ใหญ่) เป็นชื่อตัวแปรโดดๆ เพราะว่ามันจะทำให้เราสับสนกับ เลข 0 และ 1 (ถ้าต้องการใช้ตัวแอลให้ใช้ `L` (ตัวแอลพิพม์ใหญ่) แทน)\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2 สไตล์หลักที่นิยมใช้ในการตั้งชื่อตัวแปร: CamelCase (คาเมลเคส) และ snake_case (สเนคเคส)**\n", "\n", "* CamelCase (คาเมลเคส): เป็นการตั้งชื่อตัวแปรโดยเชื่อมคำทั้งหมดเข้าด้วยกัน ไม่มีช่องว่างหรือเครื่องหมายแบ่งคำ แยกแต่ละคำด้วยอักษรพิมพ์ใหญ่ตัวเดียว ทำให้รูปร่างของตัวแปรมีส่วนเว้าส่วนโค้งคล้ายๆ อูฐ เช่น iPhone, eBay, xValue, totalMin, totalHours, phoneNumber (เฉพาะคำแรกเป็นตัวอักษรตัวเล็กทั้งหมด เรียก Lower CamelCase) และ FirstName, LastName, JavaScript (ตัวอักษรตัวแรกของทุกคำเป็นตัวใหญ่ทั้งหมด (ตัวแรกด้วย) เรียก Upper CamelCase) ฯลฯ\n", "* Snake_case (สเนคเคส): เป็นการตั้งชื่อตัวแปรโดยใช้ตัวพิมพ์เล็กทั้งหมดแล้ว\n", "คั่นระหว่างคำขั้นด้วย Underscore ```\"_\"``` ทำให้รูปร่างของตัวแปรมีข่อต่อคล้ายๆ งู เช่น total_min, total_hours, phone_number, c_cpp ฯลฯ\n", "\n", "\n", "อ่านรายละเอียดเพิ่มเติมเกี่ยวกับ CamelCase และ snake_case ได้ที่ [Wiki: Camel case](https://en.wikipedia.org/wiki/Camel_case), [Wiki: Snake case](https://en.wikipedia.org/wiki/Snake_case)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{tip}\n", "\n", "**สไตล์ในการตั้งชื่อ (Naming Styles)**\n", "\n", "— Variable\n", "\n", "ตั้งชื่อตัวแปรเป็นตัวอักษรตัวเดียว คำศัพท์ 1 คำ โดยใช้ตัวเล็กทั้งหมด (ไม่ใช้ตัวอักษร `l` ,`o`) แต่ถ้ามีมากกว่าหนึ่งคำให้คั่นด้วย underscores คั่น (snake_case) \n", "ยกตัวอย่างเช่น x, y, i, j, var, my_variable, count \n", "\n", "- Function\n", "\n", "ตั้งชื่อฟังก์ชันเป็นตัวอักษรตัวเล็กหมด แต่ถ้าอยากเพิ่มคำใช้ underscores คั่นระหว่างคำเพื่อให้อ่านง่าน (sname_case) ยกตัวอย่างเช่น function, my_function\n", "\n", "— Class\n", "\n", "ตั้งชื่อคลาสแบบสไตล์ Upper CamelCase โดยขึ้นต้นด้วยตัวใหญ่เสมอ ไม่ใช้ underscore เพื่อให้อ่านง่าย \n", "ยกตัวอย่างเช่น Model, MyClass, ReadFile\n", "\n", "— Method\n", "\n", "หลักในการตั้งชื่อเมธอดจะเหมือนกับฟังก์ชัน กล่าวคือ เป็นตัวอักษรตัวเล็กหมด ถ้าอยากเพิ่มคำให้ใช้สไตล์ snake_case ยกตัวอย่างเช่น method, class_method, read_file, save_file \n", "\n", "— Constant\n", "\n", "ค่าคงที่ ใช้ตัวใหญ่ทุกตัวอักษร แต่ถ้าอยากเพิ่มคำให้ใช้ underscores ขั้นระหว่างคำ ยกตัวอย่างเช่น CONSTANT, MY_CONSTANT\n", "\n", "— Module\n", "\n", "ตั้งชื่อโมดูลเป็นตัวอักษรตัวเล็กหมด แต่ถ้าอยากเพิ่มคำใช้ underscores ขั้นระหว่างคำเพื่อให้อ่านง่าย ยกตัวอย่างเช่น module.py, my_module.py\n", "\n", "— Package\n", "\n", "ตั้งชื่อ Package เป็นตัวอักษรตัวเล็กทั้งหมด และหลีกเลี่ยงการใช้ underscores (ใช้เป็นตัวอักษรตัวเล็กทั้งหมด) ยกตัวอย่างเช่น package, mypackage\n", "\n", "ref: https://realpython.com/python-pep8/#naming-styles\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "เราสามารถแจงคำสงวนใน Python 3 ออกมาโดยใช้ไลบรารี [keyword](https://docs.python.org/3/library/keyword.html) ของ Python 3 ได้ดังนี้" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n" ] } ], "source": [ "import keyword\n", "\n", "print(keyword.kwlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ดังนั้นเราจะใช้คำสงวนเหล่านี้ (รวมถึง ```and``` ในโค้ดก่อนหน้า) เป็นอย่างอื่นไม่ได้ ไม่ว่าจะใช้เป็นชื่อตัวแปร ชื่อฟังก์ชัน หรือชื่อคลาส ก็ไม่ได้ " ] }, { "cell_type": "markdown", "metadata": { "id": "9pl9V-CbPCfu" }, "source": [ "กรณีข้างต้น เป็นการบวกเวลาของอัลบัม 3 อัลบัม (ที่มีหน่วยเป็นนาที) แล้วเก็บค่าไว้ในตัวแปร ```total_min``` หลังจากนั้นหารด้วย 60 เปลี่ยนเป็นหน่วยชั่วโมงและเก็บค่าไว้ในตัวแปร ```total_hours``` (2 คำสั่ง)\n", "\n", "เราสามารถเขียนให้อยู่ในบรรทัดเดียวได้โดยใช้วงเล็บ (คำสั่งเดียว) ดังนี้\n" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "id": "woX0Rl0JPCfu" }, "outputs": [ { "data": { "text/plain": [ "2.3666666666666667" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "total_hours = (43 + 42 + 57) / 60 # Total hours in a single expression\n", "total_hours" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ได้ ```total_hours``` เป็นเลขจำนวนชั่วโมงที่เป็นจำนวนทศนิยม" ] }, { "cell_type": "markdown", "metadata": { "id": "ZFMwViviPCfu" }, "source": [ "แต่ถ้าต้องการให้ ```total_hours``` เป็นเลขจำนนวนเต็ม ก็ใช้ ```//``` (ตัวดำเนินการหารปัดเศษทิ้ง)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total_hours = (43 + 42 + 57) // 60\n", "total_hours" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Augmented assignment statement\n", "\n", "$\\qquad$ นอกจากโอเปอเรเตอร์กำหนดค่า (Assignment operator) ```\"=\"``` ที่กล่าวแล้วยังมีโอเปอเรเตอร์กำหนดค่าอีกประเภทที่ประกอบด้วยเครื่องหมาย ```+, -, *, /, %, //, **, ฯลฯ``` รวมกับเครื่องหมาย ```=``` เป็น **Augmented assignment operator** (ตัวดำเนินการที่รวมระหว่างตัวดำเนินการทางคณิตศาสตร์และตัวดำเนินการกำหนดค่า) เช่น\n", "\n", "```+=, -=, *=, /=, %=, //=, **=``` เป็นต้น (ต้องเขียนติดกัน ห้ามมีช่องว่าง)\n", "\n", "ยกตัวอย่างเช่น ตัวดำเนินการ ```+=``` ในภาษาไพธอน เป็นการบวกค่าของสองค่า (ซ้ายและขวาของตัวดำเนินการ) เข้าด้วยกัน และนำผลผลลัพธ์ไปกำหนดค่าใหม่ให้กับตัวแปรฝั่งซ้ายของตัวดำเนินการ\n", "\n", "เมื่อนำไปกำหนดค่าตัวแปร หรือนิพจน์ต่างๆ เราจะเรียกว่า \"augmented assignment statement\" เช่น คำสั่ง\n", "```total_mins += 5``` ในโค้ดต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "75" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total_mins = 70 \n", "total_mins += 5 # total_mins = total_mins + 5\n", "total_mins" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "คำสั่ง ```total_mins += 5``` เป็นการบวกค่าในตัวแปร ```total_mins``` กับ 5 แล้วนำผลลัพธ์ไปเก็บไว้ในตัวแปร ```total_mins``` มีค่าเหมือนกับ ```total_mins``` = ```total_mins``` + 5 " ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total_mins = 70\n", "total_mins *= 2\n", "total_mins" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เช่นเดียวกัน คำสั่งข้างต้นเป็นการคูณค่าในตัวแปร ```total_mins``` กับ 2 แล้วนำผลลัพธ์ไปเก็บไว้ในตัวแปร ```total_mins``` มีค่าเหมือนกับ ```total_mins``` = ```total_mins``` * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**สรุป** ตารางสรุปตัวดำเนินการกำหนดค่า (Assignment operator) ใน Python\n", "\n", "| **โอเปอร์เรเตอร์** | **ตัวอย่าง** | **ความหมาย** |\n", "| --------- | -------- | ---------- |\n", "| ```=``` | ```a = b``` | ```a = b``` |\n", "| ```+=``` | ```a += b``` | ```a = a + b``` |\n", "| ```-=``` | ```a -= b``` | ```a = a - b``` |\n", "| ```*``` | ```a *= b``` | ```a = a * b``` |\n", "| ```/=``` | ```a /= b``` | ```a = a / b``` |\n", "| ```%=``` | ```a %= b``` | ```a = a % b``` |\n", "| ```//=``` | ```a //= b``` | ```a = a // b``` |\n", "| ```**/=``` | ```a **= b``` | ```a = a ** b``` |\n", "| ```&=``` | ```a &= b``` | ```a = a & b``` |\n", "| ```\\|=``` | ```a \\|= b``` | ```a = a \\| b``` |\n", "| ```^=``` | ```a ^= b``` | ```a = a ^ b``` |\n", "| ```>>=``` | ```a >>= b``` | ```a = a >> b``` |\n", "| ```<<=``` | ```a <<= b``` | ```a = a << b``` |\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{attention}\n", "**Augmented assignment operator** เป็นตัวดำเนินการที่รวมระหว่างตัวดำเนินการทางคณิตศาสตร์กับตัวดำเนินการกำหนดค่า ```=``` เข้าด้วยกัน เช่น ```+``` รวมกับ ```=``` เป็น ```+=``` โดยเขียนติดกัน ห้ามมีช่องว่าง และห้ามสลับที่กัน!!!\n", "```" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ " x = 10\n", " x += 5\n", " x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "คำสั่ง ```x += 5``` เป็นการบวกค่าในตัวแปร ```x``` กับ 5 แล้วนำผลลัพธ์ไปเก็บไว้ในตัวแปร ```x``` แต่คำสั่ง ```x =+ 5``` ..." ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 10\n", "x =+ 5\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เป็นการกำหนด ```+5``` ให้กับตัวแปร ```x``` !!!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note} **PEP8**\n", "หลักเกณฑ์หรือสไตล์การเขียนโปรแกรม PEP8 แนะนำให้เว้นช่องว่างทั้งก่อนและหลังตัวดำเนินการ Assignment (=), Augmented assignment (+=, -= ฯลฯ), Comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "hHMFioTHPCfu" }, "source": [ "## [Exercise] นิพจน์ (Expressions) & ตัวแปร (Variables) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```1.``` ค่า ```a``` จะมีค่าเท่าไหร่? ถ้า ```a = 4+100//3**2%10```" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```2.``` ค่า ```b``` จะมีค่าเท่าไหร่? ถ้า\n", "\n", "```\n", "b = 22//3/2+2\n", "```\n" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```3.``` รันต่อจากข้อก่อนหน้า\n", "ค่า ```b``` จะมีค่าเท่าไหร่? ถ้า ```b /= 2```" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{attention}\n", " \n", "* ต้องเข้าใจลำดับการรทำงานของตัวดำเนินการ (operator) ให้ดี __(จำลำดับความสำคัญ (สูง $\\rightarrow$ต่ำ): วงเล็บ( ) $\\rightarrow$ ```**``` $\\rightarrow$ ```*```, ```/```, ```//```, % $\\rightarrow$ ```+, -``` ตามลำดับ)__\n", "\n", "* ถ้าลำดับความสำคัญเท่ากัน จะทำจากซ้ายไปขวาเสมอ เช่น ```x = y/2*z``` จะได้ ```x = (y/2) * z``` (ยกเว้น เครื่องหมายยกกำลัง ```**``` จะทำจากขวาไปซ้าย)\n", "* ถ้าต้องการให้ชัวร์ ใส่วงเล็บ เช่น ```b = (22//3)/2+(2**(1**2))```\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "id": "gyr2502nPCfu" }, "source": [ "```4.``` ค่า ```x``` จะมีค่าเท่าไหร่? ถ้า ```x = 3 + 2 * 2```\n" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "id": "e_0h_--hPCfu" }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": { "id": "OXm8Hy-ePCfu" }, "source": [ "```5.``` ค่า ```y``` จะมีค่าเท่าไหร่? ถ้า ```y = (3 + 2) * 2```\n" ] }, { "cell_type": "code", "execution_count": 107, "metadata": { "id": "mh21ivw5PCfu" }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": { "id": "9UVHOzkEPCfv" }, "source": [ "```6.``` ค่า ```z``` จะมีค่าเท่าไหร่? ถ้า ```z = x + y```\n" ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "id": "vzN6l8ADPCfv" }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```7.``` ค่า ```x``` หลังรันโด้ตต่อไปนี้จะมีค่าเท่าไหร่?\n", "```\n", "x = 2 \n", "x += 1 \n", "x *= 2 \n", "```" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```8.``` คำนวณค่า Body Mass Index หรือ BMI (ค่าความหนาของร่างกาย ใช้เป็นมาตรฐานในการประเมินภาวะอ้วนหรือผอมในผู้ใหญ่ตั้งแต่อายุ 20 ปีขึ้นไป) ซึ่งคำนวณได้จาก การใช้น้ำหนักตัว (w) เป็นกิโลกรัมและหารด้วยส่วนสูง (h) ที่วัดเป็นเมตรยกกำลังสอง \n", "\n", " $$ BMI = \\frac{w}{h^2} $$\n", "\n", "จงหาค่า BMI ในกรณีของนาย รักดี ขยันเรียน ซึ่งสูง 177 cm มีน้ำหนัก 100 kg \n", "\n", "รูปร่างสมส่วน ค่า BMI จะอยู่ระหว่าง 18.5~22.9 (ดูรายละเอียดได้ที่ลิงค์ [ร.พ. บางปะกอก - วีธีคำนวณ ดัชนีมวลกาย (BMI)](https://www.bangpakokhospital.com/care_blog/content/วีธีคำนวณดัชนีมวลกายBMI))" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(ch1_printFunction)=\n", "## การใช้ฟังก์ชัน print( ) แสดงผลของตัวแปร (นิพจน์)\n", "\n", "$\\qquad$ เราสามารถใช้ฟังก์ชัน ```print( )``` แสดงผลของตัวแปรได้หลากหลายวิธี \n", "เช่น" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**วิธีที่ 1 แปลงเป็นสตริง (Typecasting)**\n", "\n", "วิธีนี้เป็นการแปลงตัวแปรที่เป็นตัวเลขให้เป็น**สตริง** แล้วเชื่อมแต่ละสตริงด้วยเครื่องหมาย ```\"+\"```\n", "\n", "การแปลงเป็นสตริงสามารถทำได้หลายวิธี เช่น ใช้ฟังก์ชัน ```str()``` ดังตัวอย่างต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 111, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours: 2.3666666666666667 (142 min)\n" ] } ], "source": [ "total_mins = 43 + 42 + 57 # 142 minutes\n", "total_hours = total_mins / 60 # 2.3666666666666667 hours\n", " \n", "print('Total hours: ' + str(total_hours) + ' (' + str(total_mins) + ' min)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัวอย่างข้างต้นนี้มีสตริงทั้งหมด 5 สตริง คือ ```\"Total hours: \"```, ```str(total_hours)```, ```\" (\"```, ```str(total_min)``` และ ```\"min)\"``` เชื่อมต่อเข้าด้วยกันด้วยเครื่องหมาย ```\"+\"```\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หรือจะแปลงเป็นสตริงด้วยโอเปอร์เรเตอร์ ```%``` (%-formatting เป็นรูปแบบการเขียนในยุคแรกๆ ของไพธอน) โดยใช้ %s แทนในข้อความ (s ณ ที่นี้ หมายถึงสตริง (String)) เพื่อบอกไพธอนว่าจะแทนค่าของตัวแปรไว้ตรงไหน) และตามด้วยตัวแปรที่ต้องการนำไปแทนค่า ดังตัวอย่างต่อไปนี้\n" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours: 2.3666666666666667\n" ] } ], "source": [ "print('Total hours: %s' %total_hours)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ในกรณีที่มีตัวแปรมากกว่าหนึ่งตัว ก็สามารถเขียนแบบนี้ได้ โดยใช้วงเล็บ (เป็นข้อมูลชนิด Tuple รายละเอียดจะอธิบายภายหลัง)" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours: 2.3666666666666667 (142 min)\n" ] } ], "source": [ "print('Total hours: %s (%s min)' %(total_hours, total_mins))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ข้อเสียของวิธีนี้คือ อ่านยาก โดยเฉพาะเมื่อมีการแทนค่าหลายจำนวน (ต้องใช้ %s ทั้งหมด) และไม่สามารถแทนค่าด้วยค่าเดิมซ้ำๆ ได้\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**วิธีที่ 2 ส่งผ่านค่า (อาร์กิวเมนต์) ไปยังพารามิเตอร์ของฟังก์ชัน**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "วิธีนี้เป็นการส่งทั้งสตริงและตัวแปรต่างๆ ผ่านอาร์กิวเมนต์ให้ฟังก์ชัน**ตามลำดับ**โดยคั่นด้วยเครื่องหมาย ```,```" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours: 2.3666666666666667 ( 142 min)\n" ] } ], "source": [ "# ส่งผ่านค่าไปยังพารามิเตอร์ของฟังก์ชัน\n", "print(\"Total hours:\", total_hours, \"(\", total_mins, \"min)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัวอย่างข้างต้นเป็นการส่ง 5 อาร์กิวเมนต์ไปยังฟังก์ชัน print() โดยมี \"Total hours:\", \"(\", และ \"min)\" เป็นข้อมูลสตริง ส่วน total_hours และ total_min เป็นตัวแปร)\n", "\n", "การแสดงผลในกรณีนี้ จะเรียงจามลำดับตามลำดับของอาร์กิวเมนต์ที่ส่งไปยังฟังก์ชัน และ**จะคั่นแต่ละอาร์กิวเมนต์ด้วย ช่องว่าง (space) 1 ช่องเสมอ (โดยดีฟอลต์ (Default))** ถ้าหากไม่ต้องการให้มีช่องว่างคั่นหรือต้องการคั่นเป็นตัวอักษรอื่น ก็สามารถทำได้โดยกำหนดที่พารามิเตอร์ ```sep``` ดังตัวอย่าง" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours:2.3666666666666667(142min)\n" ] } ], "source": [ "# Just pass the values as parameters\n", "print(\"Total hours:\", total_hours, \"(\", total_min, \"min)\", sep='')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ข้างต้นเป็นการกำหนดพารามิเตอร์ ```sep``` ให้เป็น \"\" หรืออักขระว่าง (Empty string, null string, empty word) ซึ่งจะทำให้แสดงผลแต่ละอาร์กิวเมนต์\n", "ติดกันโดยไม่มีช่องว่างคั่น" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**วิธีที่ 3 ใช้เมธอด ```str.format( )```**\n", "\n", "วิธีนี้เป็นการจัดสตริงรูปแบบใหม่โดยใช้เมธอด ```format()``` สามารถใช้กับตัวแปรที่เป็นตัวเลขหรือตัวอักษรก็ได้ โดยใช้วงเล็บปีกกา ```{}``` ระบุตำแหน่งของตัวแปร" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours: 2.3666666666666667 (142 min)\n" ] } ], "source": [ "# ใช้เมธอดสตริง str.format()\n", "print(\"Total hours: {} ({} min)\".format(total_hours, total_mins))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "วิธีนี้สามารถใช้ร่วมกับตัวเลขดัชนี (Index) ทำให้เราสามารถระบุลำดับของตัวแปรที่จะนำไปแทนค่าได้ " ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours: 2.3666666666666667 (142 min)\n", "Total min: 142 (2.3666666666666667 hrs)\n" ] } ], "source": [ "# ความสามารถพิเศษของวิธีนี้\n", "# สามารถใช้ร่วมกับเลขดัชนี (Index) ได้\n", "# (ทำให้สะดวกต่อการจัดเรียงลำดับใหม่หรือการพิมพ์ซ้ำหลายครั้ง)\n", "\n", "print(\"Total hours: {0} ({1} min)\".format(total_hours, total_mins))\n", "print(\"Total min: {1} ({0} hrs)\".format(total_hours, total_mins))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หรือจะทำข้อมูลเป็น Dictionary เพื่อให้เรียก Key ชื่อนั้นๆ ได้เลย เช่น" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours: 2.3666666666666667 (142 min)\n" ] } ], "source": [ "print(\"Total hours: {x} ({y} min)\".format(x=total_hours, y=total_min))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(รายละเอียดของข้อมูลชนิด Dictionary จะอธิบายภายหลัง ณ ตอนนี้ จำรูปแบบการใช้งานไปก่อน)\n", "\n", "นอกจาก วิธีนี้จะสามารถจัดเรียงลำดับใหม่ได้โดยใช้เลขดัชนี้แล้ว ยังสามารถแทนค่าด้วยค่าเดิมซ้ำๆ ได้อีกด้วย" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours: 2.3666666666666667 (142 min) (2.3666666666666667 h = 142 min)\n" ] } ], "source": [ "print(\"Total hours: {x} ({y} min) ({x} h = {y} min)\".format(x=total_hours, y=total_mins))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**วิธีที่ 4 เขียนในรูปแบบ f-string (f: formated)**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ไพธอนตั้งแต่เวอร์ชัน 3.6 เป็นต้นไป จะสามารถใช้วิธี \"f-strings\" หรือ \"formated string literals\" ได้ ซึ่งทำให้การเพิ่มตัวแปรให้กับสตริงทำได้ง่ายขึ้นอย่างมาก\n", "\n", "รูปแบบ f-string จะต้องเติมคำนำหน้าสตริงด้วยอักขระ ```f``` หรือ ```F``` ก่อนเครื่องหมายคำพูด เช่น f'สายอักขระ', F\"สายอักขระอีกสาย\"" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total hours: 2.3666666666666667 (142 min)\n" ] } ], "source": [ "# ใช้รูปแบบ f-string ใน Python 3.6 ขึ้นไป\n", "print(f\"Total hours: {total_hours} ({total_mins} min)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หากมีการระบุ \"=\" ไว้ที่จุดสิ้นสุดของนิพจน์ (expression) ที่เป็น f-string ไพธอนจะประเมินนิพจน์เป็นข้อความ (ตัวอักษร) ตามด้วยเครื่องหมาย '=' และตามด้วยค่าของนิพจน์\n", "ฉะนั้นคำสั่งข้างต้น สามารถเขียนใหม่ได้ดังนี้" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "total_hours=2.3666666666666667 (total_mins=142)\n" ] } ], "source": [ "# หรือจะเขียน f-string ในรูปแบบ f'{expr=}' แบบนี้ก็ได้\n", "print(f'{total_hours=} ({total_mins=})')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "การเขียน f-string ในรูปแบบ ```f'{expr=}'``` มักถูกใช้ในการตรวจสอบข้อผิดผลาดของโปรแกรม (debugging) เช่น\n", "\n", ">>> ```x = 5```\n", ">>> ```print(f'{x * 9 + 15 = }')```\n", "\n", ">>> [Output] x * 9 + 15 = 60\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "วิธีที่ 4 เป็นวิธีเขียนที่เขียนง่าย อ่านง่าย และประมวลผลได้เร็วกว่าการเขียนวิธีอื่นๆ (ชื่อเรียก **f-string** f ย่อมาจาก \"fast\" และ \"formated string literals\" อ่านรายละเอียดเพิ่มเติมได้ที่ลิงค์ [The Python Tutorial](https://docs.python.org/3/tutorial/inputoutput.html\n", "))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Exercise] print() Function \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```1.``` จงเขียนโค้ดกำหนดตัวแปร 3 ตัวแปร เก็บข้อมูลชื่อ-นามสกุล ส่วนสูงและน้ำหนัก คำนวณค่า BMI จากส่วนสูงและน้ำหนัก แล้วแสดงผลออกหน้าจอตามตัวอย่างต่อไปนี้\n", "\n", "```\n", "นายรักดี ขยันเรียน น้ำหนัก (w) 100 kg ส่วนสูง (h) 177 cm ค่า BMI ที่ได้ 31.91930798940279\n", "``` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```1.1)``` แสดงผลโดยใช้ฟังก์ชัน ```print()``` วิธีที่ 1 แปลงเป็นสตริง (Typecasting)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```1.2)``` แสดงผลโดยใช้ฟังก์ชัน ```print()``` วิธีที่ 2 ส่งผ่านค่า (อาร์กิวเมนต์) ไปยังพารามิเตอร์ของฟังก์ชัน\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```1.3)``` แสดงผลโดยใช้ฟังก์ชัน ```print()``` วิธีที่ 3 ใช้เมธอด ```str.format( )```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```1.4)``` แสดงผลโดยใช้ฟังก์ชัน ```print()``` วิธีที่ 4 เขียนในรูปแบบ f-string (f: formated)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ฟังก์ชันในภาษาไพธอนเบื้องต้น\n", "\n", "$\\qquad$ การคำนวณ BMI ในแบบฝึกหัดก่อนหน้านี้ เราต้องคีย์นิพจน์ BMI ทุกครั้งที่มีการเปลี่ยนค่าของตัวแปร ซึ่งเป็นเรื่องที่น่าเบื่อ $\\ddot\\frown$\n", "\n", "เราลองมาสร้างฟังก์ชัน bmi ที่คำนวณค่า BMI ด้วยส่วนสูงและน้ำหนักตัวกัน การใช้ฟังก์ชันมีประโยชน์เนื่องจากช่วยให้เราไม่ต้องพิมพ์นิพจน์ BMI ซ้ำๆ\n" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "31.91930798940279" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def bmi(height, weight):\n", " return weight / (height/100.0) ** 2\n", "bmi(177, 100.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "การประกาศฟังก์ชันในภาษาไพธอนมีรูปแบบง่ายๆ ดังนี้ \n", "\n", "**Function syntax:**\n", "```\n", "def ([]):\n", " \n", " .\n", " \n", " return \n", "```\n", "\n", "โดยใช้คำสั่ง def ต่อด้วยชื่อของฟังก์ชัน และกำหนดพารามิเตอร์ภายในวงเล็บ ```()``` พารามิเตอร์คือ ตัวแปรที่ไว้รับค่าของฟังก์ชัน (มีชื่อเรียกอีกอย่างคือ อาร์กิวเมนต์)\n", "```\n", "def ([]):\n", "```\n", "\n", "หลังเครื่องหมาย colon (```:```) จะเป็นบล็อกคำสั่งของฟังก์ชัน\n", "\n", "```\n", "return \n", "```\n", "\n", "คำสั่ง ```return``` เป็นการออกจากฟังก์ชัน โดยจะส่งค่ากลับไปยังจุดที่เรียก\n", "\n", "(รายละเอียดของฟังก์ชันจะมีกล่าวในภายหลัง)" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25.925925925925924" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bmi(180.0,84.0)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28.51851851851852" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1.1*bmi(180.0,84.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Author\n", "\n", "S.C.\n", "\n", "### Change Log\n", "\n", " \n", "| Date | Version | Change Description |\n", "|---|---|---|\n", "| 08-08-2021 | 0.1 | First edition |\n", "| 05-05-2022 | 0.2 | Fix the typo, minor change |\n", "| | | |\n", "| | | |\n" ] } ], "metadata": { "colab": { "collapsed_sections": [ "77G0KQCTPCfv" ], "name": "Ch1_1-1_Write_your_first_python_code.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel(3.8.13))", "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.8.13" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": true, "latex_user_defs": true, "report_style_numbering": false, "user_envs_cfg": true }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "248.375px" }, "toc_section_display": true, "toc_window_display": false }, "vscode": { "interpreter": { "hash": "e921dbf9f2d20a8528fa054ff97aae3eb23f1abc9a6f29b7bcee4307a5b88bad" } } }, "nbformat": 4, "nbformat_minor": 4 }