Python Programming Tutorial – Dictionary – Part 3 | GeeksforGeeks

Python Programming Tutorial – Dictionary – Part 3 | GeeksforGeeks

Hi friends and welcome to GeeksforGeeks in
this tutorial we will be continuing our discussion on Dictionaries in Python. So, let’s move
to GeeksforGeeks IDE to learn about them Let’s first discuss the update function. This
function is used to update the dictionary to add other dictionary keys. Let’s see this
example. We have 2 dictionaries. 1 containing the name
and age and dictionary 2 containing the roll number. We are updating dictionary 1 with
dictionary 2. Let’s run this code. We can see that dictionary 1 now contains roll numbers
too. Next, let’s discuss the get function. This
function return the value associated with the key mentioned in its argument. If key
is not present, the default value is returned. Here’s the format for using this function.
Let’s look at this example, we have a dictionary and we are checking the value associated with
roll_number and name using get function with default value, “not present”. We can see that
in the dictionary we just have the name and not the roll number.
So, let’s run this code. We can see that the value associated with name is printed and
for ID not present is printed. Next, let’s discuss the set default function.
This function also searches for a key and displays its value like get() but, it creates
new key with def_value if key is not present. Let’s look at the same example by simply changing
get to set_default. Let’s run this code, we can see that the first
2 outputs are same. But this time there is a new key in the dictionary with default value
not present. So, in this tutorial, we discussed some new
methods of dictionary in python. We will continue learning python in the coming tutorials.
Thank you for watching! Please leave us, your likes and comments in the comments section.

Daniel Ostrander

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *