leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Python knowledge point series (1)

Estimated reading: 18 minutes 72 views

In the learning process, it is easy to forget while learning. This tutorial is aimed at this situation. Taking the “transcript” as an example, it is possible to recall and connect the knowledge points learned before.

1. Python variables, numbers and strings

Through the previous study, we already know how to use variables, numbers and strings in Python. Next, we combine the knowledge introduced above to create a report card to record the grades of the students in the class.

First, we need to use the input() function to prompt the user for the student ID and name. Then, the user is required to enter Chinese grades, math grades, and English grades.

Next, we will use the print() function to print the student’s student number, name, Chinese grade, math grade, English grade, and the total score of the three homework. Because the input scores of each subject are of string type, in order to calculate the total score, we also need to use the int() function to convert the string type to a number type for sum calculation.

The code looks like this:

name = input("Please enter the student's name: ")
userID = input("Please enter the student ID: ")
score1 = input("Please enter the student's language score: ")
score2 = input("Please enter the student's math score: ")
score3 = input("Please enter the student's English score: ")
total = float(score1) + float(score2) + float(score3)
print ("Student number name Chinese mathematics English total score")
print (userID," " ,name, " ",score1, " ",score2, " ",score3," ",total)

We save this code to the D:\Python Programs\ch01 directory and name the program file 1.1.py. After running the program, its output is shown in Figure 1.

Python variables, numbers and strings
figure 1

2. Python list

We have successfully entered grades for a classmate and calculated his total score. However, we found a problem that the current report card can only record the grades of one person; obviously, this does not meet our requirements. Next, we’ll use the study list to record grades for multiple classmates.

get list elements in python

First of all, we want to record the grades of the three students, which requires the function of the list. For example, use the nameList list to record the name, use the IDList list to record the student number, use the scoreList1 list to record all the Chinese grades, use the scoreList2 list to record all the math grades, use the list scoreList3 to record all the English grades.

In addition, we also created a totalList list, used to record the total score of everyone. code show as below.

nameList=["Li Ruoyu","Zhang Zidong","Wang Xiaoming"]
IDList=["1","2","3"]
scoreList1=[94,100,99]
scoreList2=[93,100,95]
scoreList3=[100,94,100]
totalList=[scoreList1[0] +scoreList2[0] + scoreList3[0],
           scoreList1[1] + scoreList2[1] + scoreList3[1],
           scoreList1[2] + scoreList2[2] + scoreList3[2]]
print( "Now there are "+str(len(nameList)) +" students' scores, and their scores are as follows: ")
print ("Student number name Chinese mathematics English total score")
print (IDList[0],"    " ,nameList[0], "    ",scoreList1[0], "   ",scoreList2[0], "    ",scoreList3[0],"   ",totalList[0])
print (IDList[1],"    " ,nameList[1], "    ",scoreList1[1], "   ",scoreList2[1], "    ",scoreList3[1],"   ",totalList[1])
print (IDList[2],"    " ,nameList[2], "    ",scoreList1[2], "   ",scoreList2[2], "    ",scoreList3[2],"   ",totalList[2])

We created a nameList list to record the names of 3 classmates, namely “Li Ruoyu”, “Zhang Zidong” and “Wang Xiaoming”. Then we created an IDList list to record the student numbers of the above three students, namely “1”, “2” and “3”.

Next, use the scoreList1 list to record the Chinese scores of the three students, which are 94, 100, and 99; use the scoreList2 list to record the math scores of the three students, which are 93, 100, and 95; use the scoreList3 list to record the English scores of the three students , which are 100, 94, and 100, respectively.

Then add the elements of scoreList1, scoreList2 and scoreList3 as the elements of the totalList list, which respectively represent the total score of the three students. Next, we tell the user how many students’ grades are currently recorded according to the length of the nameList.

Finally, list their specific scores. The running result is shown in Figure 2.

Python list
figure 2

Modification of Python list elements

At this time, we found that the Chinese score of the second student was wrong, and the score should be changed from 100 to 98. The relevant code is as follows.

nameList=["Li Ruoyu","Zhang Zidong","Wang Xiaoming"]
IDList=["1","2","3"]
scoreList1=[94,100,99]
scoreList2=[93,100,95]
scoreList3=[100,94,100]
totalList=[scoreList1[0] +scoreList2[0] + scoreList3[0],
             scoreList1[1] + scoreList2[1] + scoreList3[1],
             scoreList1[2] + scoreList2[2] + scoreList3[2]]
print( "The second student's Chinese score is incorrectly entered, it should be changed to 98")
scoreList2[1]=98
totalList[1]=scoreList1[1] + scoreList2[1] + scoreList3[1]
print( "The revised grade of the second student is as follows: ")
print ("Student number name Chinese mathematics English total score")
print (IDList[1]," " ,nameList[1], " ",scoreList1[1], " ",scoreList2[1], " ",scoreList3[1]," ",totalList[1])

We modified the second element in the scoreList2 list, specifying a new element value of 98. Then print the student’s score, you can see that his Chinese score has changed from 100 to 98, and the total score has also changed from 294 to 292. The running result is shown in Figure 3.

Python list modification
image 3

python list element addition

Next, we have to enter the information of a classmate. We use the append() method to add new elements to each list. The complete code is as follows.

nameList=["Li Ruoyu","Zhang Zidong","Wang Xiaoming"]
IDList=["1","2","3"]
scoreList1=[94,100,99]
scoreList2=[93,100,95]
scoreList3=[100,94,100]
totalList=[scoreList1[0] +scoreList2[0] + scoreList3[0],
             scoreList1[1] + scoreList2[1] + scoreList3[1],
             scoreList1[2] + scoreList2[2] + scoreList3[2]]
scoreList1[1]=98
totalList[1]=scoreList1[1] + scoreList2[1] + scoreList3[1]
print("Next, we will enter the grades of a new student")
nameList.append ( input("Please enter the student's name: ")
IDList.append(input("Please enter the student ID: "))
scoreList1.append( input("Please enter the student's language score: "))
scoreList2.append( input("Please enter the student's math score: "))
scoreList3.append(input("Please enter the student's English score: "))
totalList.append(float(scoreList1[3]) + float(scoreList2[3]) + float(scoreList3[3]))
print( "Now there are "+str(len(nameList)) +" students' scores, their scores are as follows: ")
print ("Student number name Chinese mathematics English total score")
print (IDList[0]," " ,nameList[0], " ",scoreList1[0], " ",scoreList2[0], " ",scoreList3[0]," ",totalList[0] )
print (IDList[1]," " ,nameList[1], " ",scoreList1[1], " ",scoreList2[1], " ",scoreList3[1]," ",totalList[1])
print (IDList[2]," " ,nameList[2], " ",scoreList1[2], " ",scoreList2[2], " ",scoreList3[2]," ",totalList[2])
print (IDList[3]," " ,nameList[3], " ",scoreList1[3], " ",scoreList2[3], " ",scoreList3[3]," ",totalList[3])

We use the append() method to add the name entered by the user to the nameList list, add the entered student number to the IDList list, add the Chinese score to the scoreList1 list, add the math score to the scoreList2 list, and add the English score to the scoreList3 list .

Then convert the input string to a number through the float() function, and add the added result to the totalList list. According to the length of the nameList list, we know that there are already 4 students’ scores, and their specific scores are listed. The running result is shown in Figure 4.

python append() method
Figure 4

Python list element deletion

At this time, we found out that Wang Xiaoming has already transferred, so we need to remove him from our transcript. We use the index() method to find the index value of Wang Xiaoming in the list, and then remove the corresponding index element in each list. The complete code is as follows.

nameList=["Li Ruoyu","Zhang Zidong","Wang Xiaoming"]
IDList=["1","2","3"]
scoreList1=[94,100,99]
scoreList2=[93,100,95]
scoreList3=[100,94,100]
totalList=[scoreList1[0] +scoreList2[0] + scoreList3[0],
             scoreList1[1] + scoreList2[1] + scoreList3[1],
             scoreList1[2] + scoreList2[2] + scoreList3[2]]
scoreList1[1]=98
totalList[1]=scoreList1[1] + scoreList2[1] + scoreList3[1]
print("Next, we will enter the grades of a new student")
nameList.append ( input("Please enter student name: ") )
IDList.append(input("Please enter the student ID: "))
scoreList1.append( input("Please enter the student's language score: "))
scoreList2.append( input("Please enter the student's math score: "))
scoreList3.append(input("Please enter the student's English score: "))
totalList.append(float(scoreList1[3]) + float(scoreList2[3]) + float(scoreList3[3]))
print( "Now we want to delete Wang Xiaoming's record from the list")
index=nameList.index("Wang Xiaoming")
del nameList[index],IDList[index],scoreList1[index],scoreList2[index],scoreList3[index], totalList[index]
print( "Now there are "+str(len(nameList)) +" students' scores, their scores are as follows: ")
print ("Student number name Chinese mathematics English total score")
print (IDList[0]," " ,nameList[0], " ",scoreList1[0], " ",scoreList2[0], " ",scoreList3[0]," ",totalList[0])
print (IDList[1]," " ,nameList[1], " ",scoreList1[1], " ",scoreList2[1], " ",scoreList3[1]," ",totalList[1])
print (IDList[2]," " ,nameList[2], " ",scoreList1[2], " ",scoreList2[2], " ",scoreList3[2]," ",totalList[2])

We use the index() method to find the position of “Wang Xiaoming” in the nameList list, and assign it to the variable index. Then use the del() method to delete the index-th element in all lists. Then we print the transcript again, and now we can see that there are only 3 students’ grades, and the information of “Wang Xiaoming” is no longer in the transcript. The running result is shown in Figure 5.

Python list element deletion
Figure 5

Sorting list elements in Python

Finally, we need to arrange the total scores from low to high. We can use the sort() method to sort the list to complete this task. The complete code is as follows.

nameList=["Li Ruoyu","Zhang Zidong","Wang Xiaoming"]
IDList=["1","2","3"]
scoreList1=[94,100,99]
scoreList2=[93,100,95]
scoreList3=[100,94,100]
totalList=[scoreList1[0] +scoreList2[0] + scoreList3[0],
             scoreList1[1] + scoreList2[1] + scoreList3[1],
             scoreList1[2] + scoreList2[2] + scoreList3[2]]
scoreList1[1]=98
totalList[1]=scoreList1[1] + scoreList2[1] + scoreList3[1]
print("Next, we will enter the grades of a new student")
nameList.append ( input("Please enter student name: ") )
IDList.append(input("Please enter the student ID: "))
scoreList1.append( input("Please enter the student's language score: "))
scoreList2.append( input("Please enter the student's math score: "))
scoreList3.append(input("Please enter the student's English score: "))
totalList.append(float(scoreList1[3]) + float(scoreList2[3]) + float(scoreList3[3]))
print( "Now we want to delete Wang Xiaoming's record from the list")
index=nameList.index("Wang Xiaoming")
del nameList[index],IDList[index],scoreList1[index],scoreList2[index],scoreList3[index], totalList[index]
print("Please sort the total score from low to high")
totalList.sort()
print(totalList)

Use the sort() method to sort the totalList list and print the result. The running result is shown in Figure 6.

Python sort() element sorting
Image 6
We see that the lowest score among the 3 students is 282 and the highest is 292. However, since the students’ information is stored in different lists, if the order of one of the lists is changed, all the information will be confused.

About this question? Below we will detail

3. Python tuples and dictionaries

We designed a transcript using lists, which can record the grades and total marks of multiple students in each subject. However, this information is stored in different lists, and when one list changes, especially when the order of elements in the list changes, it will affect other lists. So is there any way to keep all this information in a list?

We can use a dictionary to store the information of each classmate, and then add each dictionary as an element to the list, so that by nesting the list of dictionaries, we include all the information into a list. The complete code is as follows.

studentList=[]
student={}
student={"name":"Li Ruoyu","ID":"1","score1":95,"score2":100,"score3":96,"total":291}
studentList.append(student)
student={"name":"Zhang Zidong","ID":"2","score1":93,"score2":100,"score3":95,"total":288}
studentList.append(student)
student={"name":"Wang Xiaoming","ID":"3","score1":100,"score2":94,"score3":100,"total":294}
studentList.append(student)
print( "Now there are scores of "+str(len(studentList)) +" students, their scores are as follows: ")
print ("Student number name Chinese mathematics English total score")
print (
studentList[0].get("ID")," " ,studentList[0].get("name"), " ",studentList[0].get("score1"), " ",studentList[0]. get("score2"), " ",studentList[0].get("score3"), " ",studentList[0].get("total"))
print (
studentList[1].get("ID")," " ,studentList[1].get("name"), " ",studentList[1].get("score1"), " ",studentList[1]. get("score2"), " ",studentList[1].get("score3")," ",studentList[1].get("total"))
print (studentList[2].get("ID")," " ,studentList[2].get("name"), " ",
studentList[2].get("score1"), " ",studentList[2].get("score2"), " ",studentList[2].get("score3")," ",studentList[2]. get("total"))

We first created an empty list studentList and an empty dictionary student. Then, assign a dictionary to the variable student. The keys in the dictionary are “name”, “ID”, “score1”, “score2”, “score3” and “total”, and the corresponding values ​​are “Li Ruoyu”, “1”, and “95”. “100”, “96” and “291”.

Then use the append() method to add student as an element to the list studentList. Next, add the information of “Zhang Zidong” and “Wang Xiaoming” in turn. When you print out the studentList, you can see that the list contains the information of the above three students, as shown in Figure 7.

Python tuples and dictionaries
Figure 7


Assuming that the Chinese score of the second student needs to be changed from 100 to 98, the complete code is as follows.

studentList=[]
student={}
student={"name":"Li Ruoyu","ID":"1","score1":95,"score2":100,"score3":96,"total":291}
studentList.append(student)
student={"name":"Zhang Zidong","ID":"2","score1":93,"score2":100,"score3":95,"total":288}
studentList.append(student)
student={"name":"Wang Xiaoming","ID":"3","score1":100,"score2":94,"score3":100,"total":294}
studentList.append(student)
studentList[1]["score2"]=98
studentList[1]["total"]=studentList[1]["score1"]+studentList[1]["score2"]+studentList[1]["score3"]
print( "The second student's Chinese score is incorrectly entered, it should be changed to 98")
print( "The revised grade of the second student is as follows: ")
print ("Student number name Chinese mathematics English total score")
print (
    studentList[1].get("ID")," " ,studentList[1].get("name"), " ",studentList[1].get("score1"), " ",studentList[1]. get("score2"), " ",studentList[1].get("score3")," ",studentList[1].get("total"))

Modify the value corresponding to the key “score1” in the second element of the studentList list to “98”. Then print the student’s score, you can see that his Chinese score has been changed from 100 to 98, and the total score has also changed from 288 to 286, as shown in Figure 8.

Python tuples and dictionaries
Figure 8

Next, another student’s grades must be entered. Use the append() method to add the dictionary student, the complete code is as follows.

studentList=[]
student={}
student={"name":"Li Ruoyu","ID":"1","score1":95,"score2":100,"score3":96,"total":291}
studentList.append(student)
student={"name":"Zhang Zidong","ID":"2","score1":93,"score2":100,"score3":95,"total":288}
studentList.append(student)
student={"name":"Wang Xiaoming","ID":"3","score1":100,"score2":94,"score3":100,"total":294}
studentList.append(student)
studentList[1]["score1"]=98
studentList[1]["total"]=studentList[1]["score1"]+studentList[1]["score2"]+studentList[1]["score3"]
print("Next, we will enter the grades of a new student")
student={}
student["name"]=input("Please enter the student's name: ")
student["ID"]=input("Please enter the student ID: ")
student["score1"]=input("Please enter the student's Chinese score:")
student["score2"]=input("Please enter the student's math score:")
student["score3"]=input("Please enter the student's English score:")
student["total"]=float(student["score1"]) + float(student["score2"]) + float(student["score3"])
studentList.append(student)
print( "Now there are scores of "+str(len(studentList)) +" students, their scores are as follows: ")
print ("Student number name Chinese mathematics English total score")
print (
    studentList[0].get("ID")," " ,studentList[0].get("name"), " ",studentList[0].get("score1"), " ",studentList[0]. get("score2"), " ",studentList[0].get("score3")," ",studentList[0].get("total"))
print (
    studentList[1].get("ID")," " ,studentList[1].get("name"), " ",studentList[1].get("score1"), " ",studentList[1]. get("score2"), " ",studentList[1].get("score3")," ",studentList[1].get("total"))
print (
    studentList[2].get("ID")," " ,studentList[2].get("name"), " ",studentList[2].get("score1"), " ",studentList[2]. get("score2"), " ",studentList[2].get("score3")," ",studentList[2].get("total"))
print (
    studentList[3].get("ID")," " ,studentList[3].get("name"), " ",studentList[3].get("score1"), " ",studentList[3]. get("score2"), " ",studentList[3].get("score3")," ",studentList[3].get("total"))

The result of running the above code is shown in Figure 9.

Python add dictionary
Figure 9
Assuming that the total scores need to be sorted from low to high, you can use the sort() method to sort the list, and you also need to specify the parameter key. The complete code is as follows.

studentList=[]
student={}
student={"name":"Li Ruoyu","ID":"1","score1":95,"score2":100,"score3":96,"total":291}
studentList.append(student)
student={"name":"Zhang Zidong","ID":"2","score1":93,"score2":100,"score3":95,"total":288}
studentList.append(student)
student={"name":"Wang Xiaoming","ID":"3","score1":100,"score2":94,"score3":100,"total":294}
studentList.append(student)
studentList[1]["score1"]=98
studentList[1]["total"]=studentList[1]["score1"]+studentList[1]["score2"]+studentList[1]["score3"]
print( "Next, enter the grades of a new student")
student={}
student["name"]=input("Please enter the student's name: ")
student["ID"]=input("Please enter the student ID: ")
student["score1"]=input("Please enter the student's Chinese score:")
student["score2"]=input("Please enter the student's math score:")
student["score3"]=input("Please enter the student's English score:")
student["total"]=float(student["score1"]) + float(student["score2"]) + float(student["score3"])
studentList.append(student)
print( "Now there are scores of "+str(len(studentList)) +" students, their scores are as follows: ")
print ("Student number name Chinese mathematics English total score")
print (
    studentList[0].get("ID"),"    " ,studentList[0].get("name"), "    ",studentList[0].get("score1"), "   ",studentList[0].get("score2"), "    ",studentList[0].get("score3"),"   ",studentList[0].get("total"))
print (
    studentList[1].get("ID"),"    " ,studentList[1].get("name"), "    ",studentList[1].get("score1"), "   ",studentList[1].get("score2"), "    ",studentList[1].get("score3"),"   ",studentList[1].get("total"))
print (
    studentList[2].get("ID"),"    " ,studentList[2].get("name"), "    ",studentList[2].get("score1"), "   ",studentList[2].get("score2"), "    ",studentList[2].get("score3"),"   ",studentList[2].get("total"))
print (
    studentList[3].get("ID"),"    " ,studentList[3].get("name"), "    ",studentList[3].get("score1"), "   ",studentList[3].get("score2"), "    ",studentList[3].get("score3"),"   ",studentList[3].get("total"))
studentList.sort(key=lambda x: x["total"])
print ("Sort student information in descending order of total score")
print ("Student number name Chinese mathematics English total score")
print (
   studentList[0].get("ID"),"    " ,studentList[0].get("name"), "    ",studentList[0].get("score1"), "   ",studentList[0].get("score2"), "    ",studentList[0].get("score3"),"   ",studentList[0].get("total"))
print (
   studentList[1].get("ID"),"    " ,studentList[1].get("name"), "    ",studentList[1].get("score1"), "   ",studentList[1].get("score2"), "    ",studentList[1].get("score3"),"   ",studentList[1].get("total"))
print (
   studentList[2].get("ID")," " ,studentList[2].get("name"), " ",studentList[2].get("score1"), " ",studentList[2]. get("score2"), " ",studentList[2].get("score3")," ",studentList[2].get("total"))
print (
   studentList[3].get("ID")," " ,studentList[3].get("name"), " ",studentList[3].get("score1"), " ",studentList[3]. get("score2"), " ",studentList[3].get("score3")," ",studentList[3].get("total"))

The result of running the above code is shown in Figure 10.

python tuples and dictionaries
Figure 10
Of course, it can also be arranged according to the total score from high to low, just add reverse=True in the brackets of the sort() method, which means to arrange in reverse order, the complete code is as follows.

studentList=[]
student={}
student={"name":"李若瑜","ID":"1","score1":95,"score2":100,"score3":96,"total":291}
studentList.append(student)
student={"name":"张子栋","ID":"2","score1":93,"score2":100,"score3":95,"total":288}
studentList.append(student)
student={"name":"王小明","ID":"3","score1":100,"score2":94,"score3":100,"total":294}
studentList.append(student)
studentList[1]["score2"]=98
studentList[1]["total"]=studentList[1]["score1"]+studentList[1]["score2"]+studentList[1]["score3"]
print("Next, we will enter the grades of a new classmate")
student={}
student["name"]=input("Please enter the student's name: ")
student["ID"]=input("Please enter the student ID: ")
student["score1"]=input("Please enter the student's Chinese score:")
student["score2"]=input("Please enter the student's math score:")
student["score3"]=input("Please enter the student's English score:")
student["total"]=float(student["score1"]) + float(student["score2"]) + float(student["score3"])
studentList.append(student)
print( "Now there are scores of "+str(len(studentList)) +" students, their scores are as follows: ")
print ("Student number name Chinese mathematics English total score")
print (
    studentList[0].get("ID"),"    " ,studentList[0].get("name"), "    ",studentList[0].get("score1"), "   ",studentList[0].get("score2"), "    ",studentList[0].get("score3"),"   ",studentList[0].get("total"))
print (
    studentList[1].get("ID"),"    " ,studentList[1].get("name"), "    ",studentList[1].get("score1"), "   ",studentList[1].get("score2"), "    ",studentList[1].get("score3"),"   ",studentList[1].get("total"))
print (
    studentList[2].get("ID"),"    " ,studentList[2].get("name"), "    ",studentList[2].get("score1"), "   ",studentList[2].get("score2"), "    ",studentList[2].get("score3"),"   ",studentList[2].get("total"))
print (
    studentList[3].get("ID"),"    " ,studentList[3].get("name"), "    ",studentList[3].get("score1"), "   ",studentList[3].get("score2"), "    ",studentList[3].get("score3"),"   ",studentList[3].get("total"))
studentList.sort(key=lambda x: x["total"],reverse=True)
print ("Sort student information from high to low according to the total score")
print ("Student number name Chinese mathematics English total score")
print (
    studentList[0].get("ID"),"    " ,studentList[0].get("name"), "    ",studentList[0].get("score1"), "   ",studentList[0].get("score2"), "    ",studentList[0].get("score3"),"   ",studentList[0].get("total"))
print (
    studentList[1].get("ID"),"    " ,studentList[1].get("name"), "    ",studentList[1].get("score1"), "   ",studentList[1].get("score2"), "    ",studentList[1].get("score3"),"   ",studentList[1].get("total"))
print (
    studentList[2].get("ID"),"    " ,studentList[2].get("name"), "    ",studentList[2].get("score1"), "   ",studentList[2].get("score2"), "    ",studentList[2].get("score3"),"   ",studentList[2].get("total"))
print (
    studentList[3].get("ID")," " ,studentList[3].get("name"), " ",studentList[3].get("score1"), " ",studentList[3]. get("score2"), " ",studentList[3].get("score3")," ",studentList[3].get("total"))

The result of running the code is shown in Figure 11.Python tuples and dictionaries
Figure 11
If you want to sort by the grades of a certain course, you can also do it, for example, you need to sort by the Chinese grades. However, it should be noted that because Chen Jingxian’s Chinese scores are entered, that is to say, his Chinese scores are now in string type, so when sorting, they need to use the float() function to convert them into floating-point numbers. Complete code eg.

studentList=[]
student={}
student={"name":"Li Ruoyu","ID":"1","score1":95,"score2":100,"score3":96,"total":291}
studentList.append(student)
student={"name":"Zhang Zidong","ID":"2","score1":93,"score2":100,"score3":95,"total":288}
studentList.append(student)
student={"name":"Wang Xiaoming","ID":"3","score1":100,"score2":94,"score3":100,"total":294}
studentList.append(student)
studentList[1]["score2"]=98
studentList[1]["total"]=studentList[1]["score1"]+studentList[1]["score2"]+studentList[1]["score3"]
print("Next, we will enter the grades of a new classmate")
student={}
student["name"]=input("Please enter the student's name: ")
student["ID"]=input("Please enter the student ID: ")
student["score1"]=input("Please enter the student's Chinese score:")
student["score2"]=input("Please enter the student's math score:")
student["score3"]=input("Please enter the student's English score:")
student["total"]=float(student["score1"]) + float(student["score2"]) + float(student["score3"])
studentList.append(student)
print( "Now there are scores of "+str(len(studentList)) +" students, their scores are as follows: ")
print ("Student number name Chinese mathematics English total score")
print (
    studentList[0].get("ID"),"    " ,studentList[0].get("name"), "    ",studentList[0].get("score1"), "   ",studentList[0].get("score2"), "    ",studentList[0].get("score3"),"   ",studentList[0].get("total"))
print (
    studentList[1].get("ID"),"    " ,studentList[1].get("name"), "    ",studentList[1].get("score1"), "   ",studentList[1].get("score2"), "    ",studentList[1].get("score3"),"   ",studentList[1].get("total"))
print (
    studentList[2].get("ID"),"    " ,studentList[2].get("name"), "    ",studentList[2].get("score1"), "   ",studentList[2].get("score2"), "    ",studentList[2].get("score3"),"   ",studentList[2].get("total"))
print (
    studentList[3].get("ID"),"    " ,studentList[3].get("name"), "    ",studentList[3].get("score1"), "   ",studentList[3].get("score2"), "    ",studentList[3].get("score3"),"   ",studentList[3].get("total"))
studentList.sort(key=lambda x: float(x["score1"]))
print ("Order students' information from low to high according to Chinese grades")
print ("Student number name Chinese mathematics English total score")
print (
    studentList[0].get("ID"),"    " ,studentList[0].get("name"), "    ",studentList[0].get("score1"), "   ",studentList[0].get("score2"), "    ",studentList[0].get("score3"),"   ",studentList[0].get("total"))
print (
    studentList[1].get("ID"),"    " ,studentList[1].get("name"), "    ",studentList[1].get("score1"), "   ",studentList[1].get("score2"), "    ",studentList[1].get("score3"),"   ",studentList[1].get("total"))
print (
    studentList[2].get("ID")," " ,studentList[2].get("name"), " ",studentList[2].get("score1"), " ",studentList[2]. get("score2"), " ",studentList[2].get("score3")," ",studentList[2].get("total"))
print (
    studentList[3].get("ID")," " ,studentList[3].get("name"), " ",studentList[3].get("score1"), " ",studentList[3]. get("score2"), " ",studentList[3].get("score3")," ",studentList[3].get("total"))

The running result of the code is shown in Figure 12. Chen Jingxian’s Chinese score is the lowest, so he ranks first, while Wang Xiaoming’s score is the highest, ranking last.

Python tuples and dictionaries
Figure 12
Now, we have another problem. What if we want to see a classmate’s score? Above we used the index() method to find the corresponding index value, but now since the elements in the list are of dictionary type, there is no way to use the index() method directly. It doesn’t matter, don’t worry! In the next section, we will use the newly learned knowledge to further improve the transcript procedure.

Leave a Comment

CONTENTS