Important Notice:

Course Content
Dictionary in Python (Chapter-9) M3-R5
About Lesson

get():-

            get() method का उपयोग दिए गए key की value को प्राप्त करने के लिए किया जाता है।

Syntax:-     dict.get(key, defaultVal)

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1.get(“two”))                                                                         # suresh

 

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1.get(“four”,’mahesh’))                                                       # mahesh

Note:-     यहाँ पर ‘four’ नाम की key; dictionary में मौजूद नहीं है और default में ‘mahesh’ value set की गयी है अगर दी हुई key; get() method को नहीं मिलती है तो parameter पर set की हुई value return होती है

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1.get(“four”))                                                        # None

Note:- अगर दी हुई key; get() method को नहीं मिलती है और default value set नहीं की जाती है तो ‘None’ return होता है

 

get():-

            get() method is used to get the value of the given key.

Syntax:-     dict.get(key, defaultVal)

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1.get(“two”))                                                                         # suresh

 

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1.get(“four”,’mahesh’))                                                       # mahesh

Note:-     Here the key named ‘four’; is not present in the dictionary and default value is set to ‘mahesh’ if the given key; get() method, then the value set on the parameter is returned.

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1.get(“four”))                                                        # None

Note:- If the given key; get() method returns ‘None’ if it is not found and a default value is not set.

error: Content is protected !!