Experiment : 02
Title : Creating functions, classes and objects using python. Demonstrate exception
handling and inheritance.
Theory:
Creating Functions in Python:
In Python, functions are defined using the "def" keyword followed by the
function name and arguments enclosed in parentheses. Here is an example of a
simple function that takes two arguments and returns their sum:
Example:
def add_numbers(x, y):
sum = x + y
return sum
Creating Classes and Objects in Python:
Classes are defined using the "class" keyword followed by the class name. Here is
an example of a simple class that defines a student:
Example:
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def display_student(self):
print("Name:", self.name, ", Age:",
self.age)
To create an object of this class, we can use the following syntax:
s = Student("John", 20)
s.display_student()
Exception Handling in Python:
In Python, we can handle exceptions using the "try-except" block. Here is an
example of a try-except block that handles a division by zero error:
Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero error!")
Inheritance in Python:
In Python, we can create a new class by deriving from an existing class. The new
class is called the "subclass" and the existing class is called the "superclass". Here
is an example of a superclass and a subclass:
Example:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("Animal speaks")
class Dog(Animal):
def __init__(self, name):
Animal.__init__(self, name)
def speak(self):
print("Dog barks")
Program :
Aim : Single level (Program 1)
# Student--is a parent class |
# Marks is a child class
class Student:
rollno=0
name=''
def inputStudent(self):
self.rollno=int(input('Enter the rollno:'))
self.name=input('Enter the name')
class Marks(Student): #Marks inherits the Student
class grade=''
def inputGrade(self):
self.grade=input('Enter the grade:')
def display(self):
print(self.rollno,' ',self.name,'
',self.grade)#access rollno and # name from Student
m=Marks()
m.inputStudent() #access Student class methods with
Marks obj m.inputGrade()
m.display()
Output:
Aim : Single level (Program 2)
class School:
school_name=''
def inputSchool(self):
self.school_name=input('Enter the schoolName:')
class College:
clgName=''
def inputCollege(self):
self.clgName=input('Enter the clgName:')
class Student(School,College):
rollno=0
marks=0
def inputStudent(self):
self.rollno=int(input('Enter the rollno:'))
self.marks=int(input('Enter the marks:'))
def display(self):
print(self.rollno,' ',self.marks,'
',self.school_name,' ',self.clgName)
s=Student()
s.inputStudent()
s.inputSchool()
s.inputCollege()
s.display()
Output:
Aim : Multilevel
# Student
# |
# Marks
# |
# Sports
class Student:
rollno=0
name=''
def inputStudent(self):
self.rollno=int(input('Enter the rollno:'))
self.name=input('Enter the name')
class Marks(Student): #Marks inherits the Student
class grade=''
def inputGrade(self):
self.grade=input('Enter the grade:')
class Sports(Marks):
score=0
def inputScore(self):
self.score=int(input('Enetr the score'))
def display(self):
print(self.rollno,' ',self.name,' ',self.grade,'
',self.score)
s=Sports()
s.inputStudent()
s.inputGrade()
s.inputScore()
s.display()
Output:
Aim : Hierarchical
class Person:
name=''
age=0
def inputPerson(self):
self.name=input('Enter the name:')
self.age=int(input('Enter the age:'))
class Student(Person):
rollno=0
marks=0
def inputStudent(self):
self.rollno=int(input('Enter the rollno:'))
self.marks=int(input('Enter the marks:'))
def displayStudent(self):
print(self.name,' ',self.age,' ',self.rollno,'
',self.marks)
class Employee(Person):
empid = 0
salary = 0
def inputEmployee(self):
self.empid = int(input('Enter the empid:'))
self.salary = int(input('Enter the salary:'))
def displayEmployee(self):
print(self.name, ' ', self.age, ' ',
self.empid, ' ', self.salary)
print('====Student details=====')
s=Student()
s.inputPerson()
s.inputStudent()
s.displayStudent()
print('====Employee details=====')
e=Employee()
e.inputPerson()
e.inputEmployee()
e.displayEmployee()
#Defualt constr
# class Person:
#
# def __init__(self):
# print('Defualt constr...')
#
# p=Person()
#Parameterized constr
class Person:
name=''
age=0
#John,12
def __init__(self,n,a):
self.name=n
self.age=a
def display(self):
print(self.name,' ',self.age)
# n=input('Enter the name')
# p1=Person(n,12)
p1=Person('John',12)
p1.display()
Output:
Conclusion:
We have learned Basics of functions, classes, objects, exception handling and inheritance
using python.