Operators in Dictionary


☞ We will work on the following operators in a dictionary.

1. Concatenation Operator
2. Replication Operator
3. Membership Operator

Concatenation(+) Operator :

  • It does not support concatenation operator.
Example :
 
D = {10:"Ten", 20:"Twenty"}+3
print(D)
Output :
 
D = {10:"Ten", 20:"Twenty"}+3
TypeError: unsupported operand type(s) for +: 'dict' and 'int'

Replication(*) Operator :

  • It does not support replication operator.
Example :
 
D = {10:"Ten", 20:"Twenty"}*3
print(D)
Output :
 
D = {10:"Ten", 20:"Twenty"}*3
TypeError: unsupported operand type(s) for *: 'dict' and 'int'

Membership (in,not in) Operator :

  • in : It returns True if an element exists in the given tuple; Otherwise returns False
  • not in : It returns True if an element does not exist in the given tuple; Otherwise returns False
Example :
 
D1 = 20 in {10:"Ten", 20:"Twenty"}
print (D1)

D2 = 50 not in {10:"Ten", 20:"Twenty"}
print (D2)
Output :
 
True
True