Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Boolean Operations ใน Python: and, or, not


Boolean operations พื้นฐาน 3 ตัว:

  • and: x and y — คืน x ถ้า x เป็น False, มิฉะนั้นคืน y
  • or: x or y — คืน y ถ้า x เป็น False, มิฉะนั้นคืน x
  • not: not x — คืน True ถ้า x เป็น False, มิฉะนั้นคืน False

ตัวอย่างโค้ด

x = 25
y = 18
print(x or y)   # 25
print(x and y)  # 18
print(not x)    # False

x = 0
print(not x)    # True

ผลลัพธ์

25
18
False
True