ในบทความนี้ เราจะมาดูวิธีใช้ code 3 ประเภท ที่จะช่วยให้ Python code สามารถตัดสินใจแทนเราได้:
- Conditional statements:
if,elif,else - Control flow statements:
for,while - Loop control statements:
continue,break,pass
ก่อนไปดูวิธีใช้ทั้ง 3 ประเภท เราจะไปทำความรู้จักกับ comparison และ logical statement ซึ่งเราจะใช้ร่วมกับ code 3 ประเภทนี้กัน
ถ้าพร้อมแล้ว ไปเริ่มกันเลย
- 🧮 1. Comparison & Logical Operators
- 🚦 2. Conditional Statements
- 🌊 3. Control Flow Statements
- 🚄 4. Loop Control Statements
- 💪 5. Summary
- 😺 GitHub
- 📃 References
🧮 1. Comparison & Logical Operators
#️⃣ (1) Comparison Operators
Comparison operators เป็นเครื่องหมายใช้เปรียบเทียบข้อมูล:
| Operator | Description |
|---|---|
== | เท่ากับ |
!= | ไม่เท่ากับ |
> | มากกว่า |
>= | มากกว่า/เท่ากับ |
< | น้อยกว่า |
<= | น้อยกว่า/เท่ากับ |
โดย comparison operators จะให้ผลลัพธ์เป็น Boolean (True, False) กลับมา เช่น:
# True statement
10 > 5
ผลลัพธ์:
True
และ:
# False statement
10 < 5
ผลลัพธ์:
False
♟️ (2) Logical Operators
Logical operators เป็น keywords เชื่อมเงื่อนไข ช่วยให้เราประเมินหลายเงื่อนไขพร้อมกันได้:
| Operator | Description |
|---|---|
and | Logical AND |
or | Logical OR |
not | Logical NOT |
โดยผลลัพธ์เป็นไปตาม truth table:
| Condition 1 | Operator | Condition 2 | Result |
|---|---|---|---|
True | and | True | True |
True | and | False | False |
False | and | False | False |
True | or | True | True |
True | or | False | True |
False | or | False | False |
| – | not | True | False |
| – | not | False | True |
ยกตัวอย่างเช่น:
# True and True
1 == 1 and 2 < 4
ผลลัพธ์:
True
🚦 2. Conditional Statements
Conditional statements ใช้กำหนดเงื่อนไขว่า code จะรันได้เมื่อไร และมีอยู่ 3 แบบ ได้แก่:
if: กำหนดเงื่อนไขแรกelif: กำหนดเงื่อนไขเพิ่มเติมelse: รันเมื่อตรงกับเงื่อนไขอื่น ๆ ที่ไม่ใช่ifและelif
ยกตัวอย่างเช่น เราต้องการ print ข้อความแจ้งเตือนตามสภาพอากาศ:
# Weather today
weather = "snowy"
เราสามารถทำได้แบบนี้:
# Print when sunny
if weather == "sunny":
print("It's a sunny day. Don't forget your sunscreen!")
# Print when rainy
elif weather == "rainy":
print("It's raining. Remember to bring an umbrella!")
# Print when other conditions
else:
print("Likely chilly. Wear a jacket!")
ผลลัพธ์:
Likely chilly. Wear a jacket!
อธิบาย code:
ifblock: ประเมินว่า weather เป็น"sunny"ไหม ถ้าใช่ จะ print"It's a sunny day. Don't forget your sunscreen!"elifblock: weather เป็น"rainy"ไหม ถ้าใช่ จะ print"It's raining. Remember to bring an umbrella!"elseblock: ถ้า weather เป็นค่าอื่น ๆ (เช่น"snowy") จะ print"Likely chilly. Wear a jacket!"
เพราะ weather มีค่าตรงกับ else block เราจึงได้ผลลัพธ์เป็น "Likely chilly. Wear a jacket!"
🌊 3. Control Flow Statements
Control flow statements ใช้ควบคุมลำดับการทำงานของ code และมีอยู่ 2 แบบ ได้แก่:
for: รัน code ตามจำนวนข้อมูลที่มีwhile: รัน code จนกว่าเงื่อนไขจะไม่เป็นจริง
📦 (1) for Loop
ตัวอย่าง for loop เช่น print ชื่อแขกในงาน:
# Guest list
guests = ["James Bond", "John Wick", "Jack Reacher", "Jason Bourne", "Jack Ryan"]
# Print guest names
for name in guests:
# Print name
print(name)
ผลลัพธ์:
James Bond
John Wick
Jack Reacher
Jason Bourne
Jack Ryan
อธิบาย code:
guest = ...: สร้าง list เก็บรายชื่อแขกfor ...: นำชื่อแขกใน list มา print จนกว่าจะครบทุกคน
🔁 (2) while Loop
ตัวอย่าง while loop เช่น นับเลข 1 ถึง 10:
# Starting number
number = 1
# Count to 10
while number <= 10:
# Print number
print(number)
# Add 1 to number
number += 1
ผลลัพธ์:
1
2
3
4
5
6
7
8
9
10
อธิบาย code:
number = 1: กำหนดเลขเริ่มต้นwhile ...: printnumberและปรับnumberให้สูงขึ้น 1 ค่า ทำอย่างนี้วนไปจนกว่าnumberจะมากกว่า 10 (number <= 10ไม่เป็นจริง)
🚄 4. Loop Control Statements
Loop control statements ใช้ควบคุมการทำงานของ control flow และมีอยู่ 3 แบบ ได้แก่:
continue: skip ไป item ถัดไปbreak: หยุดการทำงานของ control flowpass: placeholder สำหรับใส่ code ในอนาคต
ยกตัวอย่างเช่น เรามีรายการของที่ต้องซื้อ และแต่ละอย่างมี action ไม่เหมือนกัน:
# A shopping list program
shopping_list = ["milk", "bread", "chips", "apple", "toothpaste", "chocolate"]
# Loop through the list
for item in shopping_list:
# Skip item if chip
if item == "chips":
print("Chips are unhealthy. Skipping ...")
continue
# Stop the loop if toothpaste
if item == "toothpaste":
print("Found toothpaste, done shopping early!")
break
# Do nothing if milk
if item == "milk" or item == "bread":
pass
# Print item
print("Putting", item, "into the cart.")
ผลลัพธ์:
Putting milk into the cart.
Putting bread into the cart.
Chips are unhealthy. Skipping ...
Putting apple into the cart.
Found toothpaste, done shopping early!
อธิบาย code:
shopping_list = ...: สร้าง list เก็บรายการซื้อของif item == "chips"block: ประเมินว่า item ใช่"chip"ไหม ถ้าใช่ ให้ print"Chips are unhealthy. Skipping ..."และข้ามไป item ถัดไป (continue)if item == "toothpaste"block: ประเมินว่า item ใช่"toothpaste"ไหม ถ้าใช่ ให้ print"Found toothpaste, done shopping early!"และจบ loop ทันที (break)if item == "milk" …block: ประเมินว่า item ใช่"milk"หรือ"bread"ไหม ถ้าใช่ ไม่ต้องทำอะไร (pass)print ...: print ว่า กำลังเอา item ใส่ตระกร้า
สังเกตว่า:
- มีแค่ milk, bread, apple ที่ “กำลังใส่ตระกร้า” เพราะ milk และ bread อยู่ใน
passblock และ apple ไม่ได้อยู่ใน block ไหนเลย - Chip ถูกข้ามไป เพราะอยู่ใน
continueblock - Toothpaste อยู่ใน
breakblock และมาก่อน chocolate ทำให้เราไม่เห็น chocolate เพราะ toothpaste
💪 5. Summary
ในบทความนี้ เราได้ไปดูวิธีการเขียน code ใน Python เพื่อให้ code ตัดสินใจได้:
Comparison operators:
| Operator | Description |
|---|---|
== | เท่ากับ |
!= | ไม่เท่ากับ |
> | มากกว่า |
>= | มากกว่า/เท่ากับ |
< | น้อยกว่า |
<= | น้อยกว่า/เท่ากับ |
Logical operators:
| Operator | Description |
|---|---|
and | Logical AND |
or | Logical OR |
not | Logical NOT |
Conditional statements:
| Statement | Description |
|---|---|
if | กำหนดเงื่อนไขแรก |
elif | กำหนดเงื่อนไขเพิ่มเติม |
else | ทำ action เมื่อเข้าเงื่อนไขอื่น ๆ |
Control flow statements:
| Statement | Description |
|---|---|
for | วนจนครบทุก item |
while | วนจนกว่าเงื่อนไขจะเป็น False |
Loop control statements:
| Statement | Description |
|---|---|
continue | Skip |
break | Stop |
pass | Do nothing |
😺 GitHub
ดูตัวอย่าง code ทั้งหมดได้ที่ GitHub
