Define a class train with following members…Using python.

In Python programming we have many functions and libraries included. but when we talk about user defined function then which in defined by user and can be call again and again withing the same program. This is a good practice done by programmers. “class” is also a part of user defined function. class help us to build data and their functionality together. New type of object is created when we create new class. Each class instance have attributes attached with it which help you to maintain its state. We will discuss a example: Define A Class Train With Following Members to understand the syntax and use of class.

Example 1.

Q. Define a class train with following members. (TrainNo ,Destination ,Distance ,Fuel)

  • A member function calfuel() to calculate and assign value of fuel as per the following criteria:
  • feedinfo(): to input train no, destination, distance and invoke calfuel() function to assign value of fuel.
  • showinfo(): to display all the details for a train.


#Python 3
class train:

    def train_(self, TrainNo ,Destination ,Distance ,fuel):
        self.TrainNo = 0
        self.Destination = Destination
        self.Distance = 0
        self.fuel = 0

#define calfuel which check the condition of fuel as per distance.
    def calfuel(self):
        distance = self.Distance
        if distance <= 1500 : 
            self.fuel = 250
            print(f"Fuel required to travel {distance} is upto ",self.fuel)
        elif distance > 1500 and distance <=3000:
            self.fuel = 1000
            print(f"Fuel required to travel {distance} is upto ",self.fuel)
            self.fuel = 2500
            print(f"Fuel required to travel {distance} is upto ",self.fuel) 

#define feedinfo which takes the value from user                      

    def feedinfo(self):
        self.TrainNo = int(input("Enter the train number : "))
        self.Destination = str(input("Enter the Destination : "))
        self.Distance = int(input("Enter the distance that you need to travel"))

#define showinfo which shows all the user value and calculate the fuel and desplay      

    def showinfo(self):        
      print("\n***Train details***")   
        print("Train Number : ", self.TrainNo)  
        print("Destination : ",self.Destination)  
        print("Distance to travel ", (self.Distance))
        print("Fuel required upto (LITERS) ", self.fuel)  

#calling all the user defined functions              

s = train()
TrainNo = 0
Destination = 0
Distance = 0
fuel = 0
s.train_(TrainNo ,Destination ,Distance ,fuel)


Enter the train number : 34562 
Enter the Destination : Ahmedabad
Enter the distance that you need to travel : 1000
Fuel required to travel 1000 is upto  250
***Train details****  
Train Number :  34562
Destination :  Ahmedabad
Distance to travel  1000
Fuel required upto (LITERS)  250


It has three different case as mention in question above accordingly you can check all the cases in output.

More solved examples on python class.

Q. Define a class employee with the following specifications:                                              

  •   empno integer
  •   ename 20 characters
  •   basic, hra, da float
  •   netpay float
  •       calculate() A function to calculate basic + hra + da with float return type

o   havedata() function to accept values for empno, sname, basic, hra, da and invoke calculate() to calculate netpay.

o   dispdata() function to display all the data members on the screen.

Related article : Why To Learn Python Scope, Applications, Salary, Courses.

Q. Define a class HOTEL with the following description:

Ø  Rno             //Data Member to store Room No

Ø  Name          //Data Member to store customer Name

Ø  Tariff           //Data Member to store per day charge

Ø  NOD           //Data Member to store Number of days

Ø  CALC         //A function to calculate and return amount as NOD*Tariff and if the value of NOD*Tariff is more than 10000 then as 1.05*NOD*Tariff

o   Checkin( )   //A function to enter the content RNo,Name, Tariff and NOD

o   Checkout()  //A function to display Rno, Name, Tariff, NOD and Amount (Amount to be displayed by calling function CALC( 

Q. Define a class Applicant with following description:

  • ANo : ( Admission Number): type long
  • Name : type string
  • Agg :(Aggregate Marks) : type float
  • Grade :type str
  • A member function GradeMe( ) to find the Grade as per the Aggregate Marks obtained by a student. Equivalent Aggregate marks range and the respective Grades are shown as follows
  1. Aggregate Marks           Grade
  2. = 80        A
  3. Less than 80 and > = 65  B
  4. Less than 65 and > = 50  C
  5. Less than 50          D

o   A function Enter( ) to allow user to enter values for ANo, Name, Agg & call function GradeMe( ) to find the Grade

o   A function Result ( )   to allow user to view the content of all the data members.

Related Article : Top 5 Programming Languages In World To Boost Your Carrier.

Q. Define a class Student with the following specifications :                                                

  •   roll_no        integer
  •   name           str
  •   class            str
  •   marks[5]      integer
  •   percentage   float
  •       Calculate() a function that calculates overall percentage of marks and return the percentage of marks.

o   Readmarks() a function that reads marks and invoke the Calculate function.

o   Displaymarks() a function that prints the marks.

Know more : All About Electronics And Communication Engineering

This is the solution of some basic problems on class using python, You can change the design of the output and words as per your choice. I have provided a basic syntax on this problems. 

“Also see  some examples based on Array in Python below.”

Q3 Write a function that will accept a 2-D array and its row and column size as argument and find sum of rows and columns.
Click here for answer.

Q4. Write a function in python to find the sum of both left and right diagonal elements from a two dimensional array (matrix).
Click here for answer.

Leave a Comment

Your email address will not be published.

Tech Info