Python Membership Operators Example

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 in list ):
print “Line 2 – b is not available in the given list”
else:
print “Line 2 – b is available in the given list”

a = 2
if ( a in list ):
print “Line 3 – a is available in the given list”
else:
print “Line 3 – a is not available in the given list”

Output:

Line 1 – a is not available in the given list
Line 2 – b is not available in the given list
Line 3 – a is available in the given list

Related Posts

  • 76
    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
  • 66
    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 (…
    Tags: print, python, example, operators, learn
  • 65
    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, example, learn, operators
  • 60
    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, operators
  • 60
    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

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: