0

primes={1:2,2:3,4:5,6:11} print (primes [primes [4]]

What is the output? And how? Explain plZ

9th Feb 2019, 7:41 PM
Mhr Habib
Mhr Habib - avatar
9 Answers
+ 5
Mhr Habib Great, you already have half or your answer by yourself. And Jomari explained why
9th Feb 2019, 7:51 PM
Dapper Mink
Dapper Mink - avatar
+ 2
Why don't you try it yourself?!
9th Feb 2019, 7:45 PM
Dapper Mink
Dapper Mink - avatar
0
Its giving me a key error in the playground but in the lecture its gives output of 11
9th Feb 2019, 7:48 PM
Mhr Habib
Mhr Habib - avatar
0
This would result in an error. primes[4] resolves to 5, while primes[5] is missing. Basically, what happens here is that the `thing` inside the bracket is the KEY for the dictionary to get its value. primes[4] is 5 because you declared 4:5. primes[5] on the other hand is not present.
9th Feb 2019, 7:49 PM
Jomari Pantorilla
Jomari Pantorilla - avatar
0
Jomari pantorilla have you seen the dictionary functon first question
9th Feb 2019, 7:55 PM
Mhr Habib
Mhr Habib - avatar
0
@Mhr Habib What do you mean by if I "saw the dictionary function"? Have you checked again if maybe you've made a typo? If 6:11 becomes 5:11 instead, then the expected output of 11 would be correct.
9th Feb 2019, 7:59 PM
Jomari Pantorilla
Jomari Pantorilla - avatar
0
If it is indexing... Then my thinking is correct ...output is 11 ..
9th Feb 2019, 8:02 PM
Mhr Habib
Mhr Habib - avatar
0
Oh. Maybe you're mixing up arrays and dicts?
9th Feb 2019, 8:07 PM
Jomari Pantorilla
Jomari Pantorilla - avatar
0
Mhr Habib Dictionaries are unordered and don't support indexing. If you could use indices, you couldn't tell if primes[1] referred to the value with the key 1 (2) or the second value (3). Since the prime number 7 is missing, I also think that the dictionary in your question is incomplete.
9th Feb 2019, 10:03 PM
Anna
Anna - avatar