Lecture-10 (6) Membership Operators

6) Membership Operators


PYthon Membership Operator
----Python IN Operator
---Python NOT IN Operator





Python IN Operator
The in operator is used to check if a character/substring/element exists in a sequence or not. Evaluate to True if it finds the specified element in a sequence otherwise False.

క్యారెక్టర్/సబ్‌స్ట్రింగ్/ఎలిమెంట్ సీక్వెన్స్‌లో ఉందో లేదో తనిఖీ చేయడానికి ఇన్ ఆపరేటర్ ఉపయోగించబడుతుంది. ఒక క్రమంలో పేర్కొన్న మూలకం తప్పు అని గుర్తించినట్లయితే అది ఒప్పు అని మూల్యాంకనం చేయండి.

Example: Example 1: In this code we have initialized a list, string, set, dictionary and a tuple. Then we use membership in operator to check if the element occurs in the corresponding sequences or not.

python code:

--------------------------------------------------------------------
# initialized some sequences
list1 = [1, 2, 3, 4, 5]
str1 = "Hello World"
set1 = {1, 2, 3, 4, 5}
dict1 = {1: "Geeks", 2:"for", 3:"geeks"}
tup1 = (1, 2, 3, 4, 5)

# using membership 'in' operator
# checking an integer in a list
print(2 in list1)

# checking a character in a string
print('O' in str1)

# checking an integer in aset
print(6 in set1)

# checking for a key in a dictionary
print(3 in dict1)

# checking for an integer in a tuple
print(9 in tup1)

-------------------------------------------------------------------------------
Output:

True
False
False
True
False



-------------------------------------------------------------------------------
Python NOT IN Operator
The ‘not in’ Python operator evaluates to true if it does not find the variable in the specified sequence and false otherwise.

'నాట్ ఇన్' పైథాన్ ఆపరేటర్ పేర్కొన్న క్రమంలో వేరియబుల్‌ను కనుగొనలేకపోతే ఒప్పు అని మరియు లేకపోతే తప్పు అని మూల్యాంకనం చేస్తుంది.

Example: 

python code:

--------------------------------------------------------------------
# initialized some sequences
list1 = [1, 2, 3, 4, 5]
str1 = "Hello World"
set1 = {1, 2, 3, 4, 5}
dict1 = {1: "Geeks", 2:"for", 3:"geeks"}
tup1 = (1, 2, 3, 4, 5)

# using membership 'not in' operator
# checking an integer in a list
print(2 not in list1)

# checking a character in a string
print('O' not in str1)

# checking an integer in aset
print(6 not in set1)

# checking for a key in a dictionary
print(3 not in dict1)

# checking for an integer in a tuple
print(9 not in tup1)

-------------------------------------------------------------------------------


Output:
False
True
True
False
True



The operators.contains() Method
An alternative to Membership ‘in’ operator is the contains() function. This function is part of the Operator module in Python. The function take two arguments, the first is the sequence and the second is the value that is to be checked.

మెంబర్‌షిప్ 'ఇన్' ఆపరేటర్‌కు ప్రత్యామ్నాయం కలిగి() ఫంక్షన్. ఈ ఫంక్షన్ పైథాన్‌లోని ఆపరేటర్ మాడ్యూల్‌లో భాగం. ఫంక్షన్ రెండు వాదనలను తీసుకుంటుంది, మొదటిది క్రమం మరియు రెండవది తనిఖీ చేయవలసిన విలువ.


Syntax: operator.contains(sequence, value)


Example: In this code we have initialized a list, string, set, dictionary and a tuple. Then we use operator module’s contain() function to check if the element occurs in the corresponding sequences or not.


ఈ కోడ్‌లో మేము జాబితా, స్ట్రింగ్, సెట్, డిక్షనరీ మరియు టుపుల్‌ని ప్రారంభించాము. మూలకం సంబంధిత సీక్వెన్స్‌లలో సంభవిస్తుందో లేదో తనిఖీ చేయడానికి మేము ఆపరేటర్ మాడ్యూల్ యొక్క కలిగి() ఫంక్షన్‌ని ఉపయోగిస్తాము.













Comments