Sunday, October 29, 2023

CHAPTER 7 LEARN CLASS IN PYTHON

 LEARN PYTHON THROUGH SERIES OF ROBOT'S ACTIVITIES 

A user-defined class serves as a template or prototype from which objects can be built. Classes offer a way to group functionality and data. A new class produces a new type of object, enabling the creation of new instances of that type. Each instance of a class may have characteristics connected to it to preserve its state. Class instances may also contain methods for changing their state that is defined by their class. Sana is working as a professor in a college. Sana had ordered the robot named Tana, to monitor students of her college to improve the educational methods, that is to say,

how well the students have learned the content of each subject by conducting a series of tests or exams on each student.

 

Sana gives the marks obtained by the student to the robot, Tana, and similarly gives the completed question paper to the robot. At present questions are asked from 5 subjects of 20 marks each from each subject.

 

The function of this task is to assess whether the content of all 5 subjects is well understood by the students based on how much each student has scored in these 5 subjects.

To do this, the robot needs information about how each student wrote the answers to the related questions in the 5 subjects,

and the robot has to calculate the percentage of the student's score in each subject and convert it into a percentage and submit it to the professor.

The job of Tana is to get the given data, and marks of the students and, at the same time take the marks of the students in each subject separately and convert it into percentages and get the learning percentage of each subject.

Program 1:

[In]Class Student():

    def __init__(self, name, roll, gen):

        self.name=name

        self.roll=roll

        self.gen=gen

    def show(self):

        print("The student details are: Name :",  self.name, ", Roll No: ", self.roll, ", Gender: ", self.gen)

#This comment prints the student details.

[In]class Student():

    def __init__(self, name, roll, gen):

        self.name=name

        self.roll=roll

        self.gen=gen

    def show(self):

        print("The student details are: Name :",  self.name, ", Roll No: ", self.roll, ", Gender: ", self.gen)

stu1=Student('raj',123,"male")

stu1.show()

[Out]The student details are: Name : raj , Roll No:  123 , Gender:  male

[In]class Science(Student):

    def __init__(self, name, roll, gen):

        super().__init__(name, roll, gen)

        self.marks1=0

        self.marks2=0

    def test1marks(self, score1):

        self.score1=score1

        self.marks1 = self.marks1+ self.score1

        [In]print("The test 1 marks updated", self.marks1)

    def test2marks(self, score2):

        self.score2=score2

        self.marks2 = self.marks2+ self.score2

        [In]print("The test 2 marks updated", self.marks2)

[In]stu1=Science('raji',123,'female')

[In]stu1.test1marks(100)

[Out]The test 1 marks updated 100

[In]stu1.test2marks(10)

[Out]The test 2 marks updated 10

Program 2:

In the second example, A shopkeeper has to calculate the total price for 3 items bought by a customer. The 3 items are moong, oil, and vegetables. The program is very simple to make a class program that contains different functions such as _init_, and price. The output of this program executes the price function with the prices of all the groceries. This is available in the Ingredients class. The values of prices are passed to the Ingredients class.

[In] class Ingredients:

    def __init__(self, m,o,c):

        self.m=m

        self.o=o

        self.c=c

    def price(self):

        tot= self.m + self.o + self.c

        print("The total price is Rs.", tot)

p=Incredients(45,34,4)

p.price()

​[Out] The total price is Rs.  83

The next program explains the functions of a class with more than one function.

[In]class Ingredients:

def unitprice(self,moong,oil,veg):

        self.moong=moong self.oil = oil

        self.veg = veg

        print("The unitprice of moong is Rs",self.moong)

        print("The unitprice of oil is Rs",self.oil)

        print("The unitprice of veg is Rs",self.veg)

def quantity(self,moonquan=float,oilquan=float, vegquan=float):

        self.moonquan=moonquan

        self.oilquan = oilquan

        self.vegquan = vegquan

        print("The quantity of moong is ",self.moonquan,"kg")

        print("The quantity of oil is ",self.oilquan,"kg")

        print("The quantity of veg is ",self.vegquan,"kg")

def price(self,m=float,o=float,v=float,tot=float):

        m=self.moong*self.moonquan

        o=self.oil*self.oilquan

        v=self.veg*self.vegquan

        self.m =m

        self.o =o

        self.v =v

        tot= self.m+self.o+self.v

        print("The total price of the groceries is Rs. ",tot)

p=Incredients()

p.unitprice(1,4,4)

p.quantity(0.5,1,4)

p.price()

[Out] The unitprice of moong is Rs 1

The unitprice of oil is Rs 4

The unitprice of veg is Rs 4

The quantity of moong is  0.5 kg

The quantity of oil is  1 kg

The quantity of veg is  4 kg

The total price of the groceries is Rs.  20.5

 

 

 

No comments:

Post a Comment

CHAPTER 18 EXPLORING THERMODYNAMICS WITH PYTHON: UNDERSTANDING CARNOT'S THEOREM AND MORE

  Python is a versatile programming language that can be used to simulate and analyze various physical phenomena, including thermal physics ...