Logical Operators
Logical Operators అంటే ఏమిటి?
ఒకటి లేదా అంతకంటే ఎక్కువ conditions ను కలిపి ఒక నిర్ణయం తీసుకోవడానికి Logical Operators ఉపయోగిస్తాము. ఇవి సాధారణంగా True లేదా False ఫలితాన్ని ఇస్తాయి.
Python లోని మూడు Logical Operators
- and: అన్ని conditions
Trueఅయితే మాత్రమే ఫలితంTrue. - or: కనీసం ఒక condition
Trueఅయితే ఫలితంTrue. - not: Boolean ఫలితాన్ని తారుమారు చేస్తుంది.
TrueనుFalseగా,FalseనుTrueగా మారుస్తుంది.
ఎందుకు ఉపయోగిస్తాము?
ఒక నిర్ణయం తీసుకోవడానికి రెండు లేదా అంతకంటే ఎక్కువ నియమాలను కలపాల్సినప్పుడు Logical Operators ఉపయోగిస్తాము. ఉదాహరణకు, ఒక వ్యక్తి వయస్సు 18 లేదా అంతకంటే ఎక్కువగా ఉండాలి మరియు అతని దగ్గర ticket కూడా ఉండాలి.
ఎలా పనిచేస్తాయి?
and operator అన్ని conditions ను పరిశీలిస్తుంది. or operator కనీసం ఒక condition నిజమా అని చూస్తుంది. not ఒక condition యొక్క వ్యతిరేక ఫలితాన్ని ఇస్తుంది.
and Truth Table
True and True→TrueTrue and False→FalseFalse and True→FalseFalse and False→False
or Truth Table
True or True→TrueTrue or False→TrueFalse or True→TrueFalse or False→False
not Truth Table
not True→Falsenot False→True
Simple Explanation
రెండు తలుపులు తెరవడానికి రెండు తాళాలు అవసరమైతే అది and. రెండు తలుపుల్లో ఏదో ఒకటి తెరిచి ఉంటే సరిపోతే అది or. ఒక condition కు వ్యతిరేకంగా ఆలోచించాలంటే not ఉపయోగిస్తాము.
ఉదాహరణలు: age >= 18 and has_id, is_weekend or is_holiday, not is_closed.
Real-Life Example
ఒక సినిమా థియేటర్లోకి ప్రవేశించడానికి వ్యక్తి వయస్సు 18 లేదా అంతకంటే ఎక్కువగా ఉండాలి మరియు ticket ఉండాలి. ఈ రెండు conditions నిజమైతేనే ప్రవేశం లభిస్తుంది. ఇది and కు ఉదాహరణ.
ఒక రోజును సెలవు రోజుగా పరిగణించడానికి అది Saturday కావచ్చు లేదా holiday కావచ్చు. కనీసం ఒక condition నిజమైతే సరిపోతుంది. ఇది or కు ఉదాహరణ.
Code Examples
and మరియు or Operators
age = 20
has_ticket = True
can_enter = age >= 18 and has_ticket
print(can_enter)
is_weekend = False
is_holiday = True
can_relax = is_weekend or is_holiday
print(can_relax)
మొదటి condition లో వయస్సు 18 లేదా అంతకంటే ఎక్కువగా ఉండటం మరియు ticket ఉండటం రెండూ అవసరం. రెండవ condition లో weekend లేదా holiday లో ఏదో ఒకటి నిజమైతే సరిపోతుంది.
True True
not Operator
is_logged_in = False
can_view = not is_logged_in
print(can_view)
is_closed = True
is_open = not is_closed
print(is_open)
not operator Boolean value ను తారుమారు చేస్తుంది. అందువల్ల False కు not ఉపయోగిస్తే True వస్తుంది.
True False
మూడు Logical Operators కలిసి ఉపయోగించడం
age = 25
has_id = True
is_banned = False
can_enter = age >= 18 and has_id and not is_banned
print(can_enter)
ప్రవేశానికి వయస్సు సరిపోవాలి, ID ఉండాలి మరియు వ్యక్తి banned అయి ఉండకూడదు. చివరి condition లో not is_banned ఉపయోగించి banned కాకపోవడాన్ని పరీక్షిస్తున్నాము.
True
Line-by-Line Explanation
ఈ line లో age అనే variable లో 20 అనే వయస్సును నిల్వ చేస్తుంది. తరువాత age ను comparison condition లో ఉపయోగిస్తాము.
వ్యక్తి దగ్గర ticket ఉందని Boolean value
True తో సూచిస్తుంది.
మొదట age 18 లేదా అంతకంటే ఎక్కువా అని Python పరీక్షిస్తుంది. తరువాత ticket ఉందా అని చూస్తుంది. రెండు conditions నిజమైతే can_enter లో
True నిల్వ అవుతుంది.
can_enter లో ఉన్న Boolean ఫలితాన్ని screen పై చూపిస్తుంది.
ఈ రోజు weekend కాదని
False value తో సూచిస్తుంది.
ఈ రోజు holiday అని
True value తో సూచిస్తుంది.
weekend లేదా holiday లో కనీసం ఒకటి నిజమా అని పరీక్షిస్తుంది. holiday నిజం కావడంతో can_relax లో
True వస్తుంది.
విశ్రాంతి తీసుకోవచ్చా అనే Boolean ఫలితాన్ని print చేస్తుంది.
వినియోగదారు login కాలేదని
False value తో చూపిస్తుంది.
not operator False ను True గా మార్చుతుంది. అందువల్ల can_view లో True నిల్వ అవుతుంది.
can_view యొక్క Boolean ఫలితాన్ని screen పై చూపిస్తుంది.
స్థలం మూసి ఉందని
True value తో సూచిస్తుంది.
is_closed విలువ True కాబట్టి
not దాన్ని False గా మార్చుతుంది. అందువల్ల is_open విలువ False అవుతుంది.
స్థలం తెరిచి ఉందా లేదా అనే Boolean ఫలితాన్ని చూపిస్తుంది.
age variable లో 25 అనే వయస్సును నిల్వ చేస్తుంది.
వ్యక్తి దగ్గర ID ఉందని సూచిస్తుంది.
వ్యక్తి banned కాలేదని False value తో సూచిస్తుంది.
ఇక్కడ మూడు conditions ఉన్నాయి. వయస్సు సరిపోవాలి, ID ఉండాలి, banned అయి ఉండకూడదు. మూడు conditions నిజమైతే can_enter విలువ True అవుతుంది.
ప్రవేశానికి అనుమతి ఉందా అనే తుది ఫలితాన్ని చూపిస్తుంది.
Try It
Try this Python code:
age = 20
has_ticket = True
can_enter = age >= 18 and has_ticket
print(can_enter)
Practice
Practice: ఒక వ్యక్తి సినిమా చూడటానికి వెళ్లాడు. అతని age 16 మరియు అతని దగ్గర ticket ఉంది. age 18 లేదా అంతకంటే ఎక్కువగా ఉండాలి మరియు ticket ఉండాలి అనే నియమంతో and operator ఉపయోగించి can_enter ను కనుగొనండి.
Hint: age >= 18 and has_ticket అనే ఆకృతిని ఉపయోగించండి.
Practice: ఒక రోజు weekend కాకపోయినా holiday అయితే విశ్రాంతి తీసుకోవచ్చా అని or operator ఉపయోగించి పరీక్షించండి. is_weekend = False, is_holiday = True values ఉపయోగించండి.
Hint: is_weekend or is_holiday ను ఉపయోగించండి.
Practice: is_locked = True ఇచ్చినప్పుడు తలుపు unlock అయిందా అని not operator ఉపయోగించి కనుగొనండి.
Hint: is_unlocked = not is_locked వ్రాయండి.
Common Mistakes
and ఉపయోగించినప్పుడు ఒక condition True అయితే చాలు అని అనుకోవడం.and లోని అన్ని conditions True అయినప్పుడే మొత్తం ఫలితం True అవుతుంది.
or ఉపయోగించినప్పుడు అన్ని conditions True కావాలని అనుకోవడం.or లో కనీసం ఒక condition True అయినా మొత్తం ఫలితం True అవుతుంది.
not ను comparison operator లాగా తప్పుగా ఉపయోగించడం.not ఒక Boolean expression ఫలితాన్ని తారుమారు చేస్తుంది. ఉదాహరణకు not is_closed.
"and".and, or, not ను quotes లేకుండా Python operators గా వ్రాయాలి.
ప్రతి condition ను పూర్తిగా వ్రాయండి. ఉదాహరణకు
age >= 18 and has_ticket.
Interview Questions
Python లో Logical Operators ఏవి?
Answer: and, or, not అనే మూడు Logical Operators ఉన్నాయి.
and operator ఎప్పుడు True ఇస్తుంది?
Answer: దాని రెండు లేదా అన్ని conditions True అయినప్పుడు and మొత్తం ఫలితంగా True ఇస్తుంది.
or operator ఎప్పుడు True ఇస్తుంది?
Answer: కనీసం ఒక condition True అయినప్పుడు or True ఇస్తుంది.
not True ఫలితం ఏమిటి?
Answer: not True ఫలితం False.
not operator యొక్క ఉపయోగం ఏమిటి?
Answer: ఒక Boolean expression యొక్క ఫలితాన్ని వ్యతిరేకంగా మార్చడానికి not ఉపయోగిస్తాము.
Exam Questions
print(True and False) యొక్క output ఏమిటి?
Answer:
False
print(False or True) యొక్క output ఏమిటి?
Answer:
True
print(not False) యొక్క output ఏమిటి?
Answer:
True
ఒక student పరీక్షలో pass కావడానికి marks 35 లేదా అంతకంటే ఎక్కువగా ఉండాలి మరియు attendance True ఉండాలి. ఈ condition ను ఎలా వ్రాస్తారు?
Answer:
marks >= 35 and attendance
ఒక వ్యక్తి weekend లేదా holiday రోజున బయటకు వెళ్లవచ్చు. ఈ condition ను ఎలా వ్రాస్తారు?
Answer:
is_weekend or is_holiday
Quick Revision
andఅంటే అన్ని conditions True కావాలి.orఅంటే కనీసం ఒక condition True అయితే సరిపోతుంది.notBoolean ఫలితాన్ని తారుమారు చేస్తుంది.- Logical Operators సాధారణంగా
TrueలేదాFalseఫలితాన్ని ఇస్తాయి. - Multiple conditions ను కలిపి నిర్ణయం తీసుకోవడానికి Logical Operators ఉపయోగిస్తాము.
- తదుపరి lesson లో Identity Operators గురించి నేర్చుకుంటాము.
