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]




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.