Why this code prints only keys not values. | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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 ответов
+ 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
+ 1
4th Jul 2018, 7:51 PM
Raj Charan