Thursday, 23 September 2021

Python: Membership operators: in, not in

Membership operators are used to check whether an item is a member of array, list or string etc.,

 

Operator

Description

in

Return True, if the element is member of a list, string etc, else False

not in

Return True, if the element is not a member of a list, string etc, else False

 

membership_operator_demo.py

primes = [2, 3, 5, 7]
msg = 'Hello'

print('2 in primes -> ', (2 in primes))
print('2 not in primes -> ', (2 not in primes))

print('H in msg -> ', ('H' in msg))
print('H not in msg -> ', ('H' not in msg))

 

Output

2 in primes ->  True
2 not in primes ->  False
H in msg ->  True
H not in msg ->  False

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment