Identity Operators
Identity Operators అంటే ఏమిటి?
Pythonలో Identity Operators రెండు variables ఒకే objectను సూచిస్తున్నాయా లేదా తెలుసుకోవడానికి ఉపయోగిస్తారు.
Identity Operators రెండు:
is– రెండు variables ఒకే objectను సూచిస్తేTrueఇస్తుంది.is not– రెండు variables ఒకే objectను సూచించకపోతేTrueఇస్తుంది.
ఎందుకు ఉపయోగిస్తాము?
రెండు variablesలో ఉన్న values ఒకేలా ఉన్నాయా అని మాత్రమే తెలుసుకోవడానికి == ఉపయోగిస్తాము. కానీ అవి memoryలో ఒకే objectనా అని తెలుసుకోవడానికి is ఉపయోగిస్తాము.
ఇది ఎలా పనిచేస్తుంది?
is valuesను మాత్రమే compare చేయదు. Variables ఒకే object referenceను కలిగి ఉన్నాయా అని check చేస్తుంది. అందువల్ల ఒక variableను మరొక variableకు assign చేసినప్పుడు రెండూ ఒకే objectను సూచించవచ్చు.
is not దానికి వ్యతిరేకంగా పనిచేస్తుంది. Objects వేర్వేరుగా ఉంటే ఇది True ఇస్తుంది.
== మరియు is మధ్య తేడా
==values సమానంగా ఉన్నాయా అని check చేస్తుంది.isobjects ఒకటేనా అని check చేస్తుంది.
సాధారణంగా Noneతో check చేసేటప్పుడు is None ఉపయోగించడం మంచి పద్ధతి.
Simple Explanation
సులభంగా చెప్పాలంటే, == రెండు boxesలో ఉన్న వస్తువులు ఒకేలా ఉన్నాయా అని చూస్తుంది. is అయితే రెండు names ఒకే boxను చూపిస్తున్నాయా అని చూస్తుంది.
ఉదాహరణకు, b = a అని రాస్తే a మరియు b ఒకే objectను సూచిస్తాయి. కాబట్టి a is b ఫలితం True అవుతుంది.
Real-Life Example
ఒక libraryలో ఒకే పుస్తకానికి రెండు labels ఉన్నాయని ఊహించండి. రెండు labels ఒకే physical bookను చూపిస్తే అది is లాంటిది. రెండు వేర్వేరు booksలో title మరియు author ఒకేలా ఉన్నా, అవి physicalగా ఒకే book కాకపోవచ్చు. ఇది == మరియు is మధ్య తేడాను అర్థం చేసుకోవడానికి సహాయపడుతుంది.
Code Examples
is, is not మరియు == మధ్య తేడా
a = [1, 2]
b = a
c = [1, 2]
print(a is b)
print(a is c)
print(a == c)
print(a is not c)
b = a వలన a మరియు b ఒకే list objectను సూచిస్తాయి. cలో అదే values ఉన్నా, అది కొత్త list object. అందువల్ల a is c false అయినా, a == c true అవుతుంది.
True False True True
Noneతో Identity Operator ఉపయోగించడం
value = None
print(value is None)
print(value is not None)
valueలో None ఉందో లేదో check చేయడానికి is None ఉపయోగించాము. Value None కావడంతో మొదటి ఫలితం True, రెండవది False అవుతుంది.
True False
Line-by-Line Explanation
ఈ lineలో ఒక కొత్త list object తయారవుతుంది మరియు దానికి
a అనే name ఇస్తుంది.
ఈ lineలో కొత్త list తయారు కాదు.
b కూడా a సూచిస్తున్న అదే list objectను సూచిస్తుంది.
ఇక్కడ values ఒకేలా ఉన్నా, కొత్త list object తయారవుతుంది. కాబట్టి
c object, a objectకు వేరు.
a మరియు b ఒకే objectను సూచిస్తున్నాయా అని check చేస్తుంది. అవును కాబట్టి True print అవుతుంది.
a మరియు c వేర్వేరు list objects కాబట్టి False print అవుతుంది.
ఈ line objectsలోని valuesను compare చేస్తుంది. రెండు listsలో values ఒకేలా ఉన్నాయి కాబట్టి
True print అవుతుంది.
a మరియు c ఒకే object కాదు. అందువల్ల is not ఫలితం True అవుతుంది.
ఈ lineలో
valueకి ప్రత్యేకమైన empty value అయిన Noneను assign చేస్తుంది.
value నిజంగా None objectను సూచిస్తుందా అని check చేస్తుంది. అవును కాబట్టి True వస్తుంది.
value None కాకపోతే True రావాలి. కానీ ఇక్కడ value None కాబట్టి False print అవుతుంది.
Try It
Try this Python code:
first = [10, 20]
second = first
third = [10, 20]
print(first is second)
print(first is third)
print(first == third)
Practice
Practice: numbers_a, numbers_b, మరియు numbers_cను ఉపయోగించి మూడు results print చేయండి: మొదటిది isతో same object check, రెండవది isతో different object check, మూడవది ==తో equal values check.
Hint: numbers_b = numbers_a కాబట్టి ఈ రెండు names ఒకే objectను సూచిస్తాయి. numbers_c కొత్త list.
Practice: status = None అని assign చేసి, status is None మరియు status is not None resultsను print చేయండి.
Hint: Noneను check చేయడానికి is మరియు is not ఉపయోగించండి.
Common Mistakes
== మరియు is ఎప్పుడూ ఒకే పని చేస్తాయని అనుకోవడం.== valuesను compare చేస్తుంది. is object identityని compare చేస్తుంది.
కొత్తగా తయారైన రెండు lists valuesలో సమానంగా ఉండవచ్చు, కానీ objects వేర్వేరుగా ఉంటాయి.
Noneను check చేయడానికి value == None మాత్రమే ఉపయోగించడం.Noneతో identity check కోసం సాధారణంగా value is None లేదా value is not None ఉపయోగించండి.
b = a రాసినప్పుడు కొత్త copy తయారవుతుందని అనుకోవడం.b = a కొత్త object తయారు చేయదు. a మరియు b ఒకే objectను సూచిస్తాయి.
Interview Questions
Pythonలో Identity Operators ఏమిటి?
Answer: Identity Operators is మరియు is not. ఇవి రెండు variables ఒకే objectను సూచిస్తున్నాయా లేదా check చేస్తాయి.
== మరియు is మధ్య తేడా ఏమిటి?
Answer: == values సమానమా అని check చేస్తుంది. is రెండు variables ఒకే objectను సూచిస్తున్నాయా అని check చేస్తుంది.
a = [1], b = a అయినప్పుడు a is b ఫలితం ఏమిటి?
Answer: ఫలితం True. ఎందుకంటే b కూడా a సూచిస్తున్న అదే list objectను సూచిస్తుంది.
Pythonలో Noneను check చేయడానికి ఏ operator ఉపయోగించడం మంచిది?
Answer: is operatorను ఉపయోగించడం మంచిది. ఉదాహరణకు, value is None.
Exam Questions
Identity Operators పేర్లు రాయండి.
Answer:
is మరియు is not.
క్రింది code output ఏమిటి?x = [1, 2]
y = x
print(x is y)
Answer:
True. ఎందుకంటే x మరియు y ఒకే list objectను సూచిస్తున్నాయి.
క్రింది code output ఏమిటి?x = [1, 2]
y = [1, 2]
print(x is y)
print(x == y)
Answer:
Output: False, తరువాత True. Lists వేర్వేరు objects, కానీ వాటి values ఒకేలా ఉన్నాయి.
is not operator ఎప్పుడు True ఇస్తుంది?
Answer:
రెండు variables ఒకే objectను సూచించనప్పుడు is not True ఇస్తుంది.
Quick Revision
isరెండు variables ఒకే objectను సూచిస్తున్నాయా అని check చేస్తుంది.is notరెండు variables వేర్వేరు objectsనా అని check చేస్తుంది.==valuesను compare చేస్తుంది;isobject identityని compare చేస్తుంది.b = aఅయితేaమరియుbసాధారణంగా ఒకే objectను సూచిస్తాయి.Noneను check చేయడానికిis Noneమరియుis not Noneఉపయోగించండి.
