Home »
Python
Membership Operators in Python
Here, we are going to implement a python program to find square and cube of a given number by creating functions.
Submitted by IncludeHelp, on August 13, 2018
Membership Operators are the operators, which are used to check whether a value/variable exists in the sequence like string, list, tuples, sets, dictionary or not.
These operator returns either True or False, if a value/variable found in the list, its returns True otherwise it returns False.
Python Membership Operators
Operator |
Description |
Example |
in |
It returns True, if a variable/value found in the sequence. |
10 in list1 |
not in |
It returns True, if a variable/value does not found in the sequence. |
10 not in list1 |
Example:
# Python example of "in" and "not in" Operators
# declare a list and a string
str1 = "Hello world"
list1 = [10, 20, 30, 40, 50]
# Check 'w' (capital exists in the str1 or not
if 'w' in str1:
print "Yes! w found in ", str1
else:
print "No! w does not found in " , str1
# check 'X' (capital) exists in the str1 or not
if 'X' not in str1:
print "yes! X does not exist in ", str1
else:
print "No! X exists in ", str1
# check 30 exists in the list1 or not
if 30 in list1:
print "Yes! 30 found in ", list1
else:
print "No! 30 does not found in ", list1
# check 90 exists in the list1 or not
if 90 not in list1:
print "Yes! 90 does not exist in ", list1
else:
print "No! 90 exists in ", list1
Output
Yes! w found in Hello world
yes! X does not exist in Hello world
Yes! 30 found in [10, 20, 30, 40, 50]
Yes! 90 does not exist in [10, 20, 30, 40, 50]
TOP Interview Coding Problems/Challenges