Write a program to print only keys of a dictionary


The program defines a dictionary named "student" which contains information about a student. The information includes the student's name, roll number, marks, age, gender, and city.

  • The first line print(student.keys()) retrieves a list of all the keys in the dictionary and prints it.
  • The second line print("\n==== Keys ====") is just a label to separate the list of keys from the rest of the output.
  • The third line for k in student.keys(): starts a for loop that iterates over each key in the "student" dictionary. The loop variable "k" takes the value of each key in turn during each iteration.
  • The fourth line print(" ",k) prints the value of "k" which is one of the keys in the "student" dictionary.

In summary, the program retrieves all the keys from the "student" dictionary and prints each key on a separate line.

Source Code

student = {
	"Name": "Tara",
	"RollNo":130046, 
	"Mark": 458, 
	"Age":16,
	"Gender":"Female",
	"City": "Chennai"
}
print(student.keys())
print("\n==== Keys ====")
for k in student.keys():
	print(" ",k)

Output


dict_keys(['Name', 'RollNo', 'Mark', 'Age', 'Gender', 'City'])

==== Keys ====
  Name
  RollNo
  Mark
  Age
  Gender
  City