Python 4.2 - Other operators


Other operators

Python provides two other operators called Membership and Identity operators which are used for some different purposes. Let’s understand these operators one by one.

1.   Membership operators

Membership operators are used to check the membership of a value or object in a sequence such as list, string, tuple, dictionary. We can check, if a particular value is available in a sequence or not. For example, we can check whether number 4 is available in the list [1,2,3,4,5]. This checking is called membership i.e. we can check whether number 4 is a member of the sequence. There are two types of membership operators –

·         in

·         not in

in operator

‘in’ operator returns True if the value or element is a member of a sequence i.e. it returns True if an element is found in a sequence such as list, string etc. ‘in’ operator is mostly used with for loop. Syntax to use ‘in’ operator is –

value in sequence

The above syntax represents that we want to check whether the value is available in the specified sequence. Above statement will return True, if value is available in specified sequence otherwise it will return False.  

For example, let’s say we have a list – 

lst = [1,2,3,4,5,6,7,8]

And we want to check, whether 5 is available in the list. Then we can write something like this –

print(5 in lst) # prints True since 5 is available in lst.

We can also check in other sequences. See below code –

# uses of ‘in’ operator

lst = [1,2,3,4,5,6,7,8,9,10] # created a list named lst

str = "india" # a string named ‘str’

x = 15 # stored value 15 into variable ‘x’

print(5 in lst) # checking whether 5 is available in lst

print(x in lst) # checking whether value of ‘x’ i.e. 15 is available in lst

print('i' in str) # checking whether character 'i' is available in str

print('dia' in "india") # checking whether 'dia' is available in 'india'

print('------------------------------------')

# using in operator with for loop to print all values together

for i in lst: # each value will be stored into ‘I’ from ‘lst’ at a time

    print(i) # printing each value of i

Output –

True

False

True

True

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

1

2

3

4

5

6

7

8

9

10

not in operator

‘not in’ operator works exact opposite to ‘in’ operator. ‘not in’ operator returns True if the value is not available in the sequence and returns False if value is available in the sequence. Let’s take an example –

lst = [1,2,3,4,5]

print(2 not in lst) # prints False since 2 is available in lst

print(8 not in lst) # prints True

2.   Identity operators

These operators are used to compare the memory locations of two objects such as variables, lists, strings etc. We can check whether two objects pointing to same memory location. Please remember that these operators do not compare values of two objects rather they compare addresses of two objects. There are two identity operators –

·         is

·         is not

is operator

This operator returns True if memory locations of two objects are same otherwise returns False. Let’s take an example – 

# comparing identity or memory location of two objects

x = 10 # x will point to some location where 10 is stored

y = 10 # y will point to same location as of x

# let's check their memory locations using id() function

# as we know, how Python handles variables. so their locations

# will be same

print("location of x -",id(x)) # memory location of x

print("location of y -",id(y)) # memory location of y

# now let's compare their memory locations using is operator

print(x is y) # prints True since their memory location is same

y = 20 # it will change the location of y

print("location of y -",id(y)) # let's check it

# now compare their memory locations again

print(x is y) # will print False since y is pointing to some other location 


Output – 


location of x - 140707403077568

location of y - 140707403077568

True

location of y - 140707403077888

False

Keep in mind that it did not compare vales of x and y rather it compared memory locations of x and y. if you want to check, let’s take two lists which will have same elements but different memory locations. See code below –

# comparing identity or memory locations of two objects

lst1 = [1,2,3,4,5] # a list with some elements

lst2 = [1,2,3,4,5] # second list with same elements

# now let's check their memory locations

print("location of lst1 -",id(lst1))

print("location of lst2 -",id(lst2))

# let's compare their memory locations

print(lst1 is lst2) # False, although values are same but memory locations are different

Output –

location of lst1 - 2081509500800

location of lst2 - 2081504247744

False

is not operator

‘is not’ operator works exact opposite to ‘is’ operator. It will return True if memory locations of two objects are not same otherwise it will return False. Let’s take an example –

# comparing identity or memory location of two objects

x = 10 # x will point to some location where 10 is stored

y = 10 # y will point to same location as of x

# let's check their memory locations using id() function

# as we know, how Python handles variables. so their locations

# will be same

print("location of x -",id(x)) # memory location of x

print("location of y -",id(y)) # memory location of y

# now let's compare their memory locations using is not opera-tor

print(x is not y) # prints False since their memory locations are same

y = 20 # it will change the location of y

print("location of y -",id(y)) # let's check it

# now compare their memory locations again

print(x is not y) # will print True since x and y have different locations


Output –


location of x - 2093653584464

location of y - 2093653584464

False

location of y - 2093653584784

True

Comments

Popular posts from this blog

Python 4.4 - Points to remember

Python 3.6 - Points to remember

Python 3.3 - Datatypes in Python