Python Logical Operators
In this page:
result = condition1 and condition2
result = condition1 or condition2
result = not condition
and Operator
and तभी True होता है जब दोनों operands सही हों; अगर कोई भी पक्ष गलत हो तो पूरी expression गलत होती है, जो उन दो शर्तों को जोड़ने के लिए स्वाभाविक पसंद है जिनका *दोनों* सही होना ज़रूरी हो।
उदाहरण: The and Operator
print(True and False)
print(5 > 2 and 3 > 1)
or Operator
or तब True होता है जब कम से कम एक operand सही हो, और तभी विफल होता है जब दोनों पक्ष गलत हों — जब भी कई शर्तों में से किसी एक के पूरा होने पर नतीजा मिलना चाहिए, यह सही औज़ार है।
उदाहरण: The or Operator
print(True or False)
print(5 > 10 or 3 > 1)
not Operator
not boolean value को पलट देता है: not True False बन जाता है और उल्टा भी, और अक्सर किसी condition को उसके विपरीत रूप में दोबारा लिखे बिना साफ़ ढंग से उलटने के लिए इस्तेमाल होता है।
उदाहरण: The not Operator
print(not True)
Logical Operators को मिलाना
एक ही expression में and, or और not को मिलाना जल्दी अस्पष्ट हो सकता है, इसलिए sub-conditions को parentheses में लपेटना — (a and b) or c — Python और अगले पढ़ने वाले, दोनों के लिए इच्छित evaluation क्रम को स्पष्ट कर देता है।
उदाहरण: Combining Logical Operators
a, b, c = True, False, True
print((a and b) or c)
Short-circuit Evaluation
Python logical evaluation को short-circuit करती है: a and b में, अगर a पहले से गलत है तो b को evaluate ही नहीं किया जाता, और a or b में, अगर a पहले से सही है तो b छोड़ दिया जाता है।
यह केवल optimization नहीं है — code कभी-कभी जान-बूझकर इस पर निर्भर करता है, जैसे None object पर error से बचने के लिए obj and obj.value।
उदाहरण: Short-circuit Evaluation
obj = None
print(obj and obj.upper())
Chapter Quiz — Complete all 16 topics to unlock
0/16 topics done
Complete these topics first:
- Python print()
- Python input()
- Python Format Strings
- Python f-strings
- Python String Formatting
- Python Arithmetic Operators
- Python Relational Operators
- Python Logical Operators
- Python Bitwise Operators
- Python Assignment Operators
- Python Increment & Decrement
- Python Ternary Operator
- Python Operator Precedence
- Python Identity Operators
- Python Membership Operators
- Python Operators