Operator precedence - not True == False
Hiện tượng lạ
print(not True == False) # False ❌
print(not (True == False)) # True
# Tương tự
print(not 1 == 2) # False
print(not (1 == 2)) # TrueWhat the Python?! not True == False không phải True sao?! 🤔
Giải thích
Operator precedence
# Python evaluate:
not True == False
# = not (True == False) # == có precedence cao hơn not
# = not False
# = True
# KHÔNG phải:
# = (not True) == False
# = False == False
# = TrueBảng precedence (cao → thấp)
1. () # Parentheses
2. ** # Exponentiation
3. +x, -x, ~x # Unary plus, minus, NOT
4. *, /, //, % # Multiplication, Division
5. +, - # Addition, Subtraction
6. <<, >> # Shifts
7. & # Bitwise AND
8. ^ # Bitwise XOR
9. | # Bitwise OR
10. ==, !=, <, >, <=, >=, is, in # Comparisons
11. not # Boolean NOT
12. and # Boolean AND
13. or # Boolean ORVí dụ phổ biến
not và ==
# not có precedence THẤP hơn ==
not x == y # = not (x == y)
# Dùng ()
(not x) == y # Rõ ràng hơnand và or
# and có precedence cao hơn or
a or b and c # = a or (b and c)
# Không phải
(a or b) and cArithmetic và comparison
# Arithmetic trước comparison
2 + 3 == 5 # = (2 + 3) == 5 = True
1 < 2 + 3 # = 1 < (2 + 3) = TrueBest Practice
✅ Luôn dùng ()
# ✅ Rõ ràng
if (not value) == expected:
pass
# ❌ Confusing
if not value == expected:
passTóm tắt
Operator precedence:
- Comparison (
==) >notand>or- ✅ Luôn dùng
()để rõ ràng
Nhớ: Khi nghi ngờ, dùng ()!
Last updated on
Python