0% found this document useful (0 votes)
6 views2 pages

Python Membership Operators

The document provides examples of Python programs that demonstrate the use of membership operators 'in' and 'not in'. It includes code snippets for checking the existence of elements in lists, strings, and tuples. Each example illustrates how to use these operators to determine if a value is present or absent in a sequence.

Uploaded by

singh.lalit41269
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python Membership Operators

The document provides examples of Python programs that demonstrate the use of membership operators 'in' and 'not in'. It includes code snippets for checking the existence of elements in lists, strings, and tuples. Each example illustrates how to use these operators to determine if a value is present or absent in a sequence.

Uploaded by

singh.lalit41269
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Programs Demonstrating

Membership Operators

Membership Operators - Example Programs


These programs demonstrate the use of membership operators: in and not in .

Membership operators are used to test whether a value or variable is found in a


sequence (such as a list, string, or tuple).

1. WAP in Python to check if an element exists in a list


using in operator.

In [4]: # create a list of fruits and assign it to a variable


fruits = ["apple", "banana", "cherry"] # All String values

# Search for any fruit eg 'banana' in list object


# then print the result
if "banana" in fruits:
print("Banana is in the list")
else:
print("Banana is not in the list")

Banana is in the list

2. WAP in Python to check if an element does not exist in


a list using not in operator.

In [6]: # create a variable and assign it list of numbers


numbers = [1, 2, 3, 4, 5] # All Integer values

# Search if any number eg '10' doesn't exist in list using 'not in'
# then print the result
if 10 not in numbers:
print("10 is not in the list")
else:
print("10 is in the list")

10 is not in the list

3. WAP in Python to check if a character exists in a given


string or not.
In [8]: # create a variable and assign it any String Text
text = "Python Programming" # String type
# create a variable and assign it a character to search
token = "P" # String type

# Search if token is present in list using 'in'


# then print the result
if token in text:
print(token, "is present in the string")
else:
print(token, "is not present in the string")

P is present in the string

4. WAP in Python to check if any colour doesn't exist in a


tuple using not in operator.

In [10]: # create a variable and assign tuple containing colours


colors = ("red", "green", "blue")

# Search if 'yellow' doesn't exist in tuple using 'not in'


# then print the result
if "yellow" not in colors:
print("Yellow is not in the tuple")
else:
print("Yellow is in the tuple")

Yellow is not in the tuple

5. WAP in Python to validate specific vacancy from list of


vacancies.
In [12]: # create a variable and assign it list of vacancies
vacancies_for = ["teacher", "co-ordinator", "principal"]

# create a variable and assign it job applied for


applied_for = "professor"

# Search if job is present in list of vacancies using 'in' operator


# then print the result
if applied_for in vacancies_for:
print("Thankyou for applying")
else:
print("Sorry. This post doesn't exists")

Sorry. This post doesn't exists

You might also like