Page Contents
How to add two numbers in Python is one of the most searched and easiest application of Python . In this tutorial we will learn how to add two numbers in Python using various ways –
How to add two numbers in Python
# how to add two numbers
# declaring two numbers
number1 = 10
number2 = 15
# adding two numbers
addition = number1 + number2
print(addition)
25
In the above code first we have declared two variables number1
and number2
as value 10
and 15
respectively . Then we have added the two numbers using +
operator and stored the number in a new variable addition
. After that we have printed addition
which printed 25
in the output .
How to add two numbers in Python taking user input
# how to add two numbers taking user input
# declaring two numbers
number1 = input("Enter the first number :")
number2 = input("Enter the second number :")
# adding two numbers
addition = int(number1) + int(number2)
print(addition)
Enter the first number :10 Enter the second number :15 25
In the above code we have added two numbers by taking user input . We have taken user input using input()
function and stored them in number1
and number2
variable . Before adding the two numbers we need to typecast both the variable into a integer using int()
function as input()
function always takes input as a string . After that we have printed the addition
.
After executing the program we have given 10
and 15
in the input()
function . So number1
and number2
assigned with 10
and 15
and printed 25
in the output .
How to add two numbers in Python using function
# how to add two numbers using function
# defining the function
def add(number1,number2):
addition = number1 + number2
print(addition)
# calling the add function
add(10,15)
25
In the above code we have written a function named add
to add two numbers taking number1
and number2
as parameter . When calling the function with argument 10
and 15
, number1
and number2
is assigned with 10
and 15
. Then number1
and number2
is added and stored in addition
variable . After that addition
is printed which printed 25
in the output .
How to add two numbers in Python using class
# how to add two numbers using class
# defining the class
class add:
def __init__(self,number1,number2):
self.addition = number1 + number2
# creating a object
addnumbers = add(10,15)
# printing the object variable addition
print(addnumbers.addition)
25
In the above code we have defined a class named add
. A constructor is defined in the class which have taken two parameters number1
and number2
except self . Then we have added the two numbers and stored the number in a new object variable addition
.
After that we have created a new object addnumbers
of add
class by passing argument as 10
and 15
. So the value of the object variable addition
have become 25
. Then we have printed the object variable addition
and it printed 25
in the output .
Conclusion
In this tutorial we have seen various ways to add two numbers in Python . Now it’s on you and your requirement which method is the best fit for you .
If you want to say anything about this article please comment below . If you enjoy the article please like and share the article .