Here , we are going to write python program to create a dictionary of students to store names and marks obtained in five subjects .We can add number of students in dictionary using for loop.
Python program to create dictionary using nested for loop
students = dict()
n = int(input("Enter number of students :"))
for i in range(n):
sname = input("Enter names of student :")
marks= []
for j in range(5):
mark = float(input("Enter marks :"))
marks.append(mark)
students[sname] = marks
print("Dictionary of student created :")
print(students)
Output
Enter number of students :5 Enter names of student :MEENA Enter marks :45 Enter marks :55 Enter marks :67 Enter marks :88 Enter marks :90 Enter names of student :ATHANG Enter marks :55 Enter marks :44 Enter marks :66 Enter marks :77 Enter marks :88 Enter names of student :SUMEDH Enter marks :44 Enter marks :66 Enter marks :77 Enter marks :88 Enter marks :90 Enter names of student :SUSHIL Enter marks :54 Enter marks :67 Enter marks :54 Enter marks :67 Enter marks :88 Enter names of student :ANJALI Enter marks :77 Enter marks :88 Enter marks :44 Enter marks :55 Enter marks :66 Dictionary of student created : {'MEENA': [45.0, 55.0, 67.0, 88.0, 90.0], 'ATHANG': [55.0, 44.0, 66.0, 77.0, 88.0], 'SUMEDH': [44.0, 66.0, 77.0, 88.0, 90.0], 'SUSHIL': [54.0, 67.0, 54.0, 67.0, 88.0], 'ANJALI': [77.0, 88.0, 44.0, 55.0, 66.0]}
Python program to create dictionary of students using if-else statement
Here, we are going to write python program to create dictionary of students using if-else statement. With the help of if-else statement,we can easily search requested items in dictionary that we have created.We can also find out the item;s value by its corresponding key.
python program to create dictionary of students
# Creating dictionary
students_data = {"Meena" : [55,88,77,66,44],
"Sumedh":[56,78,55,88,70],
"Sushil": [44,65,76,33,77]}
print("Original Dictionary : ")
print(students_data)
print()
# searching item in dictionary
name = input("Enter name of student :")
if name in students_data.keys():
print(students_data[name])
else :
print("No student found")
Output:
Original Dictionary : {'Meena': [55, 88, 77, 66, 44], 'Sumedh': [56, 78, 55, 88, 70], 'Sushil': [44, 65, 76, 33, 77]} Enter name of student :Meena [55, 88, 77, 66, 44]
<