Booleans: true/false values and comparisons Booleans: ค่าบูลีนและการเปรียบเทียบ

Learn True / False, the == != < > operators, and logical and / or / not เรียนรู้ True / False, ตัวดำเนินการ == != < > และ logical and / or / not

A boolean value has only two possible values: True and False. It says whether a condition holds. Booleans usually come from a comparison such as == or !=, and several conditions can be combined with and, or, and not. Booleans are the basis of every if statement and every loop condition you will meet later. This lesson follows on from Literals and Variables, where you first saw True / False.

ค่าบูลีน (Boolean) มีเพียงสองค่า คือ True และ False ใช้บอกว่าเงื่อนไขเป็นจริงหรือเท็จ — มักได้มาจากการเปรียบเทียบ เช่น == != และนำมารวมด้วย and or not ค่าบูลีนเป็นพื้นฐานของคำสั่ง if และเงื่อนไขของลูปทุกแบบที่จะเจอในบทถัดๆ ไป บทนี้ต่อจาก Literals and Variables ที่เคยเห็น True / False แล้ว

1 Boolean literals 1 Boolean Literals

A boolean literal in Python is one of exactly two values: True and False (the first letter is a capital). Use type() to check that a value is of type bool.

Literal บูลีนใน Python มีสองค่าเท่านั้น: True และ False (ตัวอักษรแรกเป็นตัวพิมพ์ใหญ่) — ใช้ type() ดูชนิดได้ว่าเป็น bool

[1]
[output]
True
False
<class 'bool'>

2 Comparison operators (==, !=, <, >, <=, >=) 2 ตัวดำเนินการเปรียบเทียบ (==, !=, <, >, <=, >=)

Each comparison operator below returns a boolean — True when the condition holds and False when it does not. Note that == (compare) is different from = (assign a value to a variable).

ตัวดำเนินการเปรียบเทียบด้านล่างคืนค่าเป็นบูลีน — True เมื่อเงื่อนไขเป็นจริง และ False เมื่อไม่เป็นจริง สังเกตว่า == (เปรียบเทียบ) ต่างจาก = (กำหนดค่าให้ตัวแปร)

Operator Nameชื่อ Descriptionความหมาย
== Equal toเท่ากับ True if the two values are equalTrue เมื่อสองค่าเท่ากัน
!= Not equal toไม่เท่ากับ True if the two values are not equalTrue เมื่อสองค่าไม่เท่ากัน
< / > Less / greater thanน้อยกว่า / มากกว่า True if the left value is less / greater than the rightTrue เมื่อค่าซ้ายน้อยกว่า / มากกว่าค่าขวา
<= / >= Less / greater than or equalน้อยกว่าหรือเท่ากับ / มากกว่าหรือเท่ากับ True if less/greater than or equalTrue เมื่อน้อยกว่า/มากกว่า หรือ เท่ากัน

2.1 Try == and != 2.1 ทดลอง == และ !=

Use == to ask whether two values are equal, and != to ask whether they are not equal. The result is True or False.

ใช้ == เพื่อถามว่าสองค่าเท่ากันหรือไม่ และ != เพื่อถามว่าไม่เท่ากันหรือไม่ ผลลัพธ์เป็น True หรือ False

[2]
[output]
True
True
False

2.2 Try <, >, <=, >= 2.2 ทดลอง <, >, <=, >=

These operators compare order (size). <= and >= are also True when the two values are equal.

ตัวดำเนินการเหล่านี้เปรียบเทียบลำดับ (ขนาด) โดย <= และ >= จะเป็น True เมื่อสองค่าเท่ากันด้วย

[4]
[output]
True
True
False

3 Logical operators (and, or, not) 3 ตัวดำเนินการตรรกะ (and, or, not)

Logical operators combine two boolean conditions, or flip one.

Logical operators ใช้รวมเงื่อนไขบูลีนสองอัน หรือกลับค่าความจริงของเงื่อนไขเดียว

Operator Nameชื่อ Descriptionความหมาย
and Logical AND True only when both sides are true เป็น True เมื่อทั้งสองข้างเป็นจริง
or Logical OR True when at least one side is true เป็น True เมื่ออย่างน้อยหนึ่งข้างเป็นจริง
not Logical NOT Flips the value — True becomes False and back กลับค่าความจริง — True เป็น False และกลับกัน

3.1 Try and, or, not 3.1 ทดลอง and, or, not

Run these examples of and, or, not, and a condition that mixes a logical operator with a comparison.

ลองรันตัวอย่าง and or not และเงื่อนไขที่รวมตัวดำเนินการตรรกะเข้ากับการเปรียบเทียบ

[3]
[output]
False
True
False
True

3.2 Example: check whether an age is in a range 3.2 ตัวอย่าง: ตรวจว่าอายุอยู่ในช่วงที่กำหนดหรือไม่

A comparison can be stored in a variable (here is_adult) and reused. Combining two comparisons with and is a common way to test "between two numbers".

ผลของการเปรียบเทียบเก็บไว้ในตัวแปรได้ (ในตัวอย่างคือ is_adult) แล้วนำกลับมาใช้ซ้ำ การรวมสองการเปรียบเทียบด้วย and เป็นวิธีที่ใช้บ่อยเพื่อตรวจว่า "อยู่ระหว่างสองจำนวน"

[5]
[output]
True
True
False

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. Boolean type — Built-in Types — Boolean (the True / False literals) (literal True / False)
  2. Comparisons — Built-in Types — Comparisons (the == != < > operators) (ตัวดำเนินการ == != < >)
  3. Boolean operations — Built-in Types — Boolean operations (and / or / not) (and / or / not)

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

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

Which are the correct boolean literals in Python?Literal บูลีนที่ถูกต้องใน Python คือข้อใด?

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

เฉลย / Answer: (A) True and FalseTrue และ False

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

  • (A) True and FalseTrue และ False
    Correct! Python uses True and False with a capital first letter.ถูกต้อง! Python ใช้ True และ False โดยตัวอักษรแรกเป็นตัวพิมพ์ใหญ่
  • (B) true and falsetrue และ false
    Wrong: all-lowercase is not a boolean literal in Python.ผิด: ตัวพิมพ์เล็กทั้งหมดไม่ใช่ literal บูลีนใน Python
  • (C) TRUE and FALSETRUE และ FALSE
    Wrong: Python does not use all-caps as a boolean literal.ผิด: Python ไม่ใช้ตัวพิมพ์ใหญ่ทั้งหมดเป็น literal บูลีน
  • (D) 1 and 0 only1 และ 0 เท่านั้น
    Wrong: 1 and 0 are numbers, not boolean literals themselves.ผิด: 1 และ 0 เป็นตัวเลข ไม่ใช่ literal บูลีนโดยตรง

ข้อ 2

2 / 10

What does print(10 == 10) show?ผลลัพธ์ของ print(10 == 10) คือข้อใด?

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

เฉลย / Answer: (B) TrueTrue

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

  • (A) FalseFalse
    Wrong: 10 equals 10, so the result is True.ผิด: 10 เท่ากับ 10 ดังนั้นผลเป็น True
  • (B) TrueTrue
    Correct! == returns True when the two values are equal.ถูกต้อง! == คืน True เมื่อสองค่าเท่ากัน
  • (C) 1010
    Wrong: a comparison returns a boolean, not a number.ผิด: การเปรียบเทียบคืนบูลีน ไม่ใช่ตัวเลข
  • (D) NoneNone
    Wrong: the result of a comparison is not None.ผิด: ผลของการเปรียบเทียบไม่ใช่ None

ข้อ 3

3 / 10

What does the != operator mean?ตัวดำเนินการ != หมายถึงอะไร?

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

เฉลย / Answer: (C) Not equal toไม่เท่ากัน

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

  • (A) Equal toเท่ากัน
    Wrong: equal to uses ==.ผิด: เท่ากันใช้ ==
  • (B) Greater thanมากกว่า
    Wrong: greater than uses >.ผิด: มากกว่าใช้ >
  • (C) Not equal toไม่เท่ากัน
    Correct! != is non-equality; it returns True when the two values differ.ถูกต้อง! != คือ Non-equality คืน True เมื่อสองค่าไม่เท่ากัน
  • (D) Less than or equal toน้อยกว่าหรือเท่ากับ
    Wrong: less than or equal uses <=.ผิด: น้อยกว่าหรือเท่ากับใช้ <=

ข้อ 4

4 / 10

What does print(True and False) show?ผลลัพธ์ของ print(True and False) คือข้อใด?

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

เฉลย / Answer: (D) FalseFalse

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

  • (A) TrueTrue
    Wrong: and is True only when both sides are true.ผิด: and เป็น True ได้เมื่อทั้งสองข้างเป็นจริง
  • (B) 11
    Wrong: the result is a boolean, not a number.ผิด: ผลลัพธ์เป็นบูลีน ไม่ใช่ตัวเลข
  • (C) NoneNone
    Wrong: and returns a truth value, not None.ผิด: and คืนค่าความจริง ไม่ใช่ None
  • (D) FalseFalse
    Correct! True and False gives False because one side is false.ถูกต้อง! True and False ได้ False เพราะมีข้างหนึ่งเป็นเท็จ

ข้อ 5

5 / 10

What does print(not False) show?ผลลัพธ์ของ print(not False) คือข้อใด?

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

เฉลย / Answer: (A) TrueTrue

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

  • (A) TrueTrue
    Correct! not flips the value — not False gives True.ถูกต้อง! not กลับค่าความจริง — not False ได้ True
  • (B) FalseFalse
    Wrong: not flips False to True.ผิด: not จะกลับ False เป็น True
  • (C) 00
    Wrong: the result is a boolean, not a number.ผิด: ผลลัพธ์เป็นบูลีน ไม่ใช่ตัวเลข
  • (D) NoneNone
    Wrong: not returns a boolean, not None.ผิด: not คืนบูลีน ไม่ใช่ None

ข้อ 6

6 / 10

What is the difference between = and ==?อะไรคือความต่างระหว่าง = กับ ==?

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

เฉลย / Answer: (B) = assigns a value to a variable; == compares whether two values are equal= กำหนดค่าให้ตัวแปร ส่วน == เปรียบเทียบว่าเท่ากันหรือไม่

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

  • (A) They are the same and interchangeableเหมือนกันทุกอย่าง ใช้แทนกันได้
    Wrong: they do completely different jobs.ผิด: ทำหน้าที่ต่างกันโดยสิ้นเชิง
  • (B) = assigns a value to a variable; == compares whether two values are equal= กำหนดค่าให้ตัวแปร ส่วน == เปรียบเทียบว่าเท่ากันหรือไม่
    Correct! = assigns; == returns a boolean from a comparison.ถูกต้อง! = คือการกำหนดค่า ส่วน == คืนบูลีนจากการเปรียบเทียบ
  • (C) == assigns; = compares== กำหนดค่า ส่วน = เปรียบเทียบ
    Wrong: it is the other way round — = assigns.ผิด: สลับกัน = คือกำหนดค่า
  • (D) = works only with numbers= ใช้กับตัวเลขเท่านั้น
    Wrong: = assigns to a variable of any type.ผิด: = ใช้กำหนดค่าให้ตัวแปรได้ทุกชนิด

ข้อ 7

7 / 10

What does print(7 <= 7) show?ผลลัพธ์ของ print(7 <= 7) คือข้อใด?

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

เฉลย / Answer: (C) True, because 7 equals 7True เพราะ 7 เท่ากับ 7

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

  • (A) False, because 7 is not less than 7False เพราะ 7 ไม่ได้น้อยกว่า 7
    Wrong: <= is true when 'less than' OR 'equal'.ผิด: <= เป็นจริงเมื่อ 'น้อยกว่า' หรือ 'เท่ากับ'
  • (B) An errorError
    Wrong: comparing numbers works fine.ผิด: การเปรียบเทียบตัวเลขทำได้ปกติ
  • (C) True, because 7 equals 7True เพราะ 7 เท่ากับ 7
    Correct! <= also accepts the equal case.ถูกต้อง! <= ยอมรับกรณีเท่ากันด้วย
  • (D) 77
    Wrong: a comparison returns a boolean.ผิด: การเปรียบเทียบคืนบูลีน

ข้อ 8

8 / 10

When does or give False?or จะให้ผลเป็น False เมื่อใด?

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

เฉลย / Answer: (D) When both sides are falseเมื่อทั้งสองข้างเป็นเท็จ

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

  • (A) When at least one side is trueเมื่ออย่างน้อยหนึ่งข้างเป็นจริง
    Wrong: in that case or gives True.ผิด: กรณีนั้น or ให้ True
  • (B) When both sides are trueเมื่อทั้งสองข้างเป็นจริง
    Wrong: if both are true, or is certainly True.ผิด: ทั้งสองข้างจริง or ยิ่งให้ True
  • (C) or is never Falseor ไม่เคยให้ False
    Wrong: or is False when both sides are false.ผิด: or ให้ False ได้เมื่อทั้งสองข้างเป็นเท็จ
  • (D) When both sides are falseเมื่อทั้งสองข้างเป็นเท็จ
    Correct! or is False only when every side is false.ถูกต้อง! or เป็น False เฉพาะเมื่อทุกข้างเป็นเท็จ

ข้อ 9

9 / 10

To check that x is between 1 and 10 inclusive, how do you write it?ต้องการตรวจว่า x อยู่ระหว่าง 1 ถึง 10 (รวมปลายทั้งสอง) เขียนอย่างไร?

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

เฉลย / Answer: (B) 1 <= x and x <= 101 <= x and x <= 10

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

  • (A) x >= 1 or x <= 10x >= 1 or x <= 10
    Wrong: using or is true for almost every value; you need and.ผิด: ใช้ or ทำให้เป็นจริงเกือบทุกค่า ต้องใช้ and
  • (B) 1 <= x and x <= 101 <= x and x <= 10
    Correct! Both conditions must hold, so use and.ถูกต้อง! ต้องเป็นจริงทั้งสองเงื่อนไข จึงใช้ and
  • (C) x = 1 and 10x = 1 and 10
    Wrong: = assigns a value and is not a range check.ผิด: = คือกำหนดค่า และไม่ใช่วิธีตรวจช่วง
  • (D) x < 1 and x > 10x < 1 and x > 10
    Wrong: this is always false (no value is both < 1 and > 10).ผิด: เงื่อนไขนี้เป็นเท็จเสมอ (ไม่มีค่าใดทั้งน้อยกว่า 1 และมากกว่า 10)

ข้อ 10

10 / 10

What does print(5 > 3 and 2 > 9) show?ผลลัพธ์ของ print(5 > 3 and 2 > 9) คือข้อใด?

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

เฉลย / Answer: (C) False, because 2 > 9 is falseFalse เพราะ 2 > 9 เป็นเท็จ

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

  • (A) True, because 5 > 3 is trueTrue เพราะ 5 > 3 เป็นจริง
    Wrong: and needs both sides true, but 2 > 9 is false.ผิด: and ต้องจริงทั้งสองข้าง แต่ 2 > 9 เป็นเท็จ
  • (B) An errorError
    Wrong: the expression is syntactically valid.ผิด: นิพจน์นี้ถูกต้องตามไวยากรณ์
  • (C) False, because 2 > 9 is falseFalse เพราะ 2 > 9 เป็นเท็จ
    Correct! True and False gives False.ถูกต้อง! True and False ได้ False
  • (D) 22
    Wrong: the result of and / a comparison is a boolean.ผิด: ผลของ and/การเปรียบเทียบเป็นบูลีน