match / case: match a value against several patterns Match / Case: เทียบค่าหลายแบบ

Use match with case (Python 3.10+) instead of a long if/elif when matching one value many ways ใช้ match กับ case (Python 3.10+) แทน if/elif ยาวๆ เมื่อเทียบค่าหลายทาง

match / case is structural pattern matching — a value is compared against each case from top to bottom, and the first matching block runs (later cases are not checked). It is not the same as switch in C, but it is used similarly to compare one value many ways.

match / case คือ structural pattern matching — เอาค่าไปเทียบกับแต่ละ case จากบนลงล่าง เจอแบบที่ตรงแล้วรันบล็อกนั้น (ไม่เช็ค case ถัดไป) ต่างจาก switch ในภาษา C แต่ใช้แทนการเทียบค่าหลายทางได้คล้ายกัน

1 match a number with several cases 1 match ค่าตัวเลข + หลาย case

Write match value: followed by a case pattern: for each pattern — indent the block under each case.

เขียน match ค่า: แล้วตามด้วย case แบบ: แต่ละแบบ — ย่อหน้าบล็อกใต้ case

[1]
[output]
Wed

2 Several values in one case with | 2 case หลายค่าด้วย |

Use | to combine several values in one case — matching any one of them enters that block.

ใช้เครื่องหมาย | รวมหลายค่าใน case เดียว — ตรงค่าใดค่าหนึ่งก็เข้าบล็อกนั้น

[2]
[output]
two or three

3 case _ as the default 3 case _ เป็นค่าเริ่มต้น

case _: is the wildcard — it catches every value that did not match an earlier case (like else for an if).

case _: คือ wildcard — จับทุกค่าที่ยังไม่ตรง case ก่อนหน้า (คล้าย else ของ if)

[3]
[output]
go or unknown

4 When to use match instead of if/elif 4 เมื่อไหร่ใช้ match แทน if/elif

When you compare the same value against several exact options, match reads more clearly than a long if/elif. (For number ranges such as >= 80, if/elif still fits better.)

เมื่อเทียบค่าเดียวกันกับหลายทางตรงๆ match อ่านง่ายกว่า if/elif ยาว (ถ้าเป็นช่วงตัวเลขเช่น >= 80 ยังเหมาะกับ if/elif มากกว่า)

[4]
[output]
saving

5 A command menu 5 เมนูคำสั่งของเครื่อง

Programs often react to a command typed by the user. match compares the text with each case; | lets one case accept several spellings, and case _ catches everything else.

โปรแกรมมักตอบสนองต่อคำสั่งที่ผู้ใช้พิมพ์ match เทียบข้อความกับแต่ละ case ใช้ | ให้หนึ่ง case รับได้หลายคำ และ case _ ดักทุกกรณีที่เหลือ

[5]
[output]
Machine stopping

6 Common mistakes 6 ข้อผิดพลาดที่พบบ่อย

  1. Expecting fall-through. Unlike switch in C, only the first matching case runs and there is no break. The demo prints two only.
  2. คิดว่าจะรันต่อทุก case ต่างจาก switch ในภาษา C เพราะ case แรกที่ตรงเท่านั้นที่ทำงาน และไม่ต้องใช้ break ตัวอย่างพิมพ์เพียง two
  3. case _ in the wrong place. The wildcard matches anything, so it must be the last case; Python reports a SyntaxError if other cases follow it.
  4. วาง case _ ผิดที่ wildcard ตรงกับทุกค่า จึงต้องเป็น case สุดท้าย ถ้ามี case อื่นตามหลัง Python จะแจ้ง SyntaxError
  5. A bare name is not a constant. case x: does not compare with a variable x; it matches everything and stores the value in x. Use literal values in case.
  6. ชื่อตัวแปรเปล่า ๆ ไม่ใช่ค่าคงที่ case x: ไม่ได้เทียบกับตัวแปร x แต่ตรงกับทุกค่าแล้วเก็บค่าลงใน x ให้ใช้ค่าตรง ๆ ใน case
[6]
[output]
two

7 Think further 7 แบบฝึกคิดต่อ

Try these yourself in the Run boxes above (or in Google Colab):

ลองทำเองในกล่อง Run ด้านบน (หรือใน Google Colab):

  1. Turn a weekday number (1–7) into its name with match. Use case 6 | 7 to print weekend.
  2. แปลงเลขวัน (1–7) เป็นชื่อวันด้วย match และใช้ case 6 | 7 ให้พิมพ์ weekend
  3. Can you rewrite the if / elif grade chain from the previous lesson with match? Why is it awkward for ranges such as >= 80?
  4. ลองเขียนเกรดด้วย if / elif จากบทก่อนใหม่ด้วย match ทำไมจึงไม่สะดวกกับช่วงตัวเลขอย่าง >= 80
  5. Add a fourth command "pause" to the menu and test an unknown word.
  6. เพิ่มคำสั่งที่สี่ "pause" ลงในเมนู แล้วทดสอบคำที่ไม่รู้จัก

Key takeaways สรุปสำคัญ

Remember these points when you write your own programs:

จำประเด็นเหล่านี้ไว้เมื่อเขียนโปรแกรมของคุณเอง:

  1. match is best when one value is compared with several fixed alternatives, such as commands, codes or menu choices.
  2. match เหมาะเมื่อต้องเทียบค่าเดียวกับตัวเลือกคงที่หลายตัว เช่น คำสั่ง รหัส หรือเมนู
  3. Only the first matching case runs, so there is no break and no fall-through to the next case.
  4. case แรกที่ตรงเท่านั้นที่ทำงาน จึงไม่ต้องใช้ break และไม่ไหลต่อไปยัง case ถัดไป
  5. Use | to group values that should behave the same, and end with case _ so unexpected input is handled instead of ignored silently.
  6. ใช้ | รวมค่าที่ต้องทำงานเหมือนกัน และปิดท้ายด้วย case _ เพื่อจัดการค่าที่คาดไม่ถึงแทนที่จะเงียบไปเฉย ๆ
  7. For ranges such as score >= 80, an if / elif chain is still the clearer tool; choose the structure that makes the intention obvious to the next reader.
  8. สำหรับช่วงตัวเลขอย่าง score >= 80 โซ่ if / elif ยังอ่านง่ายกว่า เลือกโครงสร้างที่ทำให้ผู้อ่านคนถัดไปเข้าใจเจตนาได้ชัดที่สุด
  9. A match block compares by pattern, not by variable name, so keep the case lines simple and literal.
  10. บล็อก match เทียบตาม pattern ไม่ใช่ตามชื่อตัวแปร จึงควรเขียนบรรทัด case ให้เรียบง่ายและเป็นค่าตรง ๆ

Note: Run on this page is a JavaScript simulation, not real Python. For real Python, try Google Colab. หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการทดลอง Python จริง แนะนำ Google Colab

References References / แหล่งอ้างอิง

Sources used for this lesson:

แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:

  1. The match statement — Python Language Reference — match (match / case / _)

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

What is match / case mainly used for in Python?match / case ใน Python ใช้ทำอะไรเป็นหลัก?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) Comparing a value against several patterns and running the matching blockเทียบค่ากับหลายแบบแล้วเลือกบล็อกที่ตรง

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Comparing a value against several patterns and running the matching blockเทียบค่ากับหลายแบบแล้วเลือกบล็อกที่ตรง
    Correct! match compares a value against each case.ถูกต้อง! match นำค่าไปเทียบกับแต่ละ case
  • (B) Only looping foreverวนซ้ำไม่รู้จบเท่านั้น
    Wrong: looping uses for/while.ผิด: การวนซ้ำใช้ for/while
  • (C) Creating a new data typeสร้างตัวแปรชนิดใหม่
    Wrong: match does not create a new data type.ผิด: match ไม่ได้สร้างชนิดข้อมูลใหม่
  • (D) Deleting files on diskลบไฟล์บนดิสก์
    Wrong: it has nothing to do with files.ผิด: ไม่เกี่ยวกับการจัดการไฟล์

ข้อ 2

2 / 10

What does case _: mean?case _: มีความหมายอย่างไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) Matches every value not matched by an earlier case (the default)จับทุกค่าที่ยังไม่ตรง case ก่อนหน้า (ค่าเริ่มต้น)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Matches only the value zeroจับเฉพาะค่าศูนย์เท่านั้น
    Wrong: _ does not mean the number 0.ผิด: _ ไม่ได้หมายถึงเลข 0
  • (B) Matches every value not matched by an earlier case (the default)จับทุกค่าที่ยังไม่ตรง case ก่อนหน้า (ค่าเริ่มต้น)
    Correct! _ is the wildcard, like else.ถูกต้อง! _ เป็น wildcard คล้าย else
  • (C) Stops the program at onceหยุดโปรแกรมทันที
    Wrong: case _ does not stop the program by itself.ผิด: case _ ไม่ได้หยุดโปรแกรมเอง
  • (D) Forces an errorบังคับให้เกิด Error
    Wrong: case _ is a normal fallback; it does not raise an error.ผิด: case _ เป็นทางเลือกปกติ ไม่ได้โยน Error

ข้อ 3

3 / 10

With code = 2 and case 2 | 3:, does it enter this case?จาก code = 2 และ case 2 | 3: จะเข้า case นี้หรือไม่?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) Yes, because 2 matches the left side of |เข้า เพราะ 2 ตรงกับฝั่งซ้ายของ |

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) No, because it must match both 2 and 3ไม่เข้า เพราะต้องตรงทั้ง 2 และ 3
    Wrong: | means match any one of them.ผิด: | หมายถึงตรงอย่างใดอย่างหนึ่ง
  • (B) Yes, because 2 matches the left side of |เข้า เพราะ 2 ตรงกับฝั่งซ้ายของ |
    Correct! | combines values — matching any one is enough.ถูกต้อง! | รวมหลายค่า — ตรงค่าใดค่าหนึ่งก็พอ
  • (C) Only if code is a stringเข้าเฉพาะเมื่อ code เป็นสตริง
    Wrong: string type is not relevant here.ผิด: ไม่เกี่ยวกับชนิดสตริงในตัวอย่างนี้
  • (D) Always a SyntaxErrorเกิด SyntaxError เสมอ
    Wrong: case a | b is valid syntax in Python 3.10+.ผิด: ไวยากรณ์ case a | b ถูกต้องใน Python 3.10+

ข้อ 4

4 / 10

In what order are cases checked?ลำดับการตรวจ case เป็นอย่างไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) Top to bottom — stop at the first matchจากบนลงล่าง — เจอที่ตรงแล้วหยุด

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Always bottom to topจากล่างขึ้นบนเสมอ
    Wrong: they are checked top to bottom.ผิด: ตรวจจากบนลงล่าง
  • (B) A random case is pickedสุ่มเลือก case
    Wrong: it is not random.ผิด: ไม่ได้สุ่ม
  • (C) Top to bottom — stop at the first matchจากบนลงล่าง — เจอที่ตรงแล้วหยุด
    Correct! Checked in order and stops on a match.ถูกต้อง! ตรวจตามลำดับและหยุดเมื่อตรง
  • (D) Every case runs at onceรันทุก case ที่เขียนไว้พร้อมกัน
    Wrong: only the first matching case runs.ผิด: รันเพียง case ที่ตรงอันแรก

ข้อ 5

5 / 10

Which situation suits match better than a number-range if/elif?สถานการณ์ใดเหมาะกับ match มากกว่า if/elif ช่วงตัวเลข?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) Comparing string commands like open / save / quitเทียบคำสั่งสตริงหลายค่า เช่น open / save / quit

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Comparing string commands like open / save / quitเทียบคำสั่งสตริงหลายค่า เช่น open / save / quit
    Correct! Comparing one value against several exact options reads well with match.ถูกต้อง! เทียบค่าเดียวกันหลายทางตรงๆ อ่านง่ายด้วย match
  • (B) Checking score >= 80, >= 70, >= 50เช็คว่าคะแนน >= 80, >= 70, >= 50
    Wrong: number ranges still fit if/elif better.ผิด: ช่วงตัวเลขยังเหมาะกับ if/elif มากกว่า
  • (C) Printing numbers 1 to 100 in a loopวนพิมพ์ตัวเลข 1 ถึง 100
    Wrong: loop work uses for/while.ผิด: งานวนซ้ำใช้ for/while
  • (D) Catching a division by zeroจับหารด้วยศูนย์
    Wrong: that is a try/except job.ผิด: งานนั้นใช้ try/except

ข้อ 6

6 / 10

With day = 3 and cases 1..4 printing Mon/Tue/Wed/Thu, what is the output?จาก day = 3 และ case 1..4 ที่พิมพ์ Mon/Tue/Wed/Thu ผลลัพธ์คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) WedWed

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) MonMon
    Wrong: Mon is in case 1.ผิด: Mon อยู่ใน case 1
  • (B) ThuThu
    Wrong: Thu is in case 4.ผิด: Thu อยู่ใน case 4
  • (C) Nothingไม่พิมพ์อะไร
    Wrong: case 3 matches exactly.ผิด: มี case 3 ที่ตรงพอดี
  • (D) WedWed
    Correct! day = 3 matches case 3.ถูกต้อง! day = 3 ตรงกับ case 3

ข้อ 7

7 / 10

If there is no case _: and no case matches, what happens?ถ้าไม่มี case _: และไม่มี case ใดตรงเลย จะเกิดอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) No block runs; the program moves to the next lineไม่มีบล็อกใดทำงาน โปรแกรมไปทำบรรทัดถัดไป

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) A SyntaxErrorเกิด SyntaxError
    Wrong: it is not a syntax problem.ผิด: ไม่ใช่ปัญหาไวยากรณ์
  • (B) It runs the first case automaticallyรัน case แรกโดยอัตโนมัติ
    Wrong: nothing runs automatically.ผิด: ไม่มีการรันแบบสุ่มหรือบังคับ
  • (C) No block runs; the program moves to the next lineไม่มีบล็อกใดทำงาน โปรแกรมไปทำบรรทัดถัดไป
    Correct! A match that matches no case simply does nothing.ถูกต้อง! match ที่ไม่ตรง case ใดจะไม่ทำอะไรเลย
  • (D) The program stops immediatelyโปรแกรมหยุดทำงานทันที
    Wrong: no error and no stop.ผิด: ไม่เกิด error หรือหยุด

ข้อ 8

8 / 10

From which Python version is match / case available?match / case ใช้ได้ตั้งแต่ Python เวอร์ชันใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) Python 3.10 and laterPython 3.10 ขึ้นไป

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Python 3.10 and laterPython 3.10 ขึ้นไป
    Correct! Structural pattern matching was added in 3.10.ถูกต้อง! structural pattern matching เพิ่มใน 3.10
  • (B) Python 2.7Python 2.7
    Wrong: Python 2 has no match.ผิด: Python 2 ไม่มี match
  • (C) Python 3.6Python 3.6
    Wrong: not in 3.6 yet.ผิด: ยังไม่มีใน 3.6
  • (D) Every version of Pythonทุกเวอร์ชันของ Python
    Wrong: versions before 3.10 do not have it.ผิด: เวอร์ชันเก่ากว่า 3.10 ไม่มี

ข้อ 9

9 / 10

With color = "green" and cases "red", "yellow", _, what is printed?จาก color = "green" และ case "red", "yellow", _ จะพิมพ์อะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) go or unknown (from case _)go or unknown (จาก case _)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) stopstop
    Wrong: stop is in case "red".ผิด: stop อยู่ใน case "red"
  • (B) waitwait
    Wrong: wait is in case "yellow".ผิด: wait อยู่ใน case "yellow"
  • (C) An error, because there is no case "green"Error เพราะไม่มี case "green"
    Wrong: case _ covers all remaining values.ผิด: case _ รองรับค่าที่เหลือทั้งหมด
  • (D) go or unknown (from case _)go or unknown (จาก case _)
    Correct! "green" matches neither red nor yellow, so case _ runs.ถูกต้อง! "green" ไม่ตรง red/yellow จึงเข้า case _

ข้อ 10

10 / 10

Which is not a benefit of using match instead of a long if/elif for one value?ข้อใดไม่ใช่ข้อดีของการใช้ match แทน if/elif ยาวๆ ในการเทียบค่าเดียว?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) Makes number-range comparisons like >= 80 easierทำให้เปรียบเทียบช่วงตัวเลข เช่น >= 80 ได้ง่ายกว่า

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Easier to read with many casesอ่านง่ายกว่าเมื่อมีหลายกรณี
    Wrong: that is a real benefit.ผิด: นั่นเป็นข้อดีจริง
  • (B) Can combine values in one case with |รวมหลายค่าใน case เดียวด้วย | ได้
    Wrong: that is a real benefit.ผิด: นั่นเป็นข้อดีจริง
  • (C) Has a clear default with case _มี case _ เป็นค่าเริ่มต้นชัดเจน
    Wrong: that is a real benefit.ผิด: นั่นเป็นข้อดีจริง
  • (D) Makes number-range comparisons like >= 80 easierทำให้เปรียบเทียบช่วงตัวเลข เช่น >= 80 ได้ง่ายกว่า
    Correct! For number ranges, if/elif still fits better than match.ถูกต้อง! สำหรับช่วงตัวเลข if/elif ยังเหมาะกว่า match