Python program to illustrate the working of list of objects

Here, we will see a program that will depict the creation and usage of a list of objects in Python.
Submitted by Shivang Yadav, on February 18, 2021

List in python:

List is a data structure in python which is used to store multiple elements. These elements can be any other data structure. 

Objects in Python:

Objects are instances of classes in python.

List of objects is a list that stores multiple objects.

Creating a list of objects:

listName = list()

Adding objects to the list:

listName.append(object)

Program to illustrate the working of list of objects in Python

class Student:
    def getStudentInfo(self):
        self.__rollno = input("Enter Roll No : ")
        self.__name = input("Enter Name : ")
        self.__phy = int(input("Enter Physics Marks : "))
        self.__chem = int(input("Enter Chemistry Marks : "))
        self.__math = int(input("Enter Maths Marks : "))

    def PutStudent(self):
        print("Roll Number : ", self.__rollno, end = ", ")
        print("Name : ", self.__name, end = ", ")
        self.Result()

    def Result(self):
        total = self.__phy + self.__chem + self.__math
        per = (int)(total / 3)
        print("Percentage : ", per, end = ", ")
        if (per >= 60):
            print("Result = Pass")
        else:
            print("Result = Fail")

studentList = list()

while(True):
    studentObject = Student()
    studentObject.getStudentInfo()
    studentList.append(studentObject)
    ch=input("Continue y/n?")
    if ch=='n':
        break
    
print("List Pointer : " , studentList)

for i in range(len(studentList)):
    print("Student : ", (i+1))
    studentList[i].PutStudent()

Output:

Enter Roll No : 054
Enter Name : John
Enter Physics Marks : 56
Enter Chemistry Marks : 87
Enter Maths Marks : 76
Continue y/n?y
Enter Roll No : 002
Enter Name : Jane
Enter Physics Marks : 87
Enter Chemistry Marks : 67
Enter Maths Marks : 99
Continue y/n?n
List Pointer :  [<__main__.Student object at 0x7f72020177f0>, <__main__.Student object at 0x7f72020178d0>]
Student :  1
Roll Number :  054 , Name :  John , Percentage :  73 , Result = Pass
Student :  2
Roll Number :  002 , Name :  Jane , Percentage :  84 , Result = Pass

Python class & object programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.