Monday, 24 June 2019

Membership Operators

Membership Operators:

>>> a="Hello Good morning!!"
>>> print("H" in a) #True
>>> print('h' in a) #False
>>> print('k' not in a) #True
>>> print('good' in a) #False
>>> print('Good' in a)#True
>>> print("Afternoon" in a) #False
>>> print(a in a) #True, this is not the right way as to be searched object should be in single/double quote
>>> print(b in a) #Error


Example 2:
>>> b=["Hyd","Kadapa","Vij",'Blore']
>>> type(b)
<class 'list'>
>>> print("Hyd" in b) #True
>>> print("Delhi" in b) #False
>>> print("Kur" not in b) #True
>>> 

The membership operators are used to check whether the given object present in the given Collection (List, Tuple,String, Set or Dict.)

There are two membership operators are available:
in
not in

inàreturns True if the given object present in the specified collection
not inàit returns True if the given object not present in the specified collection.


Next topic: Bitwise Operators
Previous Topic: Special Operators

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...