Membership Operators
Membership Operators అంటే ఏమిటి?
ఒక value ఒక collection లో ఉందా లేదా తెలుసుకోవడానికి Python లో Membership Operators ఉపయోగిస్తాము.
Membership Operators రెండు ఉన్నాయి:
- in — value collection లో ఉంటే True ఇస్తుంది.
- not in — value collection లో లేకపోతే True ఇస్తుంది.
ఎందుకు ఉపయోగిస్తాము?
ఒక item list లో ఉందా, ఒక word string లో ఉందా, లేదా ఒక key dictionary లో ఉందా అని సులభంగా తనిఖీ చేయడానికి వీటిని ఉపయోగిస్తాము.
ఎలా పనిచేస్తాయి?
in operator collection లో value కోసం వెతుకుతుంది. value కనిపిస్తే True, కనిపించకపోతే False ఇస్తుంది.
not in operator కూడా వెతుకుతుంది. value కనిపించకపోతే True, కనిపిస్తే False ఇస్తుంది.
ఎక్కడ ఉపయోగపడతాయి?
- List లో item ఉందా అని తనిఖీ చేయడానికి
- String లో word లేదా character ఉందా అని తెలుసుకోవడానికి
- User ఇచ్చిన value అనుమతించబడిన values లో ఉందా అని తనిఖీ చేయడానికి
- Dictionary లో key ఉందా అని చూడడానికి
Syntax
value in collection
value not in collection
Simple Explanation
ఒక shopping list లో apple ఉందా అని చూడాలి అనుకుందాం. apple in shopping_list అని రాస్తే, apple ఉంటే True వస్తుంది. లేకపోతే False వస్తుంది.
అదే విధంగా apple లేకపోతే తెలుసుకోవడానికి apple not in shopping_list అని రాయవచ్చు.
ఈ operators ఎప్పుడూ Boolean result అయిన True లేదా False ఇస్తాయి.
Real-Life Example
ఒక పాఠశాలలో అనుమతించబడిన subjects జాబితా ఉందని అనుకుందాం. ఒక student Python subject ఎంచుకున్నప్పుడు, Python ఆ జాబితాలో ఉందా అని తనిఖీ చేయవచ్చు. Python ఉంటే enrollment అనుమతించవచ్చు. లేకపోతే ఆ subject అందుబాటులో లేదని చెప్పవచ్చు.
Code Examples
List లో item ఉందా అని తనిఖీ చేయడం
fruits = ["apple", "banana", "orange"]
search_item = "banana"
print(search_item in fruits)
print("mango" not in fruits)
మొదటి print statement banana fruits list లో ఉందా అని తనిఖీ చేస్తుంది. రెండవ print statement mango fruits list లో లేదో తనిఖీ చేస్తుంది.
True True
String లో word ఉందా అని తనిఖీ చేయడం
message = "python is easy"
print("python" in message)
print("java" not in message)
String కూడా characters collection లాగా పనిచేస్తుంది. అందువల్ల ఒక word లేదా character string లో ఉందా అని in మరియు not in తో తనిఖీ చేయవచ్చు.
True True
Line-by-Line Explanation
ఈ line లో fruits అనే list సృష్టించబడుతుంది. ఇందులో మూడు fruit names నిల్వ చేయబడతాయి.
ఈ line లో తనిఖీ చేయాల్సిన item అయిన banana ను search_item అనే variable లో ఉంచుతాము.
Python search_item విలువ fruits list లో ఉందా అని తనిఖీ చేస్తుంది. banana list లో ఉన్నందున result True అవుతుంది. print దాన్ని screen పై చూపిస్తుంది.
Python mango fruits list లో లేదో తనిఖీ చేస్తుంది. mango list లో లేదు కాబట్టి not in result True అవుతుంది.
ఈ line లో python is easy అనే text ను message అనే variable లో నిల్వ చేస్తాము.
python అనే word message string లో ఉందా అని Python తనిఖీ చేస్తుంది. అది ఉన్నందున True output వస్తుంది.
java అనే word message string లో లేదో Python తనిఖీ చేస్తుంది. java లేకపోవడంతో not in result True అవుతుంది.
Try It
Try this Python code:
colors = ["red", "blue", "green"]
color = "blue"
print(color in colors)
Practice
Practice: subjects అనే list లో Python ఉందా మరియు Java లేదని Membership Operators ఉపయోగించి రెండు results print చేయండి.
Hint: in operator ను Python కోసం, not in operator ను Java కోసం ఉపయోగించండి.
Practice: languages అనే list లో Python, Java, C++ ఉన్నాయి. Ruby ఆ list లో ఉందా అని తనిఖీ చేయండి.
Hint: Ruby కోసం in operator ఉపయోగించండి.
Practice: ఒక string లో cat అనే word ఉందా మరియు dog లేదని తనిఖీ చేయండి.
Hint: String తో కూడా in మరియు not in ఉపయోగించవచ్చు.
Common Mistakes
in మరియు not in ను assignment operator లాగా ఉపయోగించడం. ఉదాహరణకు result = item in items బదులుగా operator ను తప్పుగా రాయడం.
సరైన రూపం item in items లేదా item not in items. వీటి result True లేదా False అవుతుంది.
String లో uppercase మరియు lowercase letters ఒకటే అని అనుకోవడం. ఉదాహరణకు Python మరియు python ఒకటే అని భావించడం.
Membership checking case-sensitive. String లో ఉన్న letters యొక్క case ను సరిగ్గా ఉపయోగించాలి.
List లో ఉన్న item కి బదులుగా మొత్తం list ను వెతకడం.
వెతకాల్సిన value ను ఎడమవైపు, list లేదా string ను కుడివైపు రాయాలి. ఉదాహరణకు “banana” in fruits.
not in result ను తప్పుగా అర్థం చేసుకోవడం.
Value collection లో లేకపోతే మాత్రమే not in True ఇస్తుంది. Value ఉంటే False ఇస్తుంది.
Interview Questions
Python లో Membership Operators ఏవి?
Answer:
Python లో Membership Operators in మరియు not in.
in operator ఏమి చేస్తుంది?
Answer:
ఒక value collection లో ఉందా అని తనిఖీ చేస్తుంది. value ఉంటే True, లేకపోతే False ఇస్తుంది.
not in operator ఎప్పుడు True ఇస్తుంది?
Answer:
తనిఖీ చేస్తున్న value collection లో లేనప్పుడు not in True ఇస్తుంది.
Membership Operators ఏ data types తో ఉపయోగించవచ్చు?
Answer:
వాటిని lists, strings, tuples, sets మరియు dictionaries వంటి collections తో ఉపయోగించవచ్చు. Dictionary విషయంలో సాధారణంగా keys ను తనిఖీ చేస్తాయి.
Exam Questions
ఈ code output ఏమిటి?
numbers = [1, 2, 3] print(2 in numbers)
Answer:
Output True. ఎందుకంటే 2 numbers list లో ఉంది.
ఈ code output ఏమిటి?
colors = ["red", "blue"]
print("green" not in colors)
Answer:
Output True. ఎందుకంటే green colors list లో లేదు.
Membership Operators ఏ రకమైన result ఇస్తాయి?
Answer:
అవి Boolean result ఇస్తాయి. అంటే True లేదా False.
String లో ఒక word ఉందా అని ఎలా తనిఖీ చేస్తారు?
Answer:
word in string రూపాన్ని ఉపయోగించాలి. ఉదాహరణకు “python” in message.
Quick Revision
- in ఒక value collection లో ఉందా అని తనిఖీ చేస్తుంది.
- not in ఒక value collection లో లేదో తనిఖీ చేస్తుంది.
- Membership Operators result ఎప్పుడూ True లేదా False.
- ఇవి lists మరియు strings తో సులభంగా ఉపయోగించవచ్చు.
- String checking case-sensitive.
- Dictionary లో Membership Operators సాధారణంగా keys ను తనిఖీ చేస్తాయి.
