MENU
Question -

Explain cmp(dict1, dict2) with example.



Answer -

Description
The method cmp( ) compares two dictionaries
based on key and values.
Syntax
Following is the syntax for cmp( ) method :
cmp(dict1, dict2)
Parameters
тАв dict1 тАУ This is the first dictionary to be compared with dict2.
тАв dict2 тАУ This is the second dictionary to be compared with dict1.
Return value :
This method returns 10 if both dictionaries are equal, тАУ 1 if dictl < dict2 and 1 if dictl > dict2.
Example :
The following example shows the usage of comp( ) 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 тАЬReturn Value : %dтАЭ, % cmp (dict1, dict2)
print тАЬReturn Value : %dтАЭ, % cmp (dict2, dict3)
print тАЬReturn Value : %dтАЭ, % cmp (dictl, dict4)
Let us compile and run the above program, this will produce the following result :
Return Value : тАУ 1┬а ┬а .
Return Value : 1
Return Value : 0

Comment(S)

Show all Coment

Leave a Comment

Free - Previous Years Question Papers
Any questions? Ask us!
×