Computer Science, asked by swapnajitrock4097, 1 year ago

Explain cmp(dict1, dict2) with example.

Answers

Answered by mohishkhan9996
1

Answer:

cmp(dict1, dict2) Compares elements of both dict. len(dict) Gives the total length of the dictionary. This would be equal to the number of items in the dictionary. type(variable) Returns the type of the passed variable.

plzz mark as brainlist

Answered by letmeanswer12
0

Python dictionary cmp() Method

Explanation:

Python dictionary method cmp() compares two dictionaries depend on key and values.

  • Syntax - cmp(dict1, dict2)
  • Parameters - (dict1) - this is often the primary dictionary to be compared with dict2.  (dict2) − this is often the second dictionary to be compared with dict1.
  • Return Value - This method returns 0 if both dictionaries are equal, -1 if dict1 < dict2 and 1 if dict1 > dic2.

The following example shows the usage of cmp( ) method.

  • # !/user/bin/python
  • dict1 = {‘Name’ : ‘Zara’, ‘Age’ : 7};
  • dict2 = {‘Name1 : ‘Mahnaz’, ‘Age’ : 27};  
  • dict3 = {‘Name’ : ‘Abid’, ‘Age’ : 27);  
  • dict4 = {‘Name’ : ‘Zara’, ‘Age’ : 7};
  • print “1. Result Value : %d”, % cmp (dict1, dict2)  
  • print “2. Result Value : %d”, % cmp (dict2, dict3)  
  • print “3. Result Value : %d”, % cmp (dict1, dict4)

Let us compile and run the above program, this may produce the subsequent result :  

  • 1. Result Value : – 1 .  
  • 2. Result Value : 1  
  • 3. Result Value : 0

Similar questions