Why this code prints only keys not values. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Why this code prints only keys not values.

Age = {"John" : 24, "Mary" : 27, "Sham" : 87} for word in Age: print(word)

4th Jul 2018, 7:18 PM
Raj Charan
3 Respostas
+ 6
Because that's how the dict class is designed by default :) If you want to return the values instead, you can print(Age[word]) or use Age.values() in the for loop. If you want to return the key-value pairs, you use Age.items()
4th Jul 2018, 7:37 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
Because this is for in loop -> it is looping through the indexes, which are exactly the names.
4th Jul 2018, 7:39 PM
TheWhĀ”teCat šŸ‡§šŸ‡¬
TheWhĀ”teCat šŸ‡§šŸ‡¬ - avatar
4th Jul 2018, 7:51 PM
Raj Charan