Create a new class Math1 in Python
Create a New Python Class Math1
- Create a new class Math1
- Create a Constructor for Math1 class
- Define two instance varables num1, num2
- Write a method add(self) to add the numbers and return sum
- Create two objects obj1 and obj2, passing 10,20 and 40,60 values
- Call add() method with object1
- Call add() method with object 2
Create a New Class Math1 in Python
How To Create A Class Math1 to Add Two Numbers
- First of all we will dfine a class Math1 using 'class' keyword.
- Define a construcor that is __init__() method to initialize newly created objects.
- We will define two instance variables num1, num2 in the __init__() method.
- Three parameters are used in __init__(self, num1, num2)
- 'self' refers to the current object upon which the __init__() method is called.
- num1 and num2 are the instance variables of Math1 class.
- We will assign the values of parameters to the instance variables, so that both of the instance variables are initialized at the time of object creation (AKA instanitiation).
- Similarly, define another method add(self) to return the sum of the two instance variables.
- Now, the class code is ended here, now add some statements to main() at zero indentation level.
- Add the statements to create two objects, obj1 and obj2 with values 10,20 and 40,60 respectively.
- Call the add() method for the newly created objects of Math1 class.
- Hence the output of the program will display on the screen
Coding of the Program Math1 Class
# Create a class Math1 # Math1 class has two instance variables num1, num2 # defined in the constructor that is __init__() method # class Math1 uses __init__() constructor to intialize two instance variables # Math1 class has one method called add() to return sum of the two # instance variables num1, num2 # author: www.EasyCodeBook.com (c) class Math1: def __init__(self, num1, num2): self.num1 = num1 self.num2 = num2 def add(self): return self.num1 + self.num2 obj1 = Math1(10,20) sum1 = obj1.add() print('Sum1 =' , sum1) obj2 = Math1(40,60) sum2 = obj2.add() print('Sum2 =' , sum2)
Output:
Sum1 = 30 Sum2 = 100
Python ProgramĀ with comments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# Create a class Math1 # Math1 class has two instance variables num1, num2 # defined in the constructor that is __init__() method # class Math1 uses __init__() constructor to intialize two instance variables # Math1 class has one method called add() to return sum of the two # instance variables num1, num2 # author: www.EasyCodeBook.com (c) # write a class definition class Math1: # define constructor with three parameters self, num1, num2 # the constructor will initialize the instance variables # using 'self' keyword in Python is like using 'this' keyword in Java # 'self' refers to current objet def __init__(self, num1, num2): self.num1 = num1 self.num2 = num2 # define a method add() with the parameter 'self' # add() method will retun sum of the two instance variables # for current object def add(self): return self.num1 + self.num2 # end of class Math1 # main # create first object of class Math1 obj1 = Math1(10,20) # call add() method on object obj1 sum1 = obj1.add() # print the value of sum varable print('Sum 1=' , sum1) # create second object of class Math1 obj2 = Math1(40,60) # call add() method on object obj2 sum2 = obj2.add() # print the value of sum varable print('Sum 2=' , sum2) |