Page Contents
Python Membership operators are used to check whether a value or variable is in a sequence (eg – String, List, Tuple, Dictionary, Set) or not. All the membership operators and their application are shown below in the form of a table taking x
and y
as 1
and [1,2,3,4]
. In the table, left operand x
is the value or variable and right operand y
is the sequence.
Operator | Example | Description | Result |
---|---|---|---|
in | x in y | Returns True if left operand is in the right operand else False | True |
not in | x not in y | Returns True if left operand is not in the right operand else False | False |
Now we will see about all the assignment operators in details with code –
Python IN Operator
Python IN operator Returns True
if left operand is in the right operand else False
.
Example 1: Python IN operator with various datatype
# in operator with text as right operand
print(" R in 'Rajkumar' =", "R" in "Rajkumar")
print(" v in 'Rajkumar' =", "v" in "Rajkumar")
# in operator with list as right operand
print(" R in '['R','A','J']' =", "R" in ['R','A','J'])
print(" v in '['R','A','J']' =", "v" in ['R','A','J'])
# in operator will act same for set and tuple as like list
# in operator with dictionary as right operand
# here in operator will check only dictionary key not values
print(" R in '{'R':1,'A':2,'J':3}' =", "R" in {'R':1,'A':2,'J':3})
print(" v in '{'R':1,'A':2,'J':'v'}' =", "v" in {'R':1,'A':2,'J':'v'})
# in operator with range function
print(" 2 in range(4) =", 2 in range(4))
print(" 6 in range(4) =", 6 in range(4))
R in 'Rajkumar' = True v in 'Rajkumar' = False R in '['R','A','J']' = True v in '['R','A','J']' = False R in '{'R':1,'A':2,'J':3}' = True v in '{'R':1,'A':2,'J':'v'}' = False 2 in range(4) = True 6 in range(4) = False
In the above code we have seen the application of Python IN operator with various types of Python sequence –
- First , We have used
in
operator with text as right operand . As"R"
letter is in the"Rajkumar"
text so it returnsTrue
but"v"
letter is not in the"Rajkumar"
text so it returnsFalse
. - Second , We have used
in
operator with list as right operand . As"R"
letter is in the['R','A','J']
list so it returnsTrue
but"v"
letter is not in the['R','A','J']
list so it returnsFalse
. - Third , We have used
in
operator with dictionary as right operand . As"R"
letter is in the{'R':1,'A':2,'J':3}
dictionary key so it returnsTrue
but"v"
letter is not in the{'R':1,'A':2,'J':3}
dictionary key so it returnsFalse
. - Last , We have used
in
operator with range function as right operand . As2
is in therange(4)
so it returnsTrue
but6
is not in therange(4)
so it returnsFalse
.
Python NOT IN operator
Python NOT IN operator Returns True
if left operand is not in the right operand else False
.
Example 1: Python NOT IN operator with various datatype
# not in operator with text as right operand
print(" R not in 'Rajkumar' =", "R" not in "Rajkumar")
print(" v not in 'Rajkumar' =", "v" not in "Rajkumar")
# not in operator with list as right operand
print(" R not in '['R','A','J']' =", "R" not in ['R','A','J'])
print(" v not in '['R','A','J']' =", "v" not in ['R','A','J'])
# not in operator will act same for set and tuple as like list
# not in operator with dictionary as right operand
# here in operator will check only dictionary key not values
print(" R not in '{'R':1,'A':2,'J':3}' =", "R" not in {'R':1,'A':2,'J':3})
print(" v not in '{'R':1,'A':2,'J':'v'}' =", "v" not in {'R':1,'A':2,'J':'v'})
# not in operator with range function
print(" 2 not in range(4) =", 2 not in range(4))
print(" 6 not in range(4) =", 6 not in range(4))
R not in 'Rajkumar' = False v not in 'Rajkumar' = True R not in '['R','A','J']' = False v not in '['R','A','J']' = True R not in '{'R':1,'A':2,'J':3}' = False v not in '{'R':1,'A':2,'J':'v'}' = True 2 not in range(4) = False 6 not in range(4) = True
In the above code we have seen the application of Python NOT IN operator with various types of Python sequence –
- First , We have used
not in
operator with text as right operand . As"R"
letter is in the"Rajkumar"
text so it returnsFalse
but"v"
letter is not in the"Rajkumar"
text so it returnsTrue
. - Second , We have used
not in
operator with list as right operand . As"R"
letter is in the['R','A','J']
list so it returnsFalse
but"v"
letter is not in the['R','A','J']
list so it returnsTrue
. - Third , We have used
not in
operator with dictionary as right operand . As"R"
letter is in the{'R':1, 'A':2, 'J':3}
dictionary key so it returnsFalse
but"v"
letter is not in the{'R':1,'A':2,'J':3}
dictionary key so it returnsTrue
. - Last , We have used
not in
operator with range function as right operand . As2
is in therange(4)
so it returnsFalse
but6
is not in therange(4)
so it returnsTrue
.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .