0% found this document useful (0 votes)
2 views17 pages

Function

The document provides an overview of Python functions, including syntax, arithmetic operations, return values, and variable scopes. It also covers classes and objects, demonstrating concepts like constructors, inheritance types (single, multiple, multilevel, hierarchical, and hybrid), and examples of employee salary calculations. Overall, it serves as a comprehensive guide to understanding basic programming constructs in Python.

Uploaded by

ramalanbegam1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Function

The document provides an overview of Python functions, including syntax, arithmetic operations, return values, and variable scopes. It also covers classes and objects, demonstrating concepts like constructors, inheritance types (single, multiple, multilevel, hierarchical, and hybrid), and examples of employee salary calculations. Overall, it serves as a comprehensive guide to understanding basic programming constructs in Python.

Uploaded by

ramalanbegam1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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

Arithmetic operation using Function


def add(a,b):
c=a+b
print("add:",c)

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)

a = int(input("Enter number A: "))


b = int(input("Enter number B: "))
add(a,b)
sub(a,b)
mul(a,b)
divi(a,b)
rem(a,b)

Output :

function with return

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 the String


def printme(str):
return(str)
string ="This prints a passed string into this function"
str1=printme(string)
print(str1)
Output :
This prints a passed string into this function

Global vs. Local variables

total = 0; # This is global variable.

def sum( arg1, arg2 ):

total = arg1 + arg2; # Here total is local variable.

print ("Inside the function local total : ", total)

return total;

a = int(input("Enter number A: "))

b = int(input("Enter number B: "))

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

Default Parameter Value

If we call the function without argument, it uses the default value:


def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Output :
I am from Sweden
I am from India
I am from Norway
I am from Brazil
The pass Statement

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

Python program for simple calculator

def add(num1, num2):

return num1 + num2

def subtract(num1, num2):

return num1 - num2

def multiply(num1, num2):

return num1 * num2

def divide(num1, num2):

return num1 / num2

print("Please select operation -\n" \

"1. Add\n" \

"2. Subtract\n" \

"3. Multiply\n" \

"4. Divide\n")

select = int(input("Select operations form 1, 2, 3, 4 :"))

number_1 = int(input("Enter first number: "))

number_2 = int(input("Enter second number: "))

if select == 1:

print(number_1, "+", number_2, "=",add(number_1, number_2))


elif select == 2:

print(number_1, "-", number_2, "=", subtract(number_1, number_2))

elif select == 3:

print(number_1, "*", number_2, "=",multiply(number_1, number_2))

elif select == 4:

print(number_1, "/", number_2, "=",divide(number_1, number_2))

else:

print("Invalid input")

Output:

CLASSES AND OBJECTS

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

class with function


class Person:

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)

Output: The sum of two numbers is: ", 150

class with function and Parameters


class add:
def findsum(self, n1, n2):
print(n1,n2)

n1 = input("enter name")
n2 = int(input("enter age"))
Obj = add()
sum =Obj.findsum(n1, n2)
Output: senthil,23

Constructor Program with class


class Employee:
def __init__(self, name, age):
self.name = name
self.age = age

def details(self):
print("Employee Name:", self.name)
print("Employee Age:", self.age)

emp = Employee("Sam", 36)


emp.details()
Output:
Employee Name: Sam
Employee Age: 36

Bank Program with class


class bank:
def __init__(self,lno,amo):
self.lno=lno
self.amo=amo

def loan(self):
print("Loan number :",self.lno)
print("Amount :",self.amo)
intr=self.amo*(2/100)
print("Interest : ", intr)

n1 = input("Enter loan number")


n2 = int(input("Enter Amount"))
ba = bank(n1, n2)
ba.loan()
Output:
Enter loan number : 8963
Enter Amount: 50000
Loan number: 8963
Amount: 50000
Interest : 2500.0

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 __init__(self, name):


self.name = name

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.

Employee Salary Calculation

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):

bpay=int(input("Enter Emp Bpay:"))


hra=int(input("Enter Emp HRA:"))
da=int(input("Enter Emp DA:"))

def result(self):
ns=self.bpay+self.hra+self.da
print("Net Salary :",ns)
class sign:

sa=salary()
sa.dispaly()
sa.result()

Output:

Enter Emp no : 85630


Enter Emp Name: Raja
Enter Emp Desigination: Manager
Enter Emp Bpay: 25000
Enter Emp HRA:2000
Enter Emp DA:3000
Emp no : 85630
Emp Name: Raja
Emp Desigination: Manager
Net Salary : 30000

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:

Enter Emp no : 85630


Enter Emp Name: Raja
Enter Emp Desigination: Manager
Enter Emp Bpay: 25000
Enter Emp HRA:2000
Enter Emp DA:3000
Enter increment:1000
Emp no : 85630
Emp Name: Raja
Emp Desigination: Manager
Net Salary : 30000
Increment salary:31000

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.

Hybrid Inheritance: Inheritance consisting of multiple types of inheritance is called hybrid


inheritance

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.")

class Student3(Student1, Student2):


def func4(self):
print("This function is in student 3.")

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

You might also like