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
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
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# `while`\n\n`while` คือลูปชนิดหนึ่งที่จะทำงานซ้ำๆจนกว่าเงื่อนไขจะไม่เป็น `True`\n\n**หมายเหตุ** - หากใช้เป็น `while True` ตัวลูปนั้นจะทำงานไม่รู้จบจนจนกว่าจะมีการใช้คำสั่ง `break`\n\nตัวอย่างการใช้งาน while loop ในการแสดงคำว่า \"Yes\" 5 ครั้ง"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "i = 1\nwhile i <= 5:\n print(\"Yes\")\n i += 1",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "Yes\nYes\nYes\nYes\nYes\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากตัวอย่างตอนแรกกำหนดใน `i` มีค่าเป็น 1 และตัวลูปให้เงื่อนไขเป็น `i <= 5`\nเมื่อเริ่มการทำงานจะเช็คเงื่อนไขว่าเป็นจริงหรือไม่หากเป็นจริงให้ทำงานต่อ\n* 1 <= 5 หรือไม่? | ใช่ | ทำการรันโค้ดด้านใน (แสดงคำว่า \"Yes\" และเพิ่มค่า i ไปอีก 1 เป็น 2)\n* 2 <= 5 หรือไม่? | ใช่ | ทำการรันโค้ดด้านใน (แสดงคำว่า \"Yes\" และเพิ่มค่า i ไปอีก 1 เป็น 3)\n* 3 <= 5 หรือไม่? | ใช่ | ทำการรันโค้ดด้านใน (แสดงคำว่า \"Yes\" และเพิ่มค่า i ไปอีก 1 เป็น 4)\n* 4 <= 5 หรือไม่? | ใช่ | ทำการรันโค้ดด้านใน (แสดงคำว่า \"Yes\" และเพิ่มค่า i ไปอีก 1 เป็น 5)\n* 5 <= 5 หรือไม่? | ใช่ | ทำการรันโค้ดด้านใน (แสดงคำว่า \"Yes\" และเพิ่มค่า i ไปอีก 1 เป็น 6)\n* 6 <= 5 หรือไม่? | ไม่ | จบลูปทันที และจะไม่มีการรันโค้ดด้านใน\n\nจากด้านบนจะเขียนเป็นตารางได้เป็นดังนี้\n\n| i | i <= 5 |\n|---|--------|\n| 1 | True |\n| 2 | True |\n| 3 | True |\n| 4 | True |\n| 5 | True |\n| 6 | False |\n\nสำหรับค่า `i` ที่เอาไว้ตรวจสอบเงื่อนไขก็สามารถเอามาใช้งานได้เช่นกันดังตัวอย่างนี้ที่จะเป็นการแสดงค่า `i` ออกมา"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "i = 1\nwhile i <= 5:\n print(i)\n i += 1",
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": "1\n2\n3\n4\n5\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "----\n# `for`\n`for` เป็นลูปอีกชนิดหนึ่งที่มีการกำหนดจำนวนครั้งการลูปที่แน่นอน โดยจะเป็นการลูปผ่านตัวอักษร(string), ไอเทมในลิสต์ หรือ ตัวเลขก็ได้\n\n**ตัวอย่างการลูปผ่าน `string`**"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "city = \"Moscow\"\nfor i in city:\n print(i)",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "M\no\ns\nc\no\nw\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "ค่า `i` ในการลูปนั้นจะเป็นตัวอักษรตามตำแหน่งนั้นๆ โดยสั่งให้ทุกครั้งที่ลูปแสดงค่า `i` หรือตัวอักษรนั้นๆ\nจากค่าที่ได้ใส่ไปคือ `city` ซึ่งมีค่าคือ \"Moscow\" เมื่อลูปรอบแรกค่า `i` จะเป็น \"M\" ทำให้แสดงผลเป็น \"M\" ออกมา และ \"o\", \"s\", \"c\", \"o\", \"w\" ไปตามลำดับ\n\n**ตัวอย่างการลูปผ่านลิสต์**"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "cities = [\"Kaliningrad\", \"St.Petersburg\", \"Volgograd\"]\nfor i in cities:\n print(i)",
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": "Kaliningrad\nSt.Petersburg\nVolgograd\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "เหมือนในตัวอย่างของการลูปผ่านตัวอักษร แต่จะเป็นการลูปผ่านไอเทมแต่ละตัวในลิสต์แทน\n\n**ตัวอย่างการลูปผ่านตัวเลขโดยใช้ `range()`**"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for i in range(5):\n print(i)",
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": "0\n1\n2\n3\n4\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "การ `range()` จะเป็นการสั่งให้ลูปเป็นจำนวนกี่ครั้ง โดยการนับจะเริ่มที่ 0 เช่น `range(3)` จะเป็นค่า 0 - 2 ไม่ใช่ 1 - 3\nโดยค่า `i` ที่ได้ก็จะนำมาใช้ได้เหมือนกัน\n\n----\n# การใช้ `break` เพื่อจบลูป\nลูปสามารถถูกสั่งให้จบได้โดยการใ้คำสั่ง `break` โดยเงื่อนไขไม่จำเป็นต้องเป็น False ดังตัวอย่าง"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "while True:\n txt = input()\n if txt == \"Stop\":\n break\n print(\"[Output] You entered: \" + txt)",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Hello There!\n[Output] You entered: Hello There!\nI am the senate!\n[Output] You entered: I am the senate!\nNot Yet!\n[Output] You entered: Not Yet!\nStop\n"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากตัวอย่างด้านบนเรากำหนดให้เงื่อนไขเป็น `True` ซึ่งหมายความว่าตัวลูปนั้นจะลูปไปเรื่อยๆไม่รู้จบและภายในลูปให้รับค่าและเก็บไว้ในตัวแปร `txt` จากนั้นมีเงื่อนไข `if` ตรวจหากว่าค่าที่ป้อนเข้ามาเป็น \"Stop\" ให้ทำการ `break` คำสั่งต่อไปคือให้แสดงผลข้อความ \"[Output] You entered: \" กับค่าของ `txt` ที่ได้ใส่เข้าไป\n\n3 รอบแรกได้ใส่ตัวหนังสือปกติเข้าไปซึ่งไม่เข้าเงื่อนไข \"Stop\" จึงทำให้มีการแสดงค่าออกมาเรื่อยๆ จนกระทั่งบรรทัดสุดท้ายได้ใส่ค่าว่า \"Stop\" เข้าไปทำให้เข้าเงื่อนไขและทำการรันคำสั่ง `break` และลูปจะจบทันที โดยคำสั่งทุกอย่างที่อยู่ถัดไปก็จะไม่ทำงานเช่นกัน ทำให้ไม่มีการแสดงค่า \"[Output] You entered: Stop\" ออกมา\n\n-----\n# การใช้ `continue`\n`continue` เป็นการข้ามการทำงานของลูปแต่จะยังไม่เป็นการจบลูปทันที"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "fruits = [\"banana\", \"apple\", \"melon\", \"grape\"]\nfor i in fruits:\n if i == \"melon\":\n continue\n print(i)",
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": "banana\napple\ngrape\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "จากตัวอย่างด้านบนจะเป็นการใช้ for ลูปผ่านลิสต์ `fruits` และให้แสดงค่าออกมาทุกครั้งที่ลูป แต่ได้ใส่เงื่อนไขไว้ว่าหาก `i` เป็น \"melon\" ให้ทำการ `continue`\n\nเมื่อเริ่มการทำงาจะมีการแสดงค่า \"banana\" และ \"apple\" ตามลำดับปกติเพราะไม่เข้าเงื่อนไข\nแต่พอค่า `i` เป็น \"melon\" ตัวคำสั่ง `continue` จะงานและข้ามคำสั่งทั้งหมดที่อยุ่ถัดไปทั้งหมดซึ่งในที่นี้เป็นการแสดงค่า และลูปต่อ\nหลังจากนั้น `i` เป็น \"grape\" ซึ่งไม่เข้าเงื่อนไขก็จะทำให้มีการแสดงค่าตามปกติ"
}
],
"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.3",
"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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment