Object Oriented Programming in Python

Class is a blueprint or framework or template which can be used repeatedly.

Example of Class, Object and Method.

One college decided to give each student one Standard Envelope called student Kit. Inside each Kit we have mark sheet, attendance report and final certificate.

Our student Kit ( or standard Envelope ) is an example of our Class.

One standard format is used give each student one mark sheet. This blank format with subject name and place for marks , is an example of a standard template. For each student this format is used to give them their mark sheet.

The blank format is an example of a method,
Each student can be taken as an object

Each student can have mark sheet, attendance report, final certificates etc. These formatted documents are methods ( can be used by students or objects ).

There are functions within the class which can be used by the objects. So each student ( our object ) can use standard format (methods ) to generate mark sheets , attendance report or final certificate.

There are functions in a class which belong to the object are known as methods .

Let us try some code.

Declaring a Class

class student_kit():
    pass

Declaring an object

class student_kit():
    college_name='ABC College' #class attributes

Alex=student_kit() # object declaration
In above code we have declared one class attribute as college_name. All objects have access to this class attribute. So we can print the value of the class attribute by using object.
print(Alex.college_name)
output is here
ABC College

Initialization of an object.

While creating an object we can assign attributes to it. We call it instance attribute. We will be using built in function __init__() for this. Every time the object is created this initialization process of executing __init__() is executed.
class student_kit():
    college_name='ABC College'  #class attributes

     #instance attributes
    def __init__(self,name,section):
        self.name= name
        self.section=section
        self.height=5  #instance attribute


Alex=student_kit('Alex','A')  # object declaration
print(Alex.name,Alex.section,Alex.height) #object accessing instance attributes
Output is here
Alex A 5
In above code the first parameter of the function __init__() is the object itself. We have given self as name of it, you can use any other name but for better understanding it is advisable to use the name self.

Instance attribute and class attributes

The variable college_name is an class attribute which can be used by the objects. Class can also access the class atrributes.
print(Alex.__class__.college_name) # ABC College
print(student_kit.college_name)    # ABC College
However we can't access instance attribute as class property
print(student_kit.height)  # error
AttributeError: type object 'student_kit' has no attribute 'height' ( what is an AttributeError )

Example 2

We are preparing a Class for all carnivores’ animals.
Class Name is Animal , tiger is an object of Class animal , each object has some properties ( or attributes ) as name , height and weight.

We have declared species ( as class attribute ) and its value is carnivores
Here is the code for this , ( Read all the comments against each statement ) . The last line will generate AttributeError ( What is AttributeError )
class Animal():

     #class attributes
    species='carnivores'

     #instance attributes
    def __init__(self1,name1,age,weight):
        self1.name= name1
        self1.age=age
        self1.weight=weight
        self1.height=5  #instance attribute

 #instantiate the Animal  class
tiger=Animal("Ronald",5,15)  # tiger is an object of Animal class

 # access the instance attributes
print("{} is {} years old animal".format( tiger.name, tiger.age))
print("{} weight is ={}".format( tiger.name,tiger.weight))
print("{} height is ={}".format( tiger.name,tiger.height))

 # object can access the class attributes directly
print("Species is {}".format(tiger.species))

 #object can access the class attributes
print("tiger is a {}".format(tiger.__class__.species)) #carnivores

 #class can access class attributes  
print(Animal.species)

 #instant attributes belong to object ( not to class ) 
print(Animal.height)  #AttributeError: type object 'Animal' has no attribute 'height'

Think : How attributes of a method is available for another method ?


  1. Exercise on Object Oriented Programming
  2. Create one student_kit class. Student object should have name and section.

    Method : mark_sheet , input: mark in three subjects and output should print individual mark and sum of it
    Method : attendance sheet , input: Number of working days , Number of days attended. Percentage of attendance should be presented.
    Method : Final certificate : should display passed from College name and section.
  3. Update the above code to return Certificate showing total marks obtained and attendance in percentage by the student. ( Optional ) You can give grade A, B or C and decide Pass or Fail based on marks obtained ( as entered during generation of mark sheet )
  4. Use the above code to take three students details from user as input and print one by one each student details. ( Hint : Use dictionary having a list to store the data )
  5. Keep your student_kit class in a separate file ( student_kit.py ) . Create a new file using the above code for creating three student inputs. Call the class student_kit from your new file and use the methods to show marksheet , certificate , attendance etc for each student.Use this code in your new file to link the class.
     from student_kit  import student_kit
Solution of above Exercise
Visitors Rating
Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer