Table of Contents
hide
Example : A Python program to display the Message/Values using Class and Object.
# Define the class
class Message:
def display(self):
print("Hello India")
# Create an object of the class
msg = Message()
# Call the method to display the message
msg.display()
Output:
Hello India
NB: Like this keyword, self represents the instance of the class and is used to access attributes/variables and methods within the class.
------------- OR --------------
# Define the class
class Calculation:
def input(self):
self.x=50
self.y=20
def display(self):
print("The values are = ",self.x,self.y)
# Create an object of the class
msg = Calculation()
msg.input()
msg.display()
Output:
The values are = 50 20
------------- OR --------------
# Define the class
class Calculation:
def input(self,val1,val2):
self.x=val1
self.y=val2
def display(self):
print("The values are = ",self.x,self.y)
# Create an object of the class
msg = Calculation()
msg.input(50,20)
msg.display()
Output:
The values are = 50 20
Example : A Python program to display the sum of two values using Class and Object.
# Define the class
class Calculation:
def input(self):
self.x=50
self.y=20
def process(self):
self.z=self.x+self.y
def display(self):
print("The addition is result is = ",self.z)
print("The sum of two value",self.x,"and",self.y,"is = ",self.z)
print(f"The sum of {self.x} and {self.y} is = {self.z}")
# Create an object of the class
msg = Calculation()
# Call the method to display the message
msg.input()
msg.process()
msg.display()
Output:
The addition is result is = 70
The sum of two value 50 and 20 is = 70
The sum of 50 and 20 is = 70
------------- OR --------------
# Define the class
class Calculation:
def input(self,val1,val2):
self.x=val1
self.y=val2
def process(self):
self.z=self.x+self.y
return self.z
# Create an object of the class
msg = Calculation()
msg.input(20,50)
k=msg.process()
print("The addition result is =",k)
Output:
The addition result is = 70
------------- OR --------------
# Define the class
class Calculator:
# Constructor to initialize values without arguments
def __init__(self):
self.num1 = 0
self.num2 = 0
self.result = 0
# Function to input numbers
def input(self):
self.num1 = 50
self.num2 = 20
# Function to calculate the sum
def process(self):
self.result = self.num1 + self.num2
# Function to display the result
def display(self):
print(f"The sum of {self.num1} and {self.num2} is : {self.result}")
# Main program
# Create an object of the class
calc = Calculator()
# Call the functions in sequence
calc.input()
calc.process()
calc.display()
Output:
The sum of 50 and 20 is: 70
NB: __init__() is a special constructor in-built method which is a method that initializes object attributes/variables and is automatically called when an object is created.
------------- OR --------------
# Define the class
class Calculator:
# Constructor to initialize values with arguments
def __init__(self,num1,num2):
self.num1 = num1
self.num2 = num2
# Function to calculate the sum
def process(self):
self.result = self.num1 + self.num2
# Function to display the result
def display(self):
print(f"The sum of {self.num1} and {self.num2} is : {self.result}")
# Main program
# Create an object of the class
calc = Calculator(100,200)
# Call the functions in sequence
calc.process()
calc.display()
Output:
The sum of 100 and 200 is : 300
------------- OR --------------
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display(self):
print(f"Name: {self.name}, Age: {self.age}")
# Create multiple objects of the class
person1 = Person("Raman", 25)
person2 = Person("Rohan", 30)
person3 = Person("Priyanka", 35)
# Call the method for each object
person1.display()
person2.display()
person3.display()
Output:
Name: Raman, Age: 25
Name: Rohan, Age: 30
Name: Priyanka, Age: 35
Example : A Python program to display the sum of two values using Multiple Object.
# Define the class
class Calculation:
def input(self,val1,val2):
self.x=val1
self.y=val2
def display(self):
print("The values are = ",self.x,self.y)
# Creation of multiple object of the class
obj1 = Calculation()
obj2 = Calculation()
obj1.input(50,20)
obj2.input(20,40)
obj1.display()
obj2.display()
Output:
The values are = 50 20
The values are = 20 40
Example : A Python program to display the sum of two values using Nested Functions.
# Define a function with a nested function
def additon(num1, num2):
# Nested function to calculate the sum
def calculate_sum(a, b):
return a + b
# Call the nested function
result = calculate_sum(num1, num2)
return result
# Main program
# Call the outer function
sum_result = addition(20, 50)
print(f"The sum of 20 and 50 is: {sum_result}")
Output:
The sum of 20 and 50 is: 70
Example : A Python program to display the values using Function Overloading type.
class Calculator:
def add(self, a, b=0, c=0):
return a + b + c
# Create an object of the Calculator class
calc = Calculator()
# Call the method with different numbers of arguments
print(calc.add(10))
print(calc.add(10, 20))
print(calc.add(10, 20, 30))
Output;
10 #(by adding 10 + 0 + 0)
30 #(by adding 10 + 20 + 0)
60 #(by adding 10 + 20 + 30)
NB : Python does not directly support method overloading. However, we can achieve similar functionality by using the concept of default arguments as above discussed.
0 Comments