Python Program to add two numbers using the default constructor


In this program, you will learn how to add two numbers using the default constructor in Python.


self.z = self.a + self.b

Example: How to add two numbers using default constructor in Python

class Test:

    def __init__(self):
        self.a = int(input("Enter first number:"))
        self.b = int(input("Enter second number:"))

        self.z = self.a + self.b

        print("Sum is:", self.z)


obj = Test()

Output:

Enter first number:10
Enter second number:20
Sum is: 30