Python में and ऑपरेटर एक लॉजिकल ऑपरेटर है जो केवल तभी True लौटाता है जब मूल्यांकन की जा रही दोनों शर्तें सत्य हों, अन्यथा यह False लौटाता है। यह ऑपरेटर आपको कई बूलियन अभिव्यक्तियों को एक ही सशर्त कथन में संयोजित करने की अनुमति देता है, जिससे आपके कार्यक्रमों में अधिक जटिल निर्णय लेना संभव होता है। Python अन्य प्रोग्रामिंग भाषाओं में पाए जाने वाले && जैसे प्रतीकों के बजाय and कीवर्ड का उपयोग करता है।
मूल and ऑपरेटर सिंटैक्स
and ऑपरेटर दो ऑपरेंड लेता है और मूल्यांकन करता है कि दोनों सत्य हैं या नहीं।
condition1 and condition2 and ऑपरेटर केवल तभी True लौटाता है जब condition1 और condition2 दोनों True के रूप में मूल्यांकित हों। यदि कोई भी शर्त False है, तो पूरी अभिव्यक्ति False के रूप में मूल्यांकित होती है।
उदाहरण 1: सरल and ऑपरेटर
x = 5
y = 10
if x > 0 and y > 0:
print("Both numbers are positive") Both numbers are positive
उदाहरण 2: False शर्त
x = 5
y = -10
if x > 0 and y > 0:
print("Both numbers are positive")
else:
print("At least one number is not positive") At least one number is not positive
चूंकि y नकारात्मक है, दूसरी शर्त विफल हो जाती है, जिससे पूरी अभिव्यक्ति False हो जाती है।
and ऑपरेटर के लिए सत्य तालिका
and ऑपरेटर विभिन्न बूलियन संयोजनों के साथ कैसे काम करता है, इसे समझना आवश्यक है।
| शर्त 1 | शर्त 2 | परिणाम |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
and ऑपरेटर केवल तभी True लौटाता है जब दोनों ऑपरेंड True हों।
उदाहरण: सभी सत्य तालिका मामले
# Case 1: True and True = True
if True and True:
print("Case 1: True") # This executes
# Case 2: True and False = False
if True and False:
print("Case 2: True")
else:
print("Case 2: False") # This executes
# Case 3: False and True = False
if False and True:
print("Case 3: True")
else:
print("Case 3: False") # This executes
# Case 4: False and False = False
if False and False:
print("Case 4: True")
else:
print("Case 4: False") # This executes Case 1: True Case 2: False Case 3: False Case 4: False
कई शर्तों को संयोजित करना
and ऑपरेटर का उपयोग आमतौर पर सशर्त कथनों में कई तुलना संचालन को संयोजित करने के लिए किया जाता है।
उदाहरण 1: आयु और लाइसेंस जांच
age = 25
has_license = True
if age >= 18 and has_license:
print("You can drive")
else:
print("You cannot drive") You can drive
उदाहरण 2: संख्या सीमा जांच
number = 15
if number > 10 and number < 20:
print(f"{number} is between 10 and 20") 15 is between 10 and 20
उदाहरण 3: कई शर्तें
a = 10
b = 10
c = -10
if a > 0 and b > 0:
print("The numbers are greater than 0")
if a > 0 and b > 0 and c > 0:
print("All numbers are greater than 0")
else:
print("At least one number is not greater than 0") The numbers are greater than 0 At least one number is not greater than 0
यह उदाहरण दिखाता है कि and कैसे कई शर्तों को एक साथ जोड़ सकता है।
शॉर्ट-सर्किट मूल्यांकन
Python का and ऑपरेटर शॉर्ट-सर्किट मूल्यांकन का उपयोग करता है, जिसका अर्थ है कि यदि पहली शर्त False है, तो Python दूसरी शर्त का मूल्यांकन नहीं करता क्योंकि परिणाम पहले से ही False निर्धारित है।
उदाहरण: शॉर्ट-सर्किट प्रदर्शित करना
def check_first():
print("First condition evaluated")
return False
def check_second():
print("Second condition evaluated")
return True
# Short-circuit in action
if check_first() and check_second():
print("Both are True")
else:
print("At least one is False") First condition evaluated At least one is False
ध्यान दें कि "Second condition evaluated" कभी प्रिंट नहीं होता क्योंकि check_first() ने False लौटाया, इसलिए Python ने check_second() का मूल्यांकन छोड़ दिया। यह अनुकूलन प्रदर्शन में सुधार करता है और अनावश्यक गणनाओं को रोकता है।
उदाहरण 2: शून्य से विभाजन से बचना
x = 0
y = 10
# Safe division check
if x != 0 and y / x > 2:
print("y/x is greater than 2")
else:
print("Cannot divide or condition not met") Cannot divide or condition not met
शॉर्ट-सर्किट मूल्यांकन शून्य से विभाजन त्रुटि को रोकता है क्योंकि x != 0 False है, इसलिए y / x का कभी मूल्यांकन नहीं होता।
बूलियन वेरिएबल्स के साथ and का उपयोग
बूलियन वेरिएबल्स को तुलना ऑपरेटरों के बिना सीधे and ऑपरेटर के साथ संयोजित किया जा सकता है।
उदाहरण 1: प्रत्यक्ष बूलियन जांच
is_logged_in = True
has_permission = True
if is_logged_in and has_permission:
print("Access granted")
else:
print("Access denied") Access granted
उदाहरण 2: कई बूलियन फ्लैग
a = 10
b = 12
c = 0
if a and b and c:
print("All numbers have boolean value as True")
else:
print("At least one number has boolean value as False") At least one number has boolean value as False
Python में, 0 बूलियन संदर्भ में False के रूप में मूल्यांकित होता है, जबकि गैर-शून्य संख्याएं True के रूप में मूल्यांकित होती हैं।
कई शर्तों को जोड़ना
आप एक साथ कई शर्तों की जांच करने के लिए कई and ऑपरेटरों को एक साथ जोड़ सकते हैं।
उदाहरण 1: ग्रेड आवश्यकताएं
attendance = 85
homework_score = 90
exam_score = 88
if attendance >= 80 and homework_score >= 85 and exam_score >= 85:
print("You passed the course with good grades")
else:
print("Requirements not met") You passed the course with good grades
उदाहरण 2: पासवर्ड सत्यापन
password = "SecurePass123"
has_upper = any(c.isupper() for c in password)
has_lower = any(c.islower() for c in password)
has_digit = any(c.isdigit() for c in password)
min_length = len(password) >= 8
if has_upper and has_lower and has_digit and min_length:
print("Password is strong")
else:
print("Password does not meet requirements") Password is strong
उदाहरण 3: तिथि सीमा सत्यापन
year = 2024
month = 6
day = 15
if year > 2000 and month >= 1 and month <= 12 and day >= 1 and day <= 31:
print("Valid date")
else:
print("Invalid date") Valid date
Python 'and' vs अन्य भाषाएं '&&'
C, C++, Java या JavaScript जैसी भाषाओं के विपरीत जो लॉजिकल AND के लिए && का उपयोग करती हैं, Python and कीवर्ड का उपयोग करता है।
Python सिंटैक्स:
x = 5
y = 10
if x < y and y < 15:
print("Both conditions are True") अन्य भाषाएं (JavaScript/Java/C++):
// This does NOT work in Python
if (x < y && y < 15) {
console.log("Both conditions are True");
} Python में && का उपयोग करने का प्रयास SyntaxError का परिणाम देगा। Python में हमेशा and कीवर्ड का उपयोग करें।
# This will cause an error
# if x > 0 && y > 0: # SyntaxError
# print("Error!")
# Correct Python syntax
if x > 0 and y > 0:
print("Correct!") and को or ऑपरेटर के साथ संयोजित करना
आप एक ही अभिव्यक्ति में and और or ऑपरेटरों को मिला सकते हैं, लेकिन याद रखें कि and की or से अधिक प्राथमिकता है। मूल्यांकन क्रम को नियंत्रित करने के लिए कोष्ठक का उपयोग करें।
उदाहरण 1: कोष्ठक के बिना
age = 25
has_ticket = True
is_vip = False
# and has higher precedence than or
if age >= 18 and has_ticket or is_vip:
print("You can enter") You can enter
यह इस प्रकार मूल्यांकित होता है: (age >= 18 and has_ticket) or is_vip
उदाहरण 2: स्पष्टता के लिए कोष्ठक के साथ
score = 75
extra_credit = 10
# Explicit grouping
if (score >= 70 and score < 80) or extra_credit >= 20:
print("Grade: B") Grade: B
उदाहरण 3: जटिल शर्त
temperature = 25
is_sunny = True
is_weekend = True
if (temperature > 20 and is_sunny) or is_weekend:
print("Good day for outdoor activities") Good day for outdoor activities
सामान्य उपयोग के मामले
उपयोग मामला 1: फॉर्म सत्यापन
username = "alice"
password = "password123"
email = "[email protected]"
if len(username) > 0 and len(password) >= 8 and "@" in email:
print("Registration successful")
else:
print("Please check your input") Registration successful
उपयोग मामला 2: पात्रता जांचकर्ता
age = 22
citizen = True
registered = True
if age >= 18 and citizen and registered:
print("You are eligible to vote")
else:
print("You are not eligible to vote") You are eligible to vote
उपयोग मामला 3: छूट योग्यता
purchase_amount = 150
is_member = True
has_coupon = True
if purchase_amount > 100 and (is_member or has_coupon):
discount = 0.15
final_price = purchase_amount * (1 - discount)
print(f"You qualify for 15% discount. Final price: ${final_price}") You qualify for 15% discount. Final price: $127.5
उपयोग मामला 4: पहुंच नियंत्रण
user_role = "admin"
is_authenticated = True
session_valid = True
if is_authenticated and session_valid and user_role == "admin":
print("Access granted to admin panel")
else:
print("Access denied") Access granted to admin panel
उपयोग मामला 5: डेटा सत्यापन
data = [1, 2, 3, 4, 5]
if len(data) > 0 and all(isinstance(x, int) for x in data) and min(data) >= 0:
print("Data is valid")
else:
print("Invalid data") Data is valid
नेस्टेड शर्तें vs and ऑपरेटर
and ऑपरेटर का उपयोग करना अक्सर कई if कथनों को नेस्ट करने से अधिक साफ होता है।
नेस्टेड दृष्टिकोण (कम पठनीय):
age = 25
income = 50000
if age >= 21:
if income >= 30000:
print("Loan approved")
else:
print("Insufficient income")
else:
print("Age requirement not met") and ऑपरेटर का उपयोग (अधिक पठनीय):
age = 25
income = 50000
if age >= 21 and income >= 30000:
print("Loan approved")
else:
print("Requirements not met") Loan approved
and का उपयोग करने वाला दूसरा दृष्टिकोण अधिक संक्षिप्त और समझने में आसान है।
बचने के लिए सामान्य गलतियां
गलती 1: and के बजाय && का उपयोग करना
# Wrong - SyntaxError
# if x > 0 && y > 0:
# print("Both positive")
# Correct
if x > 0 and y > 0:
print("Both positive") गलती 2: ऑपरेटर प्राथमिकता भूल जाना
# Ambiguous - may not work as intended
if x > 5 and y > 3 or z < 10:
print("Condition met")
# Better - use parentheses
if (x > 5 and y > 3) or z < 10:
print("Condition met") गलती 3: कई मानों को गलत तरीके से जांचना
x = 5
# Wrong - doesn't work as expected
# if x == 3 or 5 or 7: # Always True!
# Correct
if x == 3 or x == 5 or x == 7:
print("x is 3, 5, or 7")
# Even better - use 'in'
if x in [3, 5, 7]:
print("x is 3, 5, or 7") गलती 4: शॉर्ट-सर्किट पर विचार न करना
# Potentially unsafe
# if len(my_list) > 0 and my_list[0] > 10: # Error if my_list doesn't exist
# Safe approach
my_list = []
if my_list and len(my_list) > 0 and my_list[0] > 10:
print("First element is greater than 10") सर्वोत्तम प्रथाएं
अभ्यास 1: जटिल शर्तों के लिए कोष्ठक का उपयोग करना
# Clearer with parentheses
if (age >= 18 and age <= 65) and (has_license or has_permit):
print("Can drive") अभ्यास 2: जटिल शर्तों को वेरिएबल्स में तोड़ना
# Instead of this:
# if user.age >= 18 and user.has_account and user.verified and not user.banned:
# grant_access()
# Do this:
is_adult = user.age >= 18
has_valid_account = user.has_account and user.verified
not_banned = not user.banned
if is_adult and has_valid_account and not_banned:
print("Access granted") अभ्यास 3: संभावना के अनुसार शर्तों को क्रमबद्ध करना
# Put the most likely to fail condition first
def cheap_check():
return True
def expensive_operation():
return True
if cheap_check() and expensive_operation():
print("Both passed") अभ्यास 4: सार्थक नामों का उपयोग करना
# Poor
# if a and b and c:
# do_something()
# Better
is_authenticated = True
has_permission = True
is_active = True
if is_authenticated and has_permission and is_active:
print("Action allowed") इसे स्वयं आज़माएं
नीचे दिए गए कोड को संशोधित करके आपने जो सीखा है उसका अभ्यास करें। विभिन्न आउटपुट देखने के लिए मानों और शर्तों को बदलने का प्रयास करें!
// परिणाम देखने के लिए "कोड चलाएं" पर क्लिक करें
संबंधित विषय
अक्सर पूछे जाने वाले प्रश्न
Python में 'and' और '&&' के बीच क्या अंतर है?
Python लॉजिकल AND संचालन के लिए and कीवर्ड का उपयोग करता है। && प्रतीक का उपयोग C, Java और JavaScript जैसी अन्य भाषाओं में किया जाता है, लेकिन यह Python में SyntaxError का कारण बनेगा।
क्या and ऑपरेटर दोनों शर्तों का मूल्यांकन करता है?
हमेशा नहीं। Python शॉर्ट-सर्किट मूल्यांकन का उपयोग करता है: यदि पहली शर्त False है, तो दूसरी शर्त का मूल्यांकन नहीं किया जाता क्योंकि परिणाम पहले से ही False के रूप में जाना जाता है।
क्या मैं and को दो से अधिक शर्तों के साथ उपयोग कर सकता हूं?
हां, आप कई शर्तों को जोड़ सकते हैं: if a and b and c and d:। अभिव्यक्ति के True के रूप में मूल्यांकित होने के लिए सभी शर्तें True होनी चाहिए।
or की तुलना में and की प्राथमिकता क्या है?
and ऑपरेटर की or से अधिक प्राथमिकता है। इसका मतलब है कि a or b and c को a or (b and c) के रूप में मूल्यांकित किया जाता है। स्पष्टता के लिए कोष्ठक का उपयोग करें।
and का उपयोग करके मैं कैसे जांचूं कि कोई संख्या एक सीमा में है?
उपयोग करें: if 10 <= number <= 20: या if number >= 10 and number <= 20:। दोनों Python में मान्य हैं।
क्या मैं and को गैर-बूलियन मानों के साथ उपयोग कर सकता हूं?
हां। Python बूलियन संदर्भ में मानों का मूल्यांकन करता है। 0, None, खाली स्ट्रिंग्स और खाली संग्रह False हैं; बाकी सब कुछ True है।