Boolean
Boolean అంటే ఏమిటి?
Python లో Boolean data type రెండు విలువలను మాత్రమే కలిగి ఉంటుంది: True మరియు False.
True అంటే ఒక విషయం నిజం అని అర్థం. False అంటే అది నిజం కాదు అని అర్థం.
Boolean ఎందుకు ఉపయోగిస్తాం?
ఏదైనా condition నిజమా కాదా తెలుసుకోవడానికి Boolean values ఉపయోగిస్తాం. ఉదాహరణకు, user login అయ్యాడా, వయస్సు 18 లేదా అంతకంటే ఎక్కువా, ఒక వస్తువు అందుబాటులో ఉందా వంటి విషయాలను తెలుసుకోవడానికి Boolean ఉపయోగపడుతుంది.
Boolean ఎలా పనిచేస్తుంది?
Comparison operators ఉపయోగించినప్పుడు Python సాధారణంగా True లేదా False ను return చేస్తుంది. ఉదాహరణకు 10 > 5 ఫలితం True. 10 == 5 ఫలితం False.
ప్రధాన Boolean operators
and: రెండు conditions నిజమైనప్పుడు మాత్రమేTrueఇస్తుంది.or: కనీసం ఒక condition నిజమైనాTrueఇస్తుంది.not: Boolean విలువను వ్యతిరేకంగా మార్చుతుంది.TrueనుFalseగా,FalseనుTrueగా మార్చుతుంది.
Boolean values ను తరువాత decision-making కోసం ఉపయోగిస్తాం. ప్రస్తుతానికి Boolean value ను create చేయడం, comparison result ను చూడడం, మరియు Boolean operators ఉపయోగించడం నేర్చుకుంటాం.
Simple Explanation
Boolean ను ఒక చిన్న yes/no answer లాగా ఊహించుకోవచ్చు. Python లో yes ను True తో, no ను False తో చూపిస్తాం.
Boolean values ను variable లో store చేయవచ్చు. రెండు values ను compare చేసినప్పుడు Python మనకు Boolean result ఇస్తుంది. ఉదాహరణకు, 5 == 5 అనేది True, ఎందుకంటే రెండు values సమానం. 5 != 5 అనేది False.
గమనిక: Boolean values ఎల్లప్పుడూ capital letter తో రాయాలి: True, False. true, false Python లో సరైన Boolean values కావు.
Real-Life Example
ఒక school gate వద్ద student entry ని పరిశీలిస్తున్నారని అనుకుందాం. Student దగ్గర valid ID card ఉంటే has_id = True. ID card లేకపోతే has_id = False. అలాగే student వయస్సు 18 లేదా అంతకంటే ఎక్కువా అని check చేయవచ్చు. ఈ రెండు conditions ఆధారంగా entry ఇవ్వాలా వద్దా అని నిర్ణయించవచ్చు.
Code Examples
Boolean values ను variables లో store చేయడం
is_logged_in = True
has_permission = False
print(is_logged_in)
print(has_permission)
ఈ code లో రెండు Boolean values ను variables లో store చేశాం. ఒక variable లో True, మరొక variable లో False ఉంది. print() ద్వారా వాటి విలువలను output గా చూపించాం.
True False
Comparison ద్వారా Boolean result పొందడం
age = 20
print(age >= 18)
print(age == 20)
print(age < 18)
ఇక్కడ age ను ఇతర values తో compare చేశాం. ప్రతి comparison result గా True లేదా False వస్తుంది. >= అంటే greater than or equal to, == అంటే equal to.
True True False
and, or, not operators ఉపయోగించడం
has_ticket = True
is_adult = True
can_enter = has_ticket and is_adult
print(can_enter)
print(not has_ticket)
and operator రెండు conditions నిజమైనప్పుడు True ఇస్తుంది. not operator Boolean value ను వ్యతిరేకంగా మార్చుతుంది. ఇక్కడ రెండు values True కావడంతో can_enter విలువ True అవుతుంది.
True False
Line-by-Line Explanation
is_logged_in అనే variable ను సృష్టించి దానికి True విలువ ఇస్తుంది. User login అయ్యాడని Python గుర్తుంచుకుంటుంది.
has_permission variable లో False ను store చేస్తుంది. Permission లేదని ఇది సూచిస్తుంది.
is_logged_in లో ఉన్న Boolean విలువను screen పై చూపిస్తుంది. Output గా True వస్తుంది.
has_permission లో ఉన్న విలువను చూపిస్తుంది. ఆ variable లో False ఉండటం వల్ల output గా False వస్తుంది.
age అనే variable లో 20 అనే number ను store చేస్తుంది. తరువాత ఈ value ను comparisons కోసం ఉపయోగిస్తాం.
age 18 కంటే ఎక్కువా లేదా 18 కి సమానమా అని check చేస్తుంది. 20 ఈ condition ను satisfy చేస్తుంది కాబట్టి True print అవుతుంది.
age మరియు 20 సమానమా అని check చేస్తుంది. రెండూ సమానం కాబట్టి True output వస్తుంది.
age 18 కంటే తక్కువా అని check చేస్తుంది. 20, 18 కంటే తక్కువ కాదు కాబట్టి False output వస్తుంది.
వ్యక్తి దగ్గర ticket ఉందని సూచించడానికి has_ticket లో True ను store చేస్తుంది.
వ్యక్తి adult అని సూచించడానికి is_adult లో True ను store చేస్తుంది.
and operator రెండు conditions ను కలుపుతుంది. రెండూ True కాబట్టి can_enter లో True store అవుతుంది.
వ్యక్తి లోపలికి వెళ్లగలడా అనే final Boolean result ను చూపిస్తుంది. రెండు conditions నిజం కావడంతో True output వస్తుంది.
not operator has_ticket విలువను reverse చేస్తుంది. has_ticket విలువ True కాబట్టి not has_ticket విలువ False అవుతుంది.
Try It
Try this Python code:
is_raining = True
has_umbrella = False
can_go_out = not is_raining or has_umbrella
print(can_go_out)
Practice
Practice:
is_weekend మరియు has_free_time అనే Boolean variables ను ఉపయోగించి, ఒక వ్యక్తి relax అవ్వగలడా లేదా అని can_relax variable లో store చేయండి. Weekend కూడా ఉండాలి, free time కూడా ఉండాలి. చివరగా result ను print చేయండి.
Hint:
రెండు conditions నిజమైనప్పుడు మాత్రమే result True కావాలి. అందుకే and operator ఉపయోగించండి.
Practice:
temperature = 35 ఉన్నప్పుడు temperature 30 కంటే ఎక్కువా అని comparison ద్వారా తెలుసుకోండి. వచ్చిన Boolean result ను print చేయండి.
Hint:
temperature > 30 వంటి comparison ఉపయోగించండి.
Common Mistakes
true లేదా false అని lowercase లో రాయడం.
Python లో సరైన Boolean values True మరియు False. మొదటి అక్షరం capital గా ఉండాలి.
ఒకే equal sign = ను comparison కోసం ఉపయోగించడం.
= assignment కోసం ఉపయోగిస్తాం. రెండు values సమానమా అని check చేయడానికి == ఉపయోగించాలి.
and మరియు or operators ను తప్పుగా ఉపయోగించడం.
రెండు conditions తప్పనిసరిగా నిజం కావాలంటే and ఉపయోగించండి. కనీసం ఒక condition నిజమైనా సరిపోతే or ఉపయోగించండి.
not operator result ను reverse చేస్తుందని గుర్తించకపోవడం.
not True ఫలితం False, not False ఫలితం True అని గుర్తుంచుకోండి.
Comparison result ను string లాగా quotes లో రాయడం.
Boolean result కోసం quotes ఉపయోగించకండి. is_ready = True సరైనది; is_ready = "True" ఒక string అవుతుంది.
Interview Questions
Python లో Boolean data type అంటే ఏమిటి?
Answer: Boolean data type రెండు values ను కలిగి ఉంటుంది: True మరియు False. Conditions యొక్క result చూపించడానికి దీనిని ఉపయోగిస్తారు.
Python లో సరైన Boolean values ఏమిటి?
Answer: సరైన Boolean values True మరియు False. ఇవి capital letters తో రాయాలి.
= మరియు == మధ్య తేడా ఏమిటి?
Answer: = ఒక value ను variable లో assign చేయడానికి ఉపయోగిస్తాం. == రెండు values సమానమా అని compare చేయడానికి ఉపయోగిస్తాం.
and operator ఎప్పుడు True ఇస్తుంది?
Answer: and operator లో ఉన్న అన్ని conditions True అయినప్పుడు మాత్రమే result True అవుతుంది.
not False result ఏమిటి?
Answer: not False result True. ఎందుకంటే not Boolean value ను reverse చేస్తుంది.
Exam Questions
ఈ code output ఏమిటి? print(10 > 5)
Answer:
True
ఈ code output ఏమిటి? print(10 == 5)
Answer:
False
has_card = True మరియు is_member = False ఉన్నప్పుడు has_card and is_member result ఏమిటి?
Answer:
False. ఎందుకంటే and లోని రెండు conditions True కావాలి.
or operator పని ఏమిటి?
Answer:
or operator లో కనీసం ఒక condition True అయినా result True అవుతుంది.
Boolean value ను variable లో ఎలా store చేస్తారు?
Answer:
is_available = True లేదా is_available = False వంటి assignment ద్వారా store చేస్తారు.
Quick Revision
- Boolean data type లో రెండు values మాత్రమే ఉంటాయి: True మరియు False.
- True మరియు False లో మొదటి అక్షరం capital గా ఉండాలి.
- = assignment కోసం, == comparison కోసం ఉపయోగిస్తాం.
- Comparison operators సాధారణంగా Boolean result ఇస్తాయి.
- and రెండు conditions నిజమైనప్పుడు True ఇస్తుంది.
- or కనీసం ఒక condition నిజమైనప్పుడు True ఇస్తుంది.
- not Boolean value ను reverse చేస్తుంది.
- Boolean values ను conditions మరియు decision-making లో ఉపయోగిస్తారు.
