Site icon Console Flare Blog

Nested Lists: 10 Easy Steps for Student Management System

As we all know a list is a data type in Python that allows us to store multiple values/items of various data types in a single variable which also helps in handling huge amounts of data but still list alone is not sufficient in handling huge data so to solve this problem we use nested structure that means a list consisting many other small lists in itself. To understand the concept of nested lists, let us discuss a project in which we will cover a program called Student Management System.

In this project, we will create a program that allows us to store the name, roll no, and marks of multiple students in a single list, on the other hand, we want to provide an interactive interface to the user with the help of which the user can perform several operations on the data like, add data of a new student, view data of all students, search a student by its name, search a student by its roll no, update any data of a student, and last but not the least delete data of any student.

Step 1: Create an empty list.

In this step, we have to create an empty list by name of students that will be used to store data of different students.

students=[]

2. In this step, we will create a while loop with a permanent True condition so that the program does not end itself after a fixed number of times. The purpose of this loop is to show the options of the operations that the user can perform again and again by choosing one of the options.

while True:
    print("STUDENT MANAGEMENT SYSTEM")
    print("1. Add student")
    print("2. View all students")
    print("3. Search student by name")
    print("4. Search student by roll number")
    print("5. Update student details")
    print("6. Delete student")
    print("7. Exit")

    choice = int(input("Enter your choice (1-7): "))

In the end, we have created a variable choice that will receive input from the user like which operation the user wants to perform.

3. Now we are going to create a logic, that works only if the user selects option no. 1 which is “Add Student”. It will receive the name, roll no., and marks of a student and store it in the main list which is students[] ( the one we declared at the start of the program).

    if choice == 1:
          name = input("Enter student name: ")
          rollno = input("Enter student roll number: ")
          marks = input("Enter student marks: ")
          students.append([name, rollno, marks])
          print("Student added successfully!")

In this piece of code, we have used three variables that are name, rollno, and marks to receive data of a student from the user and then, in the end, we add these data in the form of a list into the main Students list which gives it the structure of the nested list and at the end shows the message of student added successfully.

If you wish to learn more about the Data Science field, Download this brochure

Masters in Data Science

4. After creating the code to add data of a student to the list, let us proceed with the second option which is “View all students”.

elif choice == 2:
        if len(students) == 0:
            print("No students found!")
        else:
            print("List of students:")
            print("Name\t\tRoll No\t\tMarks")
            for student in students:
                print(f'{student[0]}\t\t{student[1]}\t\t{student[2]}')
                print("")

In this code, first of all, we check if there is data already present or not in the main list. If the student’s list has no data it will display the message “No student Found” Otherwise we will proceed with showing the details of the student one by one in tabular format.

5. It is time for the third option, which is “Search student by name” which allows the user to see the details of the student by searching with his name.

elif choice == 3:
        name = input("Enter student name: ")
        for student in students:
            if student[0].lower() == name.lower():
                found = True
                print("Name:", student[0])
                print("Roll No:", student[1])
                print("Marks:", student[2])
                print("")
                break
        else:
            print("No student found with name", name)

In this code, we will use a variable name that stores the name of the student which the user wants to search. After that, we will start for loop in the list which will access details of all the students one by one and then we will cross-check if the name of the students from the list matches the name inputted by the user or not. If it gets matched, we will print the all details of the student and break the loop otherwise the code will jump to the else part and display the message “No student found”

6. It is time for the third option, which is “Search student by roll no” which allows the user to see the details of the student by searching with roll no.

elif choice == 4:
        rollno = input("Enter student roll number: ")
        for student in students:
            if student[1] == rollno:
                found = True
                print("Name:", student[0])
                print("Roll No:", student[1])
                print("Marks:", student[2])
                print("")
                break
        else:
            print("No student found with roll number", rollno)

In this code, we will use a variable rollno that stores the roll number of the student which the user wants to search. After that, we will start for loop in the list which will access details of all the students one by one and then we will cross-check if the roll no of the students from the list matches the roll no inputted by the user or not. If it gets matched, it will print all of the details of the student and break the loop otherwise the code will jump to the else part and display the message “No student found with roll no.”

For more such content and regular updates, follow us on FacebookInstagramLinkedIn

7. The user can go for option no 4 to “Update student details” which will allow the user to update the details of a student.

    elif choice == 5:
        
        rollno = input("Enter student roll number: ")
        
        for student in students:
            if student[1] == rollno:

                print("1. Update name")
                print("2. Update roll number")
                print("3. Update marks")
                subchoice = int(input("Enter your choice (1-3): "))
                if subchoice == 1:
                    name = input("Enter new name: ")
                    student[0] = name
                elif subchoice == 2:
                    rollno = input("Enter new roll number: ")
                    student[1] = rollno
                elif subchoice == 3:
                    marks = input("Enter new marks: ")
                    student[2] = marks
                else:
                    print("Invalid choice!")
                break
        else:
            print("No student found with roll number", rollno)

To update the details of a student in this code, first we will receive roll no as input from the user and store it in the variable rollno. Now we will use for loop, access details of all the students, and cross-check if the roll no is present in the list or not. If the program finds the roll no in the list, the program will show three options to the user which are name, roll no, and marks and it will ask the user which option he wants to choose and update. It will allow the user to select any one of the options at a time.

Suppose the user wants to update the name of the student the code will ask for the new name and it will be updated. Similarly, it will work for roll no and marks, it will get updated and the loop will break but if the code does not find the roll no inputted by the user, the code will jump to the else part and display the message “No student found”.

8. If the user wants to delete the details of any student, this option will help the user in doing so.

elif choice == 6:
        
        rollno = input("Enter student roll number: ")
        
        for student in students:
            if student[1] == rollno:
                
                students.remove(student)
                print("Student deleted successfully!")
                break
        else:
            print("No student found with roll number",rollno)

In this code, we use a variable rollno which will receive roll no as data from the user. After that, we will use for loop to access the details of all the students and cross-check the rollno. If the roll no. inputted by the user gets matched with any of the students. The details of that student will be removed from the main list immediately and the loop will break. Otherwise, the code will jump to the else part and will display the message, “No student found with this roll no.”

9. In the end, we have the final option that allows the user to exit from the loop.

elif choice == 7:
        
        print("Thank you for using the Student Management System!")
        break

The loop will end with the message displaying “Thank you for using the Student Management System!”

10. If the user selects the wrong option the program will jump to the else part and will show the message “Invalid Input”.

If you wish to learn more about data science or want to curve your career in the data science field feel free to join our free workshop on Masters in Data Science with PowerBI, where you will get to know how exactly the data science field works and why companies are ready to pay handsome salaries in this field.

In this workshop, you will get to know each tool and technology from scratch that will make you skillfully eligible for any data science profile.

To join this workshop, register yourself on consoleflare and we will call you back.

Thinking, Why Console Flare?

Register yourself on consoleflare and we will call you back.

Exit mobile version