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
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
True True False
2.2 Try <, >, <=, >= 2.2 ทดลอง <, >, <=, >=
These operators compare order (size). <= and >= are also True when
the two values are equal.
ตัวดำเนินการเหล่านี้เปรียบเทียบลำดับ (ขนาด) โดย <= และ >= จะเป็น True
เมื่อสองค่าเท่ากันด้วย
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 และเงื่อนไขที่รวมตัวดำเนินการตรรกะเข้ากับการเปรียบเทียบ
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 เป็นวิธีที่ใช้บ่อยเพื่อตรวจว่า "อยู่ระหว่างสองจำนวน"
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:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
-
Boolean type —
Built-in Types — Boolean
(the
True/Falseliterals) (literalTrue/False) -
Comparisons —
Built-in Types — Comparisons
(the
==!=<>operators) (ตัวดำเนินการ==!=<>) -
Boolean operations —
Built-in Types — Boolean operations
(
and/or/not) (and/or/not)