See my qes. Plzz | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

See my qes. Plzz

x={5:1,4:2,33,2:4,1:5} print(x[4]) Ans: 2 Why?

5th Apr 2021, 9:42 AM
Rajinder Sharma
Rajinder Sharma - avatar
3 Answers
+ 7
because thats a dictionary. it doesnt use index, it uses key:value pairs. x[4] means "look for the value at dictionary key 4" which, when you look, 4 is 2 in that dictionary and is also why numbers are not usually used as keys
5th Apr 2021, 9:55 AM
Slick
Slick - avatar
+ 4
This is a dictionary and so it is of key-value pairs.When you refer with the key name the equivalent value is printed. In your case when you give x[4], the the number 4 is taken as the key and its equivalent value-2 is returned.
5th Apr 2021, 9:56 AM
A.Febi Triphena
+ 2
Thnkuu guys
5th Apr 2021, 10:00 AM
Rajinder Sharma
Rajinder Sharma - avatar