Q:

Python program to illustrate the working of list of objects

belongs to collection: Python class & object programs

0

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)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python class & object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to convert hours into days... >>
<< Python program to compare two objects using operat...