Solution to Dictionary Exercise in Python

Dictionary Tutorial & Exercise
#create a dictionary with student name and marks.
#Use student name as key
#User has to enter all data. 

my_list=[]
my_dict={}


n=input("Student Name : ")
a=int(input("\n Enter maths marks: "))
b=int(input("Enter physics marks: "))
c=int(input("Enter Chemistry marks: "))

p=int(input("Enter no of days present: "))

my_list.append(a)
my_list.append(b)
my_list.append(c)
my_list.append(p)

my_dict.update({n:my_list})

print(my_dict)
For three Students & displaying name and attendance
# Create the dictionary with three students
# For each student take marks in three subjeccts and attendance
# For each student display name and attendance 

my_dict={}

for i in range(0,3):
    n=input("Student Name : ")
    a=int(input("\n Enter maths marks: "))
    b=int(input("Enter physics marks: "))
    c=int(input("Enter Chemistry marks: "))

    p=int(input("Enter no of days present: "))


    my_dict.update({n:[a,b,c,p]})

print(my_dict)

for i in my_dict:
    print("Name:",i," Attendance : ",my_dict[i][3])
Read student details from a CSV file and prepare a dictionary
fob=open('student.csv','r')
my_dict={}
for i in fob:
 print(i)
 my_list=i.split(',')               # create a list by breaking string 
 my_list[-1]=my_list[-1].strip()    # remove the last line break \n 
 #print(my_list[0])
 my_dict.update({my_list[0]:[my_list[1],my_list[2],my_list[3],my_list[4],my_list[5]]})
 my_list.clear()                    # Delete all elements of the list
print(my_dict)
Dictionary Tutorial & Exercise



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer