is , not is ( Identity Operators)

x is y
x, y : input objects
Return boolean value True or False. Returns True if both side operands are of same identity, otherwise False

x is not y
x, y : input objects
Return boolean value True or False. Returns True if both side operands are NOT of same identity, otherwise False
a=30
b=30 
print(a is b ) # True 
a=300
b=300 
print(a is b ) # False
Why the second print() returns False ?
Identity of the object changed in second case ( Why ?) . (For integer -5 to 256, id will not change.).
By using is we are checking the identity of the object Not the Value so we got False in second case ( Identity remain same for integers between -5 and 256 ) .

Difference between id and value

Our is operator checks the identity only , not the value of the object.
Example 1
a=-6
b=-6
print("Using is :",a is b) 
print("using == :",a==b)
Output
Using is : False
using == : True
Example 2
a=30
b=30 
print(a is b ) # True 
a=300
b=300 
print(a is b ) # False
print(a == b ) # True

Using type of the object

x=500
print(type(x) is int) # True
Using float
x=100.34
print(type(x) is int)  # False
x=100.34
print(type(x) is float) # True
x=30
y=400
print(type(x) is type(y)) # True

Using list

More on list

Example
my_list1=[1,2,3]
my_list2=[4,5,6]
print(type(my_list1) is type(my_list2)) # True
Using frozenset()
my_list1=[1,2,3]
my_list2=frozenset([4,5,6])
print(type(my_list1) is type(my_list2)) # False
Using boolean
x=True
y=False
print(type(x) is type(y)) # True

Example using is not

x=56.78
y=100
print((x  is not y)) # True

All Python Operators
in and not in: The Membership operators id() Iterator any()



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer