{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Coding freedom*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# คำสั่งเงื่อนไขในภาษาไทธอน (Conditions in Python)\n",
"\n",
"**20** minutes\n",
"\n",
"\n",
" **วัตถุประสงค์**\n",
"\n",
"\n",
" หลังจากทำทำแล็บ นศ.จะสามารถ \n",
"\n",
"\n",
" * เขียนคำสั่งเงื่อนไขเพื่อตัดสินใจเลือกทำ (Decision Making) ในภาษา Python (ใช้ตัวดำเนินการเปรียบเทียบ (Comparison Operators), ตัวดำเนินการทางตรรกะ (Logical Operators) และเขียนโค้ดที่มีทางเลือกหลายทาง (if-elif-else)) ได้\n",
" \n",
"\n",
"[Ref:]\n",
"* https://docs.python.org/3/tutorial/controlflow.html#if-statements\n",
"Ref:\n",
"* https://realpython.com/python-conditional-statements/ \n",
"* https://www.tutorialspoint.com/python/python_decision_making.htm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## คำสั่งเงื่อนไข (Condition Statements)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ตัวดำเนินการเปรียบเทียบ (Comparison/Relational Operators)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ตัวดำเนินการเปรียบเทียบจะเปรียบเทียบค่าตามเงื่อนไขโดยให้ผลลัพธ์เป็นบูลีน (Boolean) ตัวดำเนินการเปรียบที่ใช้เปรียบเทียบค่าสองค่ามีดังนี้\n",
"\n",
"**ตัวดำเนินการด้านการเปรียบเทียบ (Comparison operators)**\n",
"\n",
" - equal(Mathematics =) : ==
\n",
" - not equal(Mathematics ≠) : !=
\n",
" - greater than (Mathematics >) : >
\n",
" - less than (Mathematics <) : <
\n",
" - greater than or equal to (Mathematics ≥) : >=
\n",
" - less than or equal to (Mathematics ≤) : <=
\n",
"
\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ลองกำหนดค่าให้ a มีค่าเป็น 5 แล้วใช้ตัวดำเนินการเครื่องหมาย **== (equal operator)** เพื่อพิจารณาว่าค่าทั้งสอง (ซ้ายกับขวา) มีค่าเท่ากันหรือไม่ กรณีด้านล่างเป็นการเปรียบเทียบตัวแปร a กับ 6"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Condition Equal\n",
"\n",
"a = 5\n",
"a == 6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ผลลัพธ์ที่ได้เป็น False เพราะ 5 ไม่เท่ากับ 6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"คำสั่งต่อไปนี้เปรียบเทียบว่า i > 5 หรือไม่ \n",
"ผลลัพธ์จะเป็น True ก็ต่อเมื่อซ้าย (กรณีคือตัวแปร i) มากกว่าขวา (กรณีคือ 5), แต่ถ้าไม่ใช่ ผลลัพธ์จะเป็น False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Greater than Sign\n",
"\n",
"i = 6\n",
"i > 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ผลลัพธ์ข้างต้นจึงเป็น True เนื่องจากกรณีนี้ i มีค่าเท่ากับ 6 ซึ่งมากกว่า 5 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ลองกำหนดใหม่ ให้ i มีค่าเท่ากับ 2 \n",
"ผลลัพธ์ที่ได้เป็น False เนื่องจากกรณีนี้ i มีค่าเท่ากับ 2 ซึ่งน้อยกว่า 5\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Greater than Sign\n",
"\n",
"i = 2\n",
"i > 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ตัวเลขที่มีค่ามากกว่า 5 สีเขียว และที่เหลือสีแดง \n",
"ถ้า i มีค่าอยู่ในขอบเขตสีเขียวผลลัพธ์เป็น **True** แต่ถ้ามีค่าอยู่ในขอบเขตสีแดงผลลัพธ์เป็น **False**. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" **ตัวดำเนินการ != ( Not equal )**\n",
"ใช้ตรวจสอบว่าตัวแปร 2 ตัวมีค่าไม่เท่ากันใช่หรือไม่ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Inequality Sign\n",
"\n",
"i = 2\n",
"i != 6"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Inequality Sign\n",
"\n",
"i = 6\n",
"i != 6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ตัวเลขที่มีค่าเท่ากับ 6 สีแดง (**False**) นอกเหนือจากนั้นสีเขียว (**True**)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ตัวดำเนินการ **!= ( Not equal )** ใช้กับสตริงได้"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use Equality sign to compare the strings\n",
"\n",
"\"Rap Against Dictatorship\" == \"Michael Jackson\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use Inequality sign to compare the strings\n",
"\n",
"\"Rap Against Dictatorship\" != \"Michael Jackson\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**แอสกี หรือ รหัสมาตรฐานของสหรัฐอเมริกา(ASCII: American Standard Code for Information Interchange)**\n",
"\n",
"เป็นรหัสอักขระที่ประกอบด้วยอักษรละติน เลขอารบิก เครื่องหมายวรรคตอน และสัญลักษณ์ต่างๆ โดยแต่ละรหัสจะแทนด้วยตัวอักขระหนึ่งตัว เช่น รหัส 65 (เลขฐานสิบ) หรือ 01000001 (เลขฐานสอง) ใช้แทนอักษรเอ (A) พิมพ์ใหญ่ เป็นต้น\n",
"\n",
"(Source: วิกิพีเดีย https://th.wikipedia.org)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"\n",
" \n",
" | Char. | \n",
" ASCII | \n",
" Char. | \n",
" ASCII | \n",
" Char. | \n",
" ASCII | \n",
" Char. | \n",
" ASCII | \n",
"
\n",
"\n",
"\n",
" \n",
" | A | \n",
" 65 | \n",
" N | \n",
" 78 | \n",
" a | \n",
" 97 | \n",
" n | \n",
" 110 | \n",
"
\n",
" \n",
" | B | \n",
" 66 | \n",
" O | \n",
" 79 | \n",
" b | \n",
" 98 | \n",
" o | \n",
" 111 | \n",
"
\n",
" \n",
" | C | \n",
" 67 | \n",
" P | \n",
" 80 | \n",
" c | \n",
" 99 | \n",
" p | \n",
" 112 | \n",
"
\n",
" \n",
" | D | \n",
" 68 | \n",
" Q | \n",
" 81 | \n",
" d | \n",
" 100 | \n",
" q | \n",
" 113 | \n",
"
\n",
" \n",
" | E | \n",
" 69 | \n",
" R | \n",
" 82 | \n",
" e | \n",
" 101 | \n",
" r | \n",
" 114 | \n",
"
\n",
" \n",
" | F | \n",
" 70 | \n",
" S | \n",
" 83 | \n",
" f | \n",
" 102 | \n",
" s | \n",
" 115 | \n",
"
\n",
" \n",
" | G | \n",
" 71 | \n",
" T | \n",
" 84 | \n",
" g | \n",
" 103 | \n",
" t | \n",
" 116 | \n",
"
\n",
" \n",
" | H | \n",
" 72 | \n",
" U | \n",
" 85 | \n",
" h | \n",
" 104 | \n",
" u | \n",
" 117 | \n",
"
\n",
" \n",
" | I | \n",
" 73 | \n",
" V | \n",
" 86 | \n",
" i | \n",
" 105 | \n",
" v | \n",
" 118 | \n",
"
\n",
" \n",
" | J | \n",
" 74 | \n",
" W | \n",
" 87 | \n",
" j | \n",
" 106 | \n",
" w | \n",
" 119 | \n",
"
\n",
" \n",
" | K | \n",
" 75 | \n",
" X | \n",
" 88 | \n",
" k | \n",
" 107 | \n",
" x | \n",
" 120 | \n",
"
\n",
" \n",
" | L | \n",
" 76 | \n",
" Y | \n",
" 89 | \n",
" l | \n",
" 108 | \n",
" y | \n",
" 121 | \n",
"
\n",
" \n",
" | M | \n",
" 77 | \n",
" Z | \n",
" 90 | \n",
" m | \n",
" 109 | \n",
" z | \n",
" 122 | \n",
"
\n",
"\n",
"
\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Extra: เราสามารถหารหัส ASCII ของอักขระใดๆ ได้\n",
"\n",
"ฟังก์ชัน ord( ) เป็นฟังก์ชัน (Built-in function) ที่เปลี่ยนตัวอักขระให้เป็นเลขจำนวนเต็ม โดยเลขจำนวนเต็มดังกล่าวเป็นค่า \"Unicode\" หรือ \"ASCII\" ของอักษรตัวนั้น (**Unicode code**; ASCII code เป็นซับเซ็ทของ Unicode code) (ord = ordinal of a one character)\n",
"\n",
"และในทางกลับกัน ฟังก์ชัน chr( ) เป็นฟังก์ชัน (Built-in function) ที่เปลี่ยนเลขจำนวนเต็ม (ค่า \"Unicode\" หรือ \"ASCII\") ให้เป็นตัวอักขระ\n",
"\n",
"ตั้งแต่ Python 3.0 ไทธอนเก็บข้อมูลสตริงก์เป็นอักขระยูนิโคด [(Unicode code)](https://th.wikipedia.org/wiki/ยูนิโคด)\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"คอมพิวเตอร์ที่รู้จักเพียง “1” กับ “0”\n",
"\n",
"```{Note}\n",
"**มาตรฐาน ASCII:** คอมพิวเตอร์ในยุคแรกๆ รองรับเพียงตัวอักษรภาษาอังกฤษเป็นหลัก จึงมีมาตรฐาน ASCII เพื่อ “แปลง” (convert) ระหว่างตัวอักขระต่างๆ ให้เป็นรหัสคอมพิวเตอร์ (เนื่องจากคอมฯ รู้จักเพียง “1” กับ “0”) ในยุคนั้น รหัส ASCII มีขนาดเพียง 7 bits ซึ่งรองรับอักขระได้ $2^7 = 128$ อักขระ (ตัวอักษรภาษาอังกฤษตัวพิมพ์ใหญ่และตัวพิมพ์เล็กรวม 52 ตัว, ตัวเลข 10 ตัว, สัญลักษณ์พิเศษและคำสั่งพิเศษอีกหลักสิบตัว) \n",
"\n",
"แต่ในยุคปัจจุบัน การใช้งานคอมพิวเตอร์ขยายตัวเพิ่มมากขึ้นและมีการเชื่อมต่อผ่านอินเทอร์เน็ตจากผู้ใช้ทั่วโลก ส่งผลให้การเข้ารหัส ASCII ที่รองรับตัวอักขระได้เพียงหลักร้อยตัวไม่เพียงพอต่อการใช้งาน\n",
" \n",
" \n",
"**มาตรฐาน Unicode:** เป็นมาตรฐานสากลเพื่อการเข้ารหัส (encoding) จัดระเบียบ และแสดงผลตัวอักขระข้อความที่ใช้กันอย่างแพร่หลายมากที่สุดในปัจจุบัน รองรับภาษาทั่วโลกกว่า 154 ภาษา (รวมถึงภาษาไทย) และสามารถแสดงผลตัวอักขระแบบต่างๆ เช่น emojis ได้มากกว่า 143,000 ตัว [List of Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters)\n",
"\n",
"ดังนั้นรหัสภาษาไทยเอาไปเปิดเครื่องที่เป็นภาษาญี่ปุ่น ก็ยังคงแสดงเป็นภาษาไทยอยู่ ไม่ออกมาเป็นภาษาญี่ปุ่น (สมัยก่อน) เพราะว่ามี code ตายตัวอยู่ว่า code นี้จองไว้สำหรับภาษาไทย แล้ว code ตรงช่วงนั้นเป็นภาษาจีน ตรงโน่นเป็นภาษาญี่ปุ่น จะไม่ใช้ที่ซ้ำกัน เป็นต้น\n",
"\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The ASCII value of 'A' is 65\n",
"The ASCII value of '฿' is 3647\n"
]
}
],
"source": [
"# Program to find the ASCII value of the given character\n",
"ch_A = 'A'\n",
"ch_Bath = '฿'\n",
"print(\"The ASCII value of '\" + ch_A + \"' is\", ord(ch_A))\n",
"print(\"The ASCII value of '\" + ch_Bath + \"' is\", ord(ch_Bath))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ยกตัวอย่าง \n",
"\n",
"ASCII code ของ ! คือ 33, ASCII code ของ + คือ 43. ดังนั้น + มีค่ามากกว่า ! เพราะ 43 มีค่ามากกว่า 33.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'+' > '!'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"และเช่นเดียวกัน B มีค่ามากกว่า A"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Compare characters\n",
"\n",
"'B' > 'A'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"กรณีที่มีหลายอักขระ จะเปรียบเทียบอักขระตัวแรกก่อน\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Compare characters\n",
"\n",
"'BA' > 'AB'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: ตัวพิมพ์ใหญ่มีรหัส ASCII แตกต่างจากตัวพิมพ์เล็ก นั่นหมายความว่าการเปรียบเทียบระหว่างตัวอักษรใน Python จะคำนึงถึงตัวพิมพ์ใหญ่ตัวพิมพ์เล็กเสมอ"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### ตัวอย่างนิพจน์ทางตรรกศาสตร์ (Boolean Logical Expression) (เพิ่มเติม)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"เราสามารถเก็บผลลัพธ์ในรูปตัวแปรได้"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"x, y = 1, 6 # x, y = (1, 6)\n",
"\n",
"p = x**5 < y\n",
"q = x**5 <= y**0.5\n",
"r = x**5 + 5*x == y\n",
"s = len('ไมเคิล แจ็คสัน') >= len('Michael Jackson')\n",
"t = len('ไมเคิล แจ็คสัน'*20) > 19*len('Paul')\n",
"u = (x**2 + 3*x + 2) != y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ลองเขียนโค้ดเช็คดูว่า ผลลัพท์ของตัวแปรแต่ละตัวมีค่าเหมือนที่คิดไว้หรือไม่ (ถ้าไม่เหมือนที่คิดไว้ ให้ไปทบทวนอีกรอบ)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"เราสามารถตรวจสอบช่วงของค่าได้"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"ra = 0 < x < -5\n",
"rb = -10 < y <= 10"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ra"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"สำหรับภาษาไทย "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3586"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ord('ข')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### คำสั่งเลือกทำ (if Statement)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**คำสั่ง if (if statement)** เป็นคำสั่งที่ใช้ควบคุมการทำงานของโปรแกรมที่เป็นพื้นฐานและง่ายที่สุด เราใช้คำสั่ง **if** เพื่อสร้างเงื่อนไขให้โปรแกรมทำงานตามที่เราต้องการ คำสั่ง **if** เปรียบเสมือนกับห้องที่ถูกล็อกไว้ ถ้าหากเงื่อนไขเป็น **True** เราจะเข้าห้องได้และโปรแกรมทำงานบางอย่างตามที่เราต้องการ แต่ถ้าเงื่อนไขเป็น **False** โปรแกรมจะข้ามการทำงานไป\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**if syntax:**\n",
"\n",
" if < expr > :
\n",
" < statement >
\n",
" . . .
\n",
" < statement >
\n",
" < following_statement >
\n",
"\n",
"\n",
"รูปแบบของการเขียนจะประกอบไปด้วยคำสั่ง if ต่อด้วยเงื่อนไข < expr > ซึ่งเป็นนิพจน์ตรรกะ (Logical Expressions) และปิดท้ายด้วย : (colon)\n",
"โค้ดบรรทัดใหม่ที่เยื้องเข้าไปหลัง colon ทุกคำสั่ง (กรณีที่มีหลายคำสั่ง เราจะเรียกเป็น **Block (บล็อกของโค้ด-if)**) จะถูกรันก็ต่อเมื่อเงื่อนไข < expr > เป็นจริง (**True**) เท่านั้น แตะถ้าเป็นเท็จ (False) ก็จะข้ามคำสั่งในบล็อกของโค้ด-if ไป แล้วรันคำสั่งในบันทัดถัดไป < following_statement > \n",
"ดังแสดงในรูปต่อไปนี้\n",
"\n",
"\n",
"\n",
"ตัวอย่าง หากกำหนดให้สี่เหลี่ยมสีฟ้าแทนกลุ่มแร็ป Rap Against Dictatorship* ถ้ามีอายุมากกว่า 18 ปี จะสามารถเข้าร่วมคอนเสิร์ตได้ (รันโค้ด print(“you can enter”)) แต่ถ้าหากอายุต่ำกว่า 18 ปี จะไม่สามารถเข้าร่วมได้ (ข้ามโค้ด print(“you can enter”))\n",
"\n",
"การทำงานของโค้ด print(\"move on\") จะถูกรันทุกกรณี ไม่เกี่ยวข้องกับเงื่อนไขของคำสั่ง if\n",
"\n",
"* \n",
"RAP AGAINST DICTATORSHIP - Prathet Ku Mee 🇹🇭 (My Country Has) - YouTube "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"ภาษาไทธอนใช้การเยื้องในการจัดกลุ่มบล๊อก (Block หรือบล็อกของโค้ด) สำหรับกลุ่มเดียวกัน ต้องเยื้อง (Indent) ให้เท่ากัน!\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n",
"Have a good day\n"
]
}
],
"source": [
"# If statement example\n",
"\n",
"if True:\n",
" print('Hello')\n",
" print('Have a good day')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Have a good day\n"
]
}
],
"source": [
"# If statement example\n",
"\n",
"if False:\n",
" print('Hello')\n",
"print('Have a good day')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"you can enter\n",
"move on\n"
]
}
],
"source": [
"# If statement example\n",
"\n",
"age = 19\n",
"#age = 17\n",
"\n",
"# expression that can be true or false\n",
"if age > 18:\n",
" \n",
" # within an indent, we have the expression that is run if the condition is true\n",
" print(\"you can enter\" )\n",
"\n",
"# The statements after the if statement will run regardless if the condition is true or false \n",
"print(\"move on\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ลองเปลี่ยนค่าตัวแปรเป็นอายุที่เข้าไปไม่ได้ดู ได้ผลลัพธ์เป็นไปตามรูปหรือไม่?\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"โค้ดข้างต้นเป็นการประเมินเงื่อนไข หากเงื่อนไขเป็นจริงก็ให้ทำการรันโค้ดในบล็อก ซึ่งเป็นการเลือกให้ทำเพียงเส้นทางเดียว แต่ถ้าเราต้องการระบุให้ทำอีกเส้นทางหากเงื่อนไขเป็นเท็จ ก็สามารถเขียนได้ด้วยคำสั่ง **else**\n",
"\n",
"**if-else syntax:**
\n",
"\n",
" if < expr > :
\n",
" < statement(s) >
\n",
" else:
\n",
" < statement(s) >
\n",
" < following_statement >
\n",
"\n",
"\n",
"คำสั่ง else จะประกอบไปด้วยคำสั่ง else และปิดท้ายด้วย : (colon) ทันที่ โค้ดบรรทัดใหม่ที่เยื้องเข้าไปหลัง colon (กรณีที่มีหลายคำสั่ง เราจะเรียกเป็น **Block (บล็อกของโค้ด-else)**) จะถูกรันก็ต่อเมื่อเงื่อนไขของ if เป็นเท็จ (**False**) เท่านั้น \n",
"\n",
"คำสั่ง **if-else** เป็นคำสั่งเลือกทำสองทาง กล่าวคือ หากเงื่อนไขของ if เป็นจริง (**True**) จะรัน**บล็อกของโค้ด-if** แตถ้าเงื่อนไขของ if เป็นเท็จ (**False**) จะรัน**บล็อกของโค้ด-else** แทน \n",
"\n",
"เรากลับไปที่ตัวอย่างคอนเสิร์ต Rap Against Dictatorship อีกครั้ง หากมีอายุ 17 ปี (อายุต่ำกว่า 18 ปี) จะไม่สามารถไปเข้าร่วมคอนเสิร์ตได้ แต่สามารถดูคอนเสิร์ต BNK48 ได้"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"go see BNK48\n",
"move on\n"
]
}
],
"source": [
"# Else statement example\n",
"\n",
"age = 17\n",
"# age = 19\n",
"\n",
"if age > 18:\n",
" print(\"you can enter\" )\n",
"else:\n",
" print(\"go see BNK48\" )\n",
" \n",
"print(\"move on\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" ลองเปลี่ยนค่าตัวแปรเป็นอายุ 19 ปี ได้ผลลัพธ์เป็นไปตามรูปหรือไม่?\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"หากเราต้องการทางเลือกมากกว่าสอง ก็สามารถเขียนได้โดยใช้คำสั่ง **elif** จะใช้ครั้งเดียวหรือมากกว่าหนึ่งครั้งก็ได้ (**elif มาจาก else if**)\n",
"\n",
"
**if-elif-else syntax:** \n",
"\n",
" if < expr > :
\n",
" < statement(s) >
\n",
" elif < expr > :
\n",
" < statement(s) >
\n",
" . . .
\n",
" elif < expr > :
\n",
" < statement(s) >
\n",
" else:
\n",
" < statement(s) >
\n",
" < following_statement >
\n",
"\n",
"\n",
"Python จะประเมินเงื่อนไข < expr > ในแต่ละเงื่อนไข (จากบนลงล่าง) และทำการรันชุดคำสั่ง (บล็อกของโค้ด) ที่สอดคล้องกับเงื่อนไขหากเงื่อนไขนั้นเป็นจริง แต่ถ้าหากไม่มีเงื่อนไขใดเป็นจริง ก็จะรันบล็อกของโค้ด-else แทน\n",
"\n",
"(จะมี elif กี่ตัวก็ได้ และมี else หรือไม่มีก็ได้) \n",
"\n",
"(ภาษาอื่นอาจมี switch และ case แต่ไทธอนใช้ if อย่างเดียว)\n",
"\n",
"เรากลับไปที่ตัวอย่างคอนเสิร์ตอีกครั้ง หากบุคคลนั้นมีอายุ 18 ปีพอดี พวกเขาจะไปดูคอนเสิร์ต SPRITE x GUYGEEGEE แทนการเข้าร่วมคอนเสิร์ตกลุ่มแร็ป Rap Against Dictatorship หรือ BNK48\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"go see BNK48\n",
"move on\n"
]
}
],
"source": [
"# Elif statment example\n",
"\n",
"age = 17\n",
"\n",
"if age > 18:\n",
" print(\"you can enter\" )\n",
"elif age == 18:\n",
" print(\"go see SPRITE x GUYGEEGEE\")\n",
"else:\n",
" print(\"go see BNK48\" )\n",
" \n",
"print(\"move on\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ตัวอย่างข้างต้นมีทางเลือก 3 ทาง \n",
"\n",
"ทางเลือกขวาสุดสำหรับคนที่อายุน้อยกว่า 18 ปี\n",
"ทางเลือกซ้าบสุดสำหรับคนที่อายุมากกว่า 18 ปี\n",
"ทางเลือกตรงกลางสำหรับคนที่มีอายุพอดี 18 ปี\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ลองดูโค้ดต่อไปนี้"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year is greater than 1980\n",
"do something..\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1983\n",
"\n",
"if album_year > 1980:\n",
" print(\"Album year is greater than 1980\")\n",
" \n",
"print('do something..')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ลองเปลี่ยนตัวแปร album_year เป็นค่าอื่นๆ ดู"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter year: 2001\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year is greater than 1980\n",
"do something..\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = int(input('Enter year: '))\n",
"if album_year > 1980:\n",
" print(\"Album year is greater than 1980\")\n",
" \n",
"print('do something..')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"คำสั่งที่เยื้องเข้าไป print(\"Album year is greater than 1980\") จะถูกรันก็ต่อเมื่อเงื่อนไขเป็นจริง True เท่านั้น\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ลองเขียนโค้ดใหม่ ให้เป็นตาม Flowchart ต่อไปนี้\n",
"\n",
"ลองเขียนดู!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"do something..\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"album_year = 1983\n",
"album_year = 1970\n",
"\n",
"if album_year > 1980:\n",
" print(\"Album year is greater than 1980\")\n",
" \n",
"print('do something..')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Click here for the solution
\n",
"\n",
"```python\n",
"# Condition statement example\n",
"\n",
"album_year = 1983\n",
"#album_year = 1970\n",
"\n",
"if album_year > 1980:\n",
" print(\"Album year is greater than 1980\")\n",
"else:\n",
" print(\"less than 1980\")\n",
"\n",
"print('do something..')\n",
"\n",
"```\n",
"\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ตัวดำเนินการทางตรรกะ (Logical operators): not, and, or\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ในบางครั้งเราต้องการตรวจสอบมากกว่าหนึ่งเงื่อนไขพร้อมๆ กัน ยกตัวอย่างเช่น ต้องการตรวจสอบว่าเงื่อนไข-1 และเงื่อนไข-2 เป็น **True** ทั้งคู่หรือไม่ ในกรณีนี้ ตัวดำเนินการทางตรรกะ (Logical operators) สามารถช่วยปรับแก้เงื่อนไขได้\n",
"\n",
"ตัวดำเนินการตรรกศาสตร์ในภาษา Python\n",
"\n",
" (ลำดับความสำคัญ not -> and -> or)\n",
" | Operator | Example | Result |
\n",
" | or | a or b | True if a or b are true, else False |
\n",
" | and | a and b | True if a and b are true, else False |
\n",
" | not | not a | True if a is False, else True |
\n",
"
\n",
"\n",
"ตัวดำเนินการข้างต้นสำหรับสองตัวแปรสรุปเป็นตารางความจริง ได้ดังนี้\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"คำสั่ง and เป็นจริง (True) เมื่อเงื่อนไขทั้งสองเป็นจริง คำสั่ง or จะเป็นเป็นจริง (True) ถ้าเงื่อนไขใดเงื่อนไขหนึ่งเป็นจริง ส่วนคำสั่ง not จะแสดงผลค่าความจริงที่ตรงกันข้าม\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"เรามาดูวิธีการตรวจสอบว่าอัลบั้มออกหลังปี 1979 (ไม่รวมปี 1979) **และ**ก่อนปี 1990 (ไม่รวมปี 1990) หรือไม่ \n",
"\n",
"จากรูปด้านล่าง Timeline 2 เส้น a และ b แทนข้อความข้างต้น สีเขียวแสดงเงื่อนไขเป็น **True** และสีแดงแสดงเงื่อนไขเป็น **False** \n",
"เส้น c แสดงเงื่อนไขทั้งสองเป็น **True** \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"เขียนโค้ดได้เป็น"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year was in between 1980 and 1989\n",
"\n",
"Do Stuff..\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1980\n",
"\n",
"if(album_year > 1979) and (album_year < 1990):\n",
" print (\"Album year was in between 1980 and 1989\")\n",
" \n",
"print(\"\")\n",
"print(\"Do Stuff..\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ในภาษาไทธอนเขียนเป็นแบบนี้ก็ได้!!"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year was in between 1980 and 1989\n",
"\n",
"Do Stuff..\n"
]
}
],
"source": [
"album_year = 1980\n",
"\n",
"if 1990 > album_year > 1979 :\n",
" print (\"Album year was in between 1980 and 1989\")\n",
" \n",
"print(\"\")\n",
"print(\"Do Stuff..\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ถ้าเราต้องตรวจสอบว่าอัลบั้มออกก่อนปี 1980 (1979 และก่อนหน้านั้น) **หรือ**หลังปี 1989 (1990 และหลังจากนั้น) หรือไม่ ก็ก็ใช้คำสั่ง **or** \n",
"\n",
"จากรูปด้านล่าง Timeline 2 เส้น a และ b แทนข้อความข้างต้น สีเขียวแสดงเงื่อนไขเป็น **True** และสีแดงแสดงเงื่อนไขเป็น **False** \n",
"เส้น c แสดงเงื่อนไขใดเงื่อนไขหนึ่งเป็นจริง (**True**)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"เขียนโค้ดได้เป็น"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album was not made in the 1980's\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1990\n",
"\n",
"if(album_year < 1980) or (album_year > 1989):\n",
" print (\"Album was not made in the 1980's\")\n",
"else:\n",
" print(\"The Album was made in the 1980's \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"คำสั่ง not ตรวจสอบว่าเงื่อนไขนั้นเป็นเท็จหรือไม่:"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year is not 1984\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1983\n",
"\n",
"if not (album_year == 1984):\n",
" print (\"Album year is not 1984\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### การเขียนคำสั่ง if หนึ่งบรรทัด (One-Line if Statements)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"จาก if syntax ข้างต้น\n",
"\n",
" if <expr>:
\n",
" <statement>
\n",
"\n",
"เราสามารถเขียนคำสั่ง ``if`` ให้อยู่ในบรรทัดเดียวได้ ดังนี้\n",
"\n",
" if <expr>: <statement> \n",
"\n",
"กรณีที่มีหลายคำสั่ง ก็สามารถใช้ ** ; ** (semicolon) คั่นระหว่างคำสั่งได้\n",
"\n",
" if <expr>: <statement_1>; <statement_2>; <statement_3> \n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year is not 1984\n",
"It is 1983\n"
]
}
],
"source": [
"album_year = 1983\n",
"\n",
"if not (album_year == 1984): print (\"Album year is not 1984\"); print (\"It is \", album_year)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**if-else, if-elif-else ก็สามารถเขียนให้เป็นบรรทัดเดียวได้เช่นกัน**\n",
"\n",
"คำสั่ง ``if-else`` ให้อยู่ในบรรทัดเดียวได้ ดังนี้\n",
"\n",
"<value_on_true> if <expr> else <value_on_false>\n",
"\n",
"Syntax ต่างกับ `if` บรรทัดเดียว (ระวังตำแหน่ง, <value_on_true> และ <value_on_false> มีได้คำสั่งเดียว)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year is not 1984\n"
]
}
],
"source": [
"album_year = 19843\n",
"\n",
"print (\"Album year is 1984\") if (album_year == 1984) else print (\"Album year is not 1984\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [Exercise]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1) จงเขียนคำสั่ง if เพื่อตรวสอบว่าอัลบั้มมีเรตติ้งมากกว่า 8 หรือไม่ ทำการทดสอบโดยใช้การจัดเรตติ้งของอัลบั้ม “Back in Black” ที่มีค่าเรตติ้ง 8.5 หากเงื่อนไขดังกล่าวเป็นจริง ให้พิมพ์ \"This album is Amazing!\" ออกหน้าจอ\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2) จงเขียนคำสั่ง if-else โดยมีเงื่อนไขดังนี้\n",
"\n",
"ถ้าเรตติ้งสูงกว่า 8 พิมพ์ “this album is amazing” แต่ถ้าเรตติ้งน้อยกว่าหรือเท่ากับ 8 พิมพ์ว่า “this album is ok” ออกหน้าจอ"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3) จงเขียนสำสั่ง if เพื่อเช็กดูว่าอัลบั้มออกก่อนปี 1980 **หรือ**ออกในปี 1991 **หรือ**ออกในปี 1993 หรือไม่ ถ้าเงื่อนไขใดเงื่อนไขหนึ่งเป็นจริง ให้พิมพ์ \"This album came out in year xxxx\" xxx ปีที่ออกจำหน่าย\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4) เมื่อทำการรันโค้ตต่อไปนี้ บรรทัดแต่ละบรรทัดจะถูกรันหรือไม่ (Y/N)?\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"# Does line execute? Y/N?\n",
"# ---\n",
"if 'foo' in ['foo', 'bar', 'baz']: # ?\n",
" print('Outer condition is true') # ?\n",
"\n",
" if 10 > 20: # ?\n",
" print('Inner condition 1') # ?\n",
"\n",
" print('Between inner conditions') # ?\n",
"\n",
" if 10 < 20: # ?\n",
" print('Inner condition 2') # ?\n",
"\n",
" print('End of outer condition') # ?\n",
"print('After outer condition') # ?\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ลองตอบโดยไม่ต้องเขียนโค้ด! (สามารถลองรันได้โดยการคัดลอกโค้ดข้างต้นแล้ววางลงในเซลล็ด้านล่าง...)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Outer condition is true\n",
"Between inner conditions\n",
"Inner condition 2\n",
"End of outer condition\n",
"After outer condition\n"
]
}
],
"source": [
"# Does line execute? Y/N?\n",
"# ---\n",
"if 'foo' in ['foo', 'bar', 'baz']: # Y\n",
" print('Outer condition is true') # Y\n",
"\n",
" if 10 > 20: # Y\n",
" print('Inner condition 1') # N\n",
"\n",
" print('Between inner conditions') # Y\n",
"\n",
" if 10 < 20: # Y\n",
" print('Inner condition 2') # Y\n",
"\n",
" print('End of outer condition') # Y\n",
"print('After outer condition') # Y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Magic Freebies: โค้ดเช็คราคาบิทคอยน์ ณ ปัจจุบัน"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"781,010.82 บาท\n"
]
}
],
"source": [
"# Beautiful Soup is a Python library for pulling data out of HTML and XML files.\n",
"from bs4 import BeautifulSoup as soup\n",
"import requests\n",
" \n",
"url = \"https://www.google.com/search?q=bitcoin+price\"\n",
"data = requests.get(url)\n",
"data_html = data.text\n",
"data_soup = soup(data_html, 'html.parser')\n",
"c_price = data_soup.find(\"div\", class_ =\"BNeawe iBp4i AP7Wnd\").text\n",
"\n",
"print(c_price)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ลองเปลี่ยนโค้ดข้างต้นให้ค้นหาราคาดอลลาร์สหรัฐ?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"“Everybody should learn to program a computer, because it teaches you how to think\"\" - Steve Jobs 1995\n"
]
},
{
"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",
"| | | |\n",
"| | | |\n"
]
}
],
"metadata": {
"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": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"metadata": {
"interpreter": {
"hash": "3abed311d39052675f1169a15d6f64f9745014cae486d494ad699e58cf37536b"
}
},
"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": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}