Function
Function
Syntax
def functionname(parameters ):
function_suite
return [expression]
Example
def my_function():
print("Hello from a function")
my_function()
Output:
Hello from a function
def sub(a,b):
c=a-b
print("sub:",c)
def mul(a,b):
c=a*b
print("Mulitiplication:",c)
def divi(a,b):
c=a//b
print("Division:",c)
def rem(a,b):
c=a%b
print("Remainder:",c)
Output :
def func1():
print ("I am learning Python function")
print ("still in func1")
func1()
def square(x):
return x*x
print(square(4))
def multiply(x=5,y=0):
print("value of x=",x)
print("value of y=",y)
return x*y
print(multiply(y=2,x=4))
Output :
return total;
sum(a, b);
print ("Outside the function global total : ", total)
Output :
Enter number A: 20
Enter number B: 30
Inside the function local total :50
Outside the function global total : 0
Multiple Calling
def my_function(fname):
print(fname + " Reference")
my_function("Emil")
my_function("Whatsup")
my_function("Telegram")
Output :
Emil Reference
Whatsup Reference
Telegram Reference
function definitions cannot be empty, but if you for some reason have a function definition with
no content, put in the pass statement to avoid getting an error.
def myfunction():
pass
"1. Add\n" \
"2. Subtract\n" \
"3. Multiply\n" \
"4. Divide\n")
if select == 1:
elif select == 3:
elif select == 4:
else:
print("Invalid input")
Output:
A class in Python serves as a blueprint for creating objects. It encapsulates data (attributes) and
functionality (methods) related to those objects. Classes provide a way to define custom data
types, enabling the creation of complex structures.
An object can be defined as a data field that has unique attributes and behavior
class and objects
class MyClass:
variable = "BALA"
myobjectx = MyClass()
print(myobjectx.variable)
output: BALA
def greet(self):
print('Hello')
harry = Person()
harry.greet()
output: 'Hello'
class with function Return and Parameters
class add:
def findsum(self, n1, n2):
sum = n1 + n2
return sum
n1 = 100
n2 = 50
Obj = add()
sum =Obj.findsum(n1, n2)
print("The sum of two numbers is: ", sum)
n1 = input("enter name")
n2 = int(input("enter age"))
Obj = add()
sum =Obj.findsum(n1, n2)
Output: senthil,23
def details(self):
print("Employee Name:", self.name)
print("Employee Age:", self.age)
def loan(self):
print("Loan number :",self.lno)
print("Amount :",self.amo)
intr=self.amo*(2/100)
print("Interest : ", intr)
INHERITANCE
Inheritance is defined as the capability of one class to derive or inherit the properties from some
other class and use it whenever needed.
Example :
class Child:
def getName(self):
return self.name
class Student(Child):
std = Child("Ram")
nam=std.getName()
print("Name:",nam)
Output:
Name: Ram
Single Inheritance: Single inheritance enables a derived class to inherit properties from a single
parent class, thus enabling code reusability and the addition of new features to existing code.
class Parent:
def func1(self):
print("This function is in parent class.")
class Child(Parent):
def func2(self):
print("This function is in child class.")
object = Child()
object.func1()
object.func2()
Output:
This function is in parent class.
This function is in child class.
Multiple Inheritance: When a class can be derived from more than one base class this type of
inheritance is called multiple inheritance. In multiple inheritance, all the features of the base
classes are inherited into the derived class.
class Mother:
def mother(self):
print(self.mothername)
class Father:
def father(self):
print(self.fathername)
# Derived class
class Son(Mother, Father):
def parents(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
# Driver's code
s1 = Son()
s1.fathername = "RAM"
s1.mothername = "SITA"
s1.parents()
Output:
Father : RAM
Mother : SITA
Multilevel Inheritance: In multilevel inheritance, features of the base class and the derived class
are further inherited into the new derived class. This is similar to a relationship representing a
child and grandfather.
class emp:
eno=int(input("Enter Emp no:"))
nam=(input("Enter Emp Name:"))
des=(input("Enter Emp Desigination:"))
def dispaly(self):
print("Emp no:",self.eno)
print("Emp Name:",self.nam)
print("Emp Desigination:",self.des)
class salary (emp):
def result(self):
ns=self.bpay+self.hra+self.da
print("Net Salary :",ns)
class sign:
sa=salary()
sa.dispaly()
sa.result()
Output:
class emp:
no=int(input("enter employee no: "))
name=input("enter employee name: ")
des=input("enter designation: ")
def display(self):
print("employee no: ",self.no)
print("employee name: ",self.name)
print("designation: ",self.des)
class salary(emp):
bp=int(input("enter employee bpay: "))
hra=int(input("enter employee hra: "))
da=int(input("enter employee da: "))
ns=bp+hra+da
def result(self):
print("employee bp: ",self.bp)
print("employee hra: ",self.hra)
print("da: ",self.da)
print("net salary: ",self.ns)
class bonus(salary):
n=int(input("enter increment:"))
def out(self):
bo=self.ns+self.n
print("Increment salary: ",bo)
e=bonus()
e.display()
e.result()
e.out()
Output:
Hierarchical Inheritance: When more than one derived classes are created from a single base
this type of inheritance is called hierarchical inheritance. In this program, we have a parent
(base) class and two child (derived) classes .
class Parent:
def func1(self):
print("This function is in parent class.")
class Child1(Parent):
def func2(self):
print("This function is in child 1.")
class Child2(Parent):
def func3(self):
print("This function is in child 2.")
class Child3(Parent):
def func4(self):
print("This function is in child 3.")
object1 = Child1()
object2 = Child2()
object3 = Child3()
object1.func1()
object1.func2()
object2.func1()
object2.func3()
object3.func1()
object3.func4()
Output:
This function is in parent class.
This function is in child 1.
This function is in parent class.
This function is in child 2.
This function is in parent class.
This function is in child 3.
class School:
def func1(self):
print("This function is in school.")
class Student1(School):
def func2(self):
print("This function is in student 1. ")
class Student2(School):
def func3(self):
print("This function is in student 2.")
object = Student3()
object.func1()
object.func2()
object.func3()
object.func4()
Output:
This function is in school
This function is in student 1
This function is in student 2
This function is in student 3