6. Python Identity Operators
Python identity operators determine whether two objects have the same id. In Python, every object has an identity. This is an integer and we can print this id using the following built-in function:
a = 30 b = 20 print(id(a)) print(id(b))
If we write “if a is b” then the identities of a and b are compared and evaluates to True if both the identities are the same.
# Python Identity Operators
a = 30
b = 20
print(id(a))
print(id(b))
if a is b:
print("a is b")
else:
print("a is not b")
The above code will output like the following:
140012776053968 140012776053648 a is not b
Please note that the identity of an object is unique during the lifetime of the program.
7. Python Membership Operator
Python membership operators check whether a value is present in an object. For example,
# Python Membership Operator
a = list((1, 2, 3, 4))
b = 3
if b in a:
print("b is in a")
else:
print("b is not in a")
The program will output the following:








































0 Comments