dictionaries | SoloLearn: Learn to code for FREE!

+2

dictionaries

What is the result of this code? fib = {1: 1, 2: 1, 3: 2, 4: 3} print(fib.get(4, 0) + fib.get(7, 5)) How does this works?

4/19/2020 1:20:50 AM

PRIYANKA K

3 Answers

New Answer

+6

It is quite simple sis . In python dictionaries, following is a conventional method to access a value for a key. The get() method is used to avoid such situations. Here in fib.get(4,0) four was present in dictionary so it got the value from dictionary but in fib.get(7,5) seven was not not found in dictionary therefore it will take the second value from your fib.get(7,5) I.e 5 From first statement u get 3 and from second statement you get 5 because 7 is not found in dictionary. So result will be 3+5=8

+5

https://www.google.com/amp/s/www.geeksforgeeks.org/get-method-dictionaries-python/amp/

0

8. read the get method for the dictionary . I am sure you will understand. pRIYANKA_K = "all the best" print(pRIYANKA_K.title() ) 👍