Python is operator
- Python provided another important operator to the programmer.
- is Operator in python: To know the reference pointing to the same object.
- The syntax for is operator in python
Oprand1 is oprand2
is operator will return True is both references pointing to the same object, otherwise false. - Example
x = 10
y = 10
x is y => True
y is x => Truepython is operator In the above example is operator gives result True as both references pointing to the same object.
How does it work? To know this read post Immutable concept in python
- This concept is known as object reusing in python.
- Reference comparison in python
- We perform python is operator by is operator.
- Consider another Example :
x = 256
y = 256
x is y => Trueis operator in python In above example is operator gives result True as both reference pointing to same object.
- Now see one more example as
x = 257
y = 257
x is y => Falseis operator in python In the above example is operator gives result False as both references pointing to different objects.
Why?
Because Reusing the same object such type of flexibility applicable to 0 to 256 only, because it is the most commonly used range for programming. - Reusing the same object only in the following ranges
int Data type => 0 to 256
Boolean Data type => Always reuse same object ( 2 objects)
String Data type => Always reuse the same object ( 2 objects)
Float Data type => Always new object created.
Complex Data type => Always new object created. - For floating point, Data type reusing concept is not applicable.
Example
x = 10.0
y = 10.0
x is y => False - For Complex Data type reusing concept is not applicable.
Example
x = 10 + 1j
y = 10 + 1j
x is y => False - Note : is operator in python used for refernce comparison within applicable ranges.
Leave a Reply