Skip to content

Instantly share code, notes, and snippets.

@phwt
Last active June 26, 2019 13:03
Show Gist options
  • Save phwt/4749e6cc67f26122db020742e3324d5c to your computer and use it in GitHub Desktop.
Save phwt/4749e6cc67f26122db020742e3324d5c to your computer and use it in GitHub Desktop.
Python-T1
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# Function\n\nFunction คือกลุ่มของโค้ดที่จะทำงานเฉพาะตอนที่เรียกใช้งานเท่านั้น โดยสามารถส่งข้อมูลเข้า Funcition ได้โดยใช้พารามิเตอร์(Parameters/Params) และสามารถส่งข้อมูลออกได้เช่นกัน\n\nในการประกาศ Function จะใช้คำสั่ง `def` ในการสร้างดังตัวอย่างด้านล่าง\n\n**ตัวอย่าง Function:**"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def say_hello():\n print(\"Hello\")\nsay_hello()",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "Hello\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากตัวอย่างด้านบนใน Function `say_hello()` เรากำหนดให้ทำการ `print(\"Hello\")` ดังนั้นเมื่อมีการเรียกใช้งาน Function `say_hello()` ข้อความที่จะได้รับก็คือ \"Hello\"\n\n**หมายเหตุ** - Function จำเป็นต้องมี Docstring เพื่ออธิบายจุดประสงค์ของตัวมัน โดยหากไม่มี Pylint จะแจ้งว่า `Missing function docstring (C0111)`\n\n--------\n## Parameters\nพารามิเตอร์เป็นตัวกลางในการส่งข้อมูลเข้าไปยัง Function การสร้างพารามิเตอร์สามารถทำได้โดยการใส่เข้าไปในวงเล็บด้านหลังของชื่อ Function ดังนี้ `def myfunction(param1, param2)`\n\n**ตัวอย่างการใช้งาน Parameter ในการส่งข้อมูลเข้าไปยัง Function**"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def plus_one(number):\n print(number + 1)\nplus_one(9)",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "10\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากตัวอย่างด้านบน Function `plus_one()` ได้สั่งให้รับค่าที่ชื่อว่า `number` โดยกำหนดให้ตัว Function นำค่า `number` ไปบวก 1 และแสดงค่านั้นออกมา\nโดยหากเราเรียกใช้งาน Function `plus_one()` โดยให้ค่า `number` เป็น 8 (`plus_one(8)`) ตัวเลขที่เราจะได้รับก็จะเป็น 9\n\n**หมายเหตุ** - หากมีการใส่พารามิเตอร์เกิน 5 ตัว Pylint จะแจ้งว่า `Too many arguments`\n\n### การตั้งค่า Default Parameter\nDefault Parameter เป็นการกำหนดค่าตั้งต้นให้พารามิเตอร์นั้นๆหากไม่ได้มีการใส่ค่าใดๆเข้ามาดังตัวอย่าง"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def enter_something(text=\"Nothing\"):\n print(text + \" is entered\")\nenter_something(\"Hello\")\nenter_something(\"There!\")\nenter_something()",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "Hello is entered\nThere! is entered\nNothing is entered\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากตัวอย่างด้านบน `enter_something()` กำหนดให้รับค่า `txt` เข้ามาโดยให้มีค่า Default เป็น \"Nothing\" โดยกำหนดให้นำ `txt` ไปรวมกับ `\" is entered\"` และแสดงค่าออกมา\n\nสองรอบแรกได้ใส่ค่าว่า \"Hello\" และ \"There!\" ตามลำดับ จึงทำให้ค่าที่ออกมาเป็น \"Hello is entered\" และ \"There! is entered\" ตามลำดับ แต่ในรอบสุดท้ายนั้นไม่ได้ใส่ค่าอะไรเข้าไปเลย `enter_something()` จึงทำให้ค่าของ txt เป็น \"Nothing\" ตามที่ได้ตั้งไว้และทำให้ค่าออกมาเป็น \"Nothing is entered\"\n\n------\n## Return\n\nFunction สามารถคืนค่าได้เช่นกัน สามารถทำได้โดยใช้คำสั่ง `return` ดังตัวอย่าง"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def timestwo(num):\n return num*2\ntimestwo(5)",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "10"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากตัวอย่างด้านบน `timestwo()` รับค่า `num` เข้าไปและคืนค่ามาด้วยการเอา `num` ไปคูณ 2 เมื่อเราใส่เลข 7 เข้าไปค่าที่จะได้ก็จะเป็น 14\n\nโดยการใช้ `return` นั้นเปรียบเสมือนเป็นการจบ Function ทันทีคำสั่งใดๆที่อยู่ต่อจาก return จะไม่ทำงานทั้งหมดดังตัวอย่าง"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def count_three():\n print(1)\n print(2)\n print(3)\n return\n print(4) #จะไม่ถูกปริ้นออกมา เพราะอยู่หลัง return\ncount_three()",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "1\n2\n3\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากตัวอย่างด้านบน `print(4)` อยู่ถัดจาก `return` ซึ่งเป็นการจบการทำงาน Function แล้วจึงทำให้ไม่มีการแสดงค่า 4 ออกมา\n\n**หมายเหตุ** - Pylint จะแจ้งว่า `Unreachable code (W0101)` เนื่องจากตัว `print(4)` นั้นจะไม่ทำงานไม่ว่าในกรณีใดๆก็ตาม\n\n-----\n# Lambda\nLambda คือ Function รูปแบบย่อ โดยสามารถรับ Argument ได้และคืนค่าออกมาได้ดังตัวอย่างด้านล่าง"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "plusone_l = lambda i: i + 1 #Lambda Version\ndef plusone_f(i):\n \"\"\" def version \"\"\"\n return i + 1\nprint(plusone_l(10))\nprint(plusone_f(10))",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "11\n11\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากตัวอย่างด้านบนจะเห็นได้ว่าทั้งสองตัวนั้นให้ค่าออกมาเท่ากัน แต่การใช้ `lambda` นั้นจะสั้นว่า\n\nข้อจำกัดของการใช้ `lambda` นั้นคือทุกอย่างต้องทำในบรรทัดเดียวเท่านั้นและสามารถคืนค่า(return)ได้อย่างเดียว การกระทำเช่นการเปลี่ยนค่าตัวแปรนั้นจะทำไม่ได้ใน `lambda`\n\n-----\n\n# Variable Scope\nScope ของตัวแปรนั้นเป็นตัวกำหนดว่าตัวแปรนั้นถูกเรียกใช้ที่ใดได้บ้าง"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "name = \"John\"\ndef sayjane():\n name = \"Jane\"\n print(name)\nsayjane()\nprint(name)",
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": "Jane\nJohn\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากในตัวอย่างตัวแปร `name` ถูกกำหนดให้มีค่าเป็น \"John\" และมี Function `sayjane()` ที่กำหนดให้ `name` มีค่าเป็น \"Jane\" จะแสดงค่านั้นออกมา\n\nจากนั้นได้เรียกใช้งาน `sayjane()` ซึ่งกำหนดค่า `name` ให้เป็น \"Jane\" และแสดงค่าออกมาซึ่งก็แสดงออกมาเป็น \"Jane\" จากนั้นก็ใช้ตำสั่ง `print(name)` เพื่อแสดงค่าของตัวแปร `name` ออกมาซึ่งถูกแสดงออกมาเป็น \"John\"\n\n**ทำไมค่าที่สองถึงถูกแสดงออกมาเป็น \"John\" ทั้งที่ตอนเรียกใช้งาน `sayjane()` ก็ถูกเปลี่ยนค่าไปแล้ว?**\nนั่นก็เป็นเพราะ `name` ในบรรทัดแรกนั้นเป็นคนละตัวกับ `name` ใน `sayjane()` ถึงแม้จะเป็นชื่อเดียวกันก็ตาม ตั้งแต่ตรงนี้ขอเรียก `name` ตัวแรกว่า `name1` และตัวที่สองว่า `name2`\n\nตัว `name2` นั้นได้ถูกประกาศใน `sayjane()` ซึ่งก็จะทำให้ `name2` รู้จักอยู่แค่ใน `sayjane()` เท่านั้น หากไปเรียกใช้งานที่อื่นก็จะไม่รู้จักค่านี้ ซึ่งผิดกับ `name1` ที่ถูกประกาศอยู่ด้านนอก `sayjane()` จึงทำให้รู้จักค่าของตัวนี้ที่อยู่ในระดับเดียวกับมัน"
}
],
"metadata": {
"kernelspec": {
"name": "python36",
"display_name": "Python 3.6",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6",
"file_extension": ".py",
"codemirror_mode": {
"version": 3,
"name": "ipython"
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment