+ 16

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?

19th Apr 2020, 1:20 AM
PRIYANKA K
PRIYANKA K - avatar
22 Respuestas
+ 53
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
19th Apr 2020, 1:27 AM
Ayush Kumar
Ayush Kumar - avatar
+ 7
fib = {1:1 , 2:1 , 3:2 , 4:3} print(fib.get( 4,0 ) + fib.get( 7,5 ) .get searching for the key. fib.get(4,0) Searching in the (fib) dictionary If key 4 in (fib) dictionary will get the value, if key 4 is not defined 4 = 0 fib.get (4,➡️0) like as key 7 in dictionary fib.get(4,0) searching a key 4 in the dictionary { 1:1 ,2:1 ,3:2 ,➡️4:3 } will 4=3 fib.get(7,5) searching a key 7 in the dictionary { 1:1 ,2:1 ,3:2 ,4:3 }❌ is not defined in dictionary will 7 = 5 fib.get(7,➡️5) fib.get(4,0) + fib.get(7,5) = (3). +. (5) =8
3rd Apr 2021, 8:06 AM
Raafat Dawood
Raafat Dawood - avatar
+ 3
get method returns the value associated with the key. get method takes the key as the argument it can also take two argument one for true and another for false. coming to the code: fib.get(4,0) checks for the key 4 in the fib dictionary, It contains 4 so it return its value which is 3 . if the fib dictionary does not contain 4 it would have return 0. In the same way fib.get (7,5) returns 5 because it does not contain 7 so the total result is 3+5 which is 8
7th Apr 2021, 1:01 PM
Bitu Kumar Sha
Bitu Kumar Sha - avatar
+ 3
1 --> 1 2 --> 1 3 --> 2 4 --> 3 The print statement is holding a sum of get() functions. The get() function has 2 Inputs: First one is what to find, and the second one is what to print instead of "None" when key not found. First block of the sum is get(4, 0). It found it, and returned the value of that key, 3. Second block of the sum is get(7, 5). Key 7 was not found, so it will return 5. Therefore is 3 + 5 printed, which is 8.
25th Aug 2021, 7:11 AM
Bobbersnucker
Bobbersnucker - avatar
+ 2
Here (fib.get(4,0)), returns 3, Since 4 is in dictionary and (fib.get(7,5)), returns 5, since 7 in not in dictionary ,So it takes value 5 as given in code, so (3+5) gets 8
24th Apr 2021, 6:02 AM
Vikash Kushwaha
Vikash Kushwaha - avatar
+ 2
fib.get(4,0)=3 fib.get(7,5)=5 3+5=8
3rd May 2022, 6:57 PM
Igor V. Melnyk
Igor V. Melnyk - avatar
+ 1
A chave esta na linha: print(fib.get(4,0) + fib.get(7, 5)) O recurso .get tem dois parâmetros: o primeiro a chave que se deseja o valor, e o segundo um valor alternativo, caso a chave não seja encontrada. fib.get(4,0) retornará 3 que é o valor da chave 4. fib.get(7,5) retornará 5 que é o valor alternativo, pois não há uma chave 7 O resultado será 8.
3rd Oct 2021, 1:00 AM
Lúcio Flávio
Lúcio Flávio - avatar
+ 1
Por si hablas español: El comando get solo tomará 1 valor y cumplirá lo siguiente... - Si el primer número está en el diccionario (es correcto), este se ejecutará como parte del diccionario y te dará como respuesta su número asignado (4: 3 entonces te da 3) y ya no se ejecuta el segundo número. - Si el primer número no está en el diccionario (no es correcto), entonces se ejecuta el segundo pero como un número normal. - Si ambos están en el diccionario (son correctos), solo se ejecuta el primero como diccionario y el segundo ha no. La respuesta a la interrogante es 8 ya que es 3 + 5 = 8👍
9th Mar 2022, 10:44 PM
Alvaro Espinoza G.
Alvaro Espinoza G. - avatar
0
8. read the get method for the dictionary . I am sure you will understand. pRIYANKA_K = "all the best" print(pRIYANKA_K.title() ) 👍
19th Apr 2020, 4:20 PM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar
0
8
13th Apr 2021, 3:00 AM
Alireza Asadiyan
Alireza Asadiyan - avatar
0
fib.get(4,0) returns 3 because 3 exists in the dictionary the elements after comma is executed only if the first element doesn't exist in the dictionary that is why (7,5) returns 5. 7 doesn't exist in the dictionary so that 5 should be printed instead. hence the total 3 + 5 is 8
7th May 2021, 9:49 AM
Lemi Elias
Lemi Elias - avatar
0
It will be 8
16th Jun 2021, 1:51 PM
Abdelrahman shaheen
0
Can I please ask what the application of this is in the real world? Why would anyone do this?
3rd Jul 2021, 2:40 AM
Chuks AJ
Chuks AJ - avatar
0
I think its true
4th Jul 2021, 10:43 AM
Hamza Rahali
Hamza Rahali - avatar
0
Answer :- 8
31st Jul 2021, 2:34 PM
Helal Uddin
Helal Uddin - avatar
0
Mateusz Wabiński Very good explaining. I understood how it works, rather than get just the answer.
12th Sep 2021, 6:43 PM
Bobbersnucker
Bobbersnucker - avatar
0
7,5 7 is not there then howcome it takes the 5 ?
17th Dec 2021, 3:43 PM
Niyas k
Niyas k - avatar
0
It'll check for the keys which are present in the given dictionary and add them. For 4 it's 3 and rest of the keys are not found so it will add the 2nd argument of 2nd parameter i.e. 5. Therefore, 3+5=8. Here no error message is provided which confuses us.
27th Feb 2022, 7:41 AM
Jaya Vishwakarma
Jaya Vishwakarma - avatar
0
#going to the second line of print(fib.get(4,0) + fib.get(7.5)) The get() method takes a key from the dictionary to output it’s corresponding key (eg. fib.get(4,0) , looking at the dictionary 4 as a key has a value of 3. So it’s lifting 3 and has no action with 0 Second instance where your key in fib.get(7,5) is not in the dictionary, it automatically lifts your default value which is 5 Hence 3+5 =8
19th Mar 2022, 1:50 PM
Nnodi Precious