Python - தருக்க செயலிகள்
தருக்க செயலிகள் நிபந்தனை கூற்றுகளை இணைக்கப் பயன்படுகின்றன. பைத்தானுக்கு மூன்று தருக்க செயலிகள் உள்ளன:
and
செயல்பாடு: இரண்டு கூற்றுகளும் உண்மையாக இருந்தால் True தரும்
சின்னம்: and
or
செயல்பாடு: கூற்றுகளில் ஒன்று உண்மையாக இருந்தால் True தரும்
சின்னம்: or
not
செயல்பாடு: முடிவைத் தலைகீழாக மாற்றுகிறது
சின்னம்: not
and செயலி
and முக்கியச்சொல் ஒரு தருக்க செயலியாகும், மேலும் நிபந்தனை கூற்றுகளை இணைக்கப் பயன்படுகிறது. முழு வெளிப்பாடும் உண்மையாக இருக்க இரண்டு நிபந்தனைகளும் உண்மையாக இருக்க வேண்டும்.
உதவிக்குறிப்பு:
and செயலி அனைத்து நிபந்தனைகளும் உண்மையாக இருக்க வேண்டும். ஒரு நிபந்தனை கூட False ஆக இருந்தால், முழு வெளிப்பாடும் False ஆகும்.
எடுத்துக்காட்டு
a என்பது b ஐ விடப் பெரியதா மற்றும் c என்பது a ஐ விடப் பெரியதா என்று சோதிக்கவும்:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
மதிப்பீடு படிப்படியாக:
நிபந்தனை 1: a > b → 200 > 33 → True
நிபந்தனை 2: c > a → 500 > 200 → True
and முடிவு: True and True → True
வெளியீடு: "Both conditions are True"
or செயலி
or முக்கியச்சொல் ஒரு தருக்க செயலியாகும், மேலும் நிபந்தனை கூற்றுகளை இணைக்கப் பயன்படுகிறது. முழு வெளிப்பாடும் உண்மையாக இருக்க குறைந்தபட்சம் ஒரு நிபந்தனை உண்மையாக இருக்க வேண்டும்.
குறிப்பு:
or செயலிக்கு குறைந்தபட்சம் ஒரு நிபந்தனை உண்மையாக இருந்தால் போதும். இரண்டு நிபந்தனைகளும் False ஆக இருந்தால் மட்டுமே முடிவு False ஆகும்.
எடுத்துக்காட்டு
a என்பது b ஐ விடப் பெரியதா அல்லது a என்பது c ஐ விடப் பெரியதா என்று சோதிக்கவும்:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
மதிப்பீடு படிப்படியாக:
நிபந்தனை 1: a > b → 200 > 33 → True
நிபந்தனை 2: a > c → 200 > 500 → False
or முடிவு: True or False → True
வெளியீடு: "At least one of the conditions is True"
not செயலி
not முக்கியச்சொல் ஒரு தருக்க செயலியாகும், மேலும் நிபந்தனை கூற்றின் முடிவைத் தலைகீழாக மாற்றப் பயன்படுகிறது.
உதவிக்குறிப்பு:
not செயலி ஒரு பூலியன் மதிப்பைத் தலைகீழாக மாற்றுகிறது: True → False, False → True
எடுத்துக்காட்டு
a என்பது b ஐ விடப் பெரியதல்ல என்று சோதிக்கவும்:
a = 33
b = 200
if not a > b:
print("a is NOT greater than b")
மதிப்பீடு படிப்படியாக:
அசல் நிபந்தனை: a > b → 33 > 200 → False
not பயன்பாடு: not False → True
முடிவு: if True → True
வெளியீடு: "a is NOT greater than b"
உண்மை அட்டவணைகள்
and செயலி உண்மை அட்டவணை
| நிபந்தனை 1 | நிபந்தனை 2 | முடிவு |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
நினைவில் கொள்ள: அனைத்தும் True ஆக இருக்க வேண்டும்
or செயலி உண்மை அட்டவணை
| நிபந்தனை 1 | நிபந்தனை 2 | முடிவு |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
நினைவில் கொள்ள: குறைந்தது ஒன்று True ஆக இருக்க வேண்டும்
not செயலி உண்மை அட்டவணை |
|
|---|---|
| அசல் மதிப்பு | not முடிவு |
| True | False |
| False | True |
பல செயலிகளை இணைத்தல்
நீங்கள் ஒரு வெளிப்பாட்டில் பல தருக்க செயலிகளை இணைக்கலாம். Python முதலில் not ஐ மதிப்பிடும், பிறகு and, பிறகு or.
செயலி முன்னுரிமை:
Python இல் தருக்க செயலிகளின் முன்னுரிமை வரிசை: not → and → or
எடுத்துக்காட்டு
and, or, மற்றும் not ஆகியவற்றை இணைத்தல்:
age = 25
is_student = False
has_discount_code = True
if (age < 18 or age > 65) and not is_student or has_discount_code:
print("Discount applies!")
மதிப்பீடு படிப்படியாக:
1. அடைப்புக்குறிக்குள்: age < 18 or age > 65 → 25 < 18 or 25 > 65 → False or False → False
2. not: not is_student → not False → True
3. and: False and True → False
4. or: False or has_discount_code → False or True → True
முடிவு: True → வெளியீடு: "Discount applies!"
தெளிவுக்காக அடைப்புக்குறிகளைப் பயன்படுத்துதல்
பல தருக்க செயலிகளை இணைக்கும்போது, உங்கள் கருத்துக்களைத் தெளிவுபடுத்தவும் மதிப்பீட்டு வரிசையைக் கட்டுப்படுத்தவும் அடைப்புக்குறிகளைப் பயன்படுத்தவும்.
எடுத்துக்காட்டு
சிக்கலான நிபந்தனைகளுக்கு அடைப்புக்குறிகளைப் பயன்படுத்துதல்:
temperature = 25
is_raining = False
is_weekend = True
if (temperature > 20 and not is_raining) or is_weekend:
print("Great day for outdoor activities!")
அடைப்புக்குறிகள் இல்லாமல்
# குழப்பமான மற்றும் தவறான மதிப்பீடு
if temperature > 20 and not is_raining or is_weekend:
print("Great day for outdoor activities!")
சிக்கல்: மதிப்பீட்டு வரிசை தெளிவாக இல்லை
அடைப்புக்குறிகளுடன்
# தெளிவான மற்றும் சரியான மதிப்பீடு
if (temperature > 20 and not is_raining) or is_weekend:
print("Great day for outdoor activities!")
நன்மை: மதிப்பீட்டு வரிசை தெளிவாக உள்ளது
மேலும் நடைமுறை எடுத்துக்காட்டுகள்
பயனர் அங்கீகரிப்பு சோதனை
username = "Tobias"
password = "secret123"
is_verified = True
if username and password and is_verified:
print("Login successful")
else:
print("Login failed")
மதிப்பீடு: "Tobias" and "secret123" and True → True and True and True → True
வரம்பு சோதனை
score = 85
if score >= 0 and score <= 100:
print("Valid score")
else:
print("Invalid score")
மதிப்பீடு: 85 >= 0 and 85 <= 100 → True and True → True
எண் வகை சோதனை
number = 15
if number > 0 and number % 2 == 0:
print("Positive even number")
elif number > 0 and number % 2 != 0:
print("Positive odd number")
else:
print("Non-positive number")
தள்ளுபடி தகுதி
age = 70
is_veteran = True
has_membership = False
if (age >= 65 or is_veteran) and not has_membership:
print("Eligible for senior/veteran discount")
elif has_membership:
print("Eligible for membership discount")
else:
print("No discount available")
செயலி முன்னுரிமை சுருக்கம்
| வரிசை | செயலி | விளக்கம் | எடுத்துக்காட்டு |
|---|---|---|---|
| 1 | () |
அடைப்புக்குறிகள் | (a and b) or c |
| 2 | not |
தலைகீழாக்குதல் | not a |
| 3 | and |
தருக்க AND | a and b |
| 4 | or |
தருக்க OR | a or b |
நினைவில் கொள்ள:
குழப்பத்தைத் தவிர்க்கவும், மேலும் தெளிவுக்காகவும் எப்போதும் அடைப்புக்குறிகளைப் பயன்படுத்தவும். (a and b) or c என்பது a and b or c ஐ விட தெளிவானது.
பயிற்சி
and செயலிக்கு என்ன தேவைப்படுகிறது?
பயிற்சி கேள்வி:
சிறந்த நடைமுறைகள்
(a and b) or c
is_valid போன்ற பூலியன் மாறிகளுக்கு
and பயன்படுத்தவும்: if 0 <= score <= 100: (Python இல் சிறப்பு தொடரியல்)