Example : A Python program to display the Message/Values using a Class and an 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=100
        self.y=200
        
        self.z=self.x+self.y
    
        print("The values are = ",self.z)

# Create an object of the class
msg = Calculation()

msg.input()

-------------  OR  --------------

# Define the class
class Calculation:
    
    def input(self,val1,val2):
        self.x=val1
        self.y=val2
        
        self.z=self.x+self.y
    
        print("The values are = ",self.z)

# Create an object of the class
msg = Calculation()

a=int(input("Enter first value = "))
b=int(input("Enter second value = "))
msg.input(a,b)

Output:
Enter first value = 10
Enter second value = 20
The values are =  30

-------------  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 a Class and an 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 Calculation:
    
    def input(self):
        self.x=int(input("Enter first value = "))
        self.y=int(input("Enter second value = "))
    
    def process(self):
        self.z=(self.x)+(self.y) 
        print (self.z)

# Create an object of the class
msg = Calculation()

msg.input()
msg.process()

Output:
Enter first value = 100
Enter second value = 200
300

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

a=int(input("Enter first value = "))
b=int(input("Enter second value = "))
msg.input(a,b)
msg.display()

Output:
Enter first value = 100
Enter second value = 200
The values are =  300

-------------  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 objects.
# 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 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 result is =", self.z)
        print("The sum of two values", self.x, "and", self.y, "is =", self.z)
        print(f"The sum of {self.x} and {self.y} is = {self.z}")
        
    def all(self):
        self.input()     # call input method
        self.process()   # call process method
        self.display()   # call display method    

# Create an object of the class
msg = Calculation()

# Call the method to display the message
msg.all()

Output:

The addition result is = 70
The sum of two values 50 and 20 is = 70
The sum of 50 and 20 is = 70

-------------  OR  --------------

# Define a function with nested functions
def calculation(num1, num2):
    # Nested function to calculate sum
    def add(a, b):
        return a + b
    
    def subtract(a, b):
        return a - b
    
    def mult(a, b):
        return a * b
    
    def div(a, b):
        if b != 0:
            return a / b
        else:
            return "Division by zero is not allowed"
    
    # Call the nested functions inside the outer function
    sum_result = add(num1, num2)
    sub_result = subtract(num1, num2)
    mul_result = mult(num1, num2)
    div_result = div(num1, num2)
    
    # Return all results using a dictionary concept
    return {"sum": sum_result, "sub": sub_result, "mul": mul_result, "div": div_result}

# Call the outer function
results = calculation(20, 50)

print(f"The sum of 20 and 50 is: {results['sum']}")
print(f"The subtraction of 20 and 50 is: {results['sub']}")
print(f"The multiplication of 20 and 50 is: {results['mul']}")
print(f"The division of 20 and 50 is: {results['div']}")

Output:

The sum of 20 and 50 is: 70
The subtraction of 20 and 50 is: -30
The multiplication of 20 and 50 is: 1000
The division of 20 and 50 is: 0.4
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 with separate parameterized functions like C++. However, we can achieve similar functionality by using the concept of default arguments as above discussed.

-------------------  OR  --------------------

class Calculator:
    def add(self, *args):
        return sum(args)   # sum() makes addition

# Create object
calc = Calculator()

print(calc.add(10))          # Output = 10
print(calc.add(10, 20))      # Output = 30
print(calc.add(10, 20, 30))  # Output = 60

NB: here, *args allows us to pass any number of arguments.

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.