Python Program to Add Two Numbers | How to Add Two Numbers in Python
In this post we will learn about how to write a program in python two add two number.
We are going to learn different ways to add two number using python programming language.
- Simple program to add two numbers in python.
- Simple program to add two numbers in python that takes input from the user.
- Python program to add two numbers using function.
- Program in python to add two number using class
Simple python program to add two numbers
This is simple python program to add two numbers without user input.
Program
num1 = 20;
num2 = 30;
result = num1+num2;
print(result);
Output:
50
In the above program two variables i.e num1 and num2 is assigned a value of 20 and 30 respectively. The type of variable is not declared because Python is a strong typed language where declaration happens automatically when a value is assigned to the variable.
The third variable "result" is used to store the result after addition two numbers and then the result is displayed on the output screen using the print function.
Python program to add two numbers from user input
This is simple python program to add two number by taking input from the user using input function.
The input() function in python is used to take input from the user and store it in the variable.
Program:
num1 = int(input("Enter First Number\n"));
num2 = int(input("Enter Second Number\n"));
result = num1+num2;
print(result);
Output:
50
In the above program num1 and num2 are two variables that are used to store values that need to be added.
The values of num1 and num2 are taken from the user using the input() of type int. Here we need to mention the type of input that user need to enter as in above program it is mentioned as integer.
If input type is not mentioned then Python automatically considers it as of type string and it will return simple concatenation of two string therefore input type is declared as of type integer.
Inside input() function statement is written that will be displayed on the screen when program is run and "\n" is used to bring the cursor on the next line.
Python program to add two number using function
Program:
def addNumber(num1, num2):
result = num1+num2;
print(result);
addNumber(20, 30);
Output:
50
The above program is about how to add two numbers in python using function. Function is set or block of statement that is used to perform certain action. Here "def" is a keyword used in Python to define function which is the accompanied by function name and then a parenthesis.
In the above program addNumber is the name of the function defined and this function carries two arguments num1 and num2.
Again num1 and num2 is added using the addition operator and the sum of the number is saved in the third variable named as result.
In the last line again the above defined function is called and the value of both the variables num1 and num2 is passed as an argument to give reference to the function.
Python program to add two number using class
Program in python to add two number using class by creating object and calling the function. \
A class can be defined as collection of objects.
or
A class is blueprint of an object and an object is a run time entity. In simple words we can say that collection of an object is referred as class.
Program:
class Addition:
def addNumber(self, num1, num2):
result = num1+num2;
return result;
obj = Addition();
print(obj.addNumber(20, 30));
Output:
50
In the above program class with name Addition is defined and then a method named addNumber is also defined.
This method takes three arguments self, num1 and num2, where self is a keyword which is used to access the attribute and methods of the class and num1 , num2 are two variables.
In the next line two numbers are added and the result of their sum is stored in the third variable called "result" which is returned using to the method using the return statement in the next line.
Now an object named "obj" is created of type Addition using the statement obj = Addition();
This object invokes the function addNumber which is defined inside the class and the values of num1 and num2 is passed as a parameter which is finally printed using the print statement and the output is displayed on the screen.