Python Identity Operators Example

There are used to compare the memory locations of two operators.

Example:

#!/usr/bin/python

a = 20
b = 20

if ( a is b ):
print “Line 1 – a and b have same identity”
else:
print “Line 1 – a and b do not have same identity”

if ( id(a) == id(b) ):
print “Line 2 – a and b have same identity”
else:
print “Line 2 – a and b do not have same identity”

b = 30
if ( a is b ):
print “Line 3 – a and b have same identity”
else:
print “Line 3 – a and b do not have same identity”

if ( a is not b ):
print “Line 4 – a and b do not have same identity”
else:
print “Line 4 – a and b have same identity”

Output:

Line 1 – a and b have same identity
Line 2 – a and b have same identity
Line 3 – a and b do not have same identity

Related Posts

  • 84
    These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators. Example: #!/usr/bin/python a = 21 b = 10 c = 0 if ( a == b ):    print "Line 1 - a is equal to b" else:…
    Tags: print, python, pythonexample, example, operator, learn, operators
  • 66
    Example: !/usr/bin/python a = 21 b = 10 c = 0 c = a + b print "Line 1 - Value of c is ", c c += a print "Line 2 - Value of c is ", c c *= a print "Line 3 - Value of c is…
    Tags: print, example, python, operator, operators
  • 66
    Example: #!/usr/bin/python a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print "Line 1 - a is available in the given list" else: print "Line 1 - a is not available in the given list" if ( b not…
    Tags: print, python, operators, example, learn
  • 66
    Example: #!/usr/bin/python a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011…
    Tags: print, example, operators, python, operator
  • 64
    Example:  #!/usr/bin/python x = True y = False print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x) Output: x and y is False x or y is True not x is False Few more example: 1. if (five == 5) AND (two ==…
    Tags: example, print, operators, python, learn

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: